简体   繁体   English

如何在 docker 上运行 PHP 脚本?

[英]How to run a PHP script on docker?

I have index.php:我有 index.php:

<?php
echo "Hello World";
?>

Dockerfile from the website: https://docs.docker.com/samples/library/php/来自网站的 Dockerfile: https ://docs.docker.com/samples/library/php/

FROM php:7.2-cli
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ "php", "./index.php" ]

I build image and run container:我构建图像并运行容器:

docker build -t my-php-app .
docker run -p 7000:80 --rm --name hello-world-test my-php-app

I see only text "Hello World" but my application doesn't work in http://localhost:7000/ why?我只看到文本“Hello World”,但我的应用程序无法在http://localhost:7000/中运行,为什么?

If you want to run some script "on the fly" with php-cli you can create the container and remove it immediately after the script execution.如果您想使用php-cli “即时”运行一些脚本,您可以创建容器并在脚本执行后立即将其删除。

Just go to the directory with your code and run:只需转到包含您的代码的目录并运行:

Unix Unix

docker container run --rm -v $(pwd):/app/ php:7.4-cli php /app/script.php

Windows - cmd视窗 - cmd

docker container run --rm -v %cd%:/app/ php:7.4-cli php /app/script.php

Windows - power shell Windows - 电源外壳

docker container run --rm -v ${PWD}:/app/ php:7.4-cli php /app/script.php

--rm will remove the container after execution --rm将在执行后删除容器

-v $(pwd):/app/ will mount current directory -v $(pwd):/app/将挂载当前目录

php:7.4-cli is the image php:7.4-cli是图片

and php /app/script.php is the command which will be executed after the container is created php /app/script.php是创建容器后执行的命令

You can keep the same base image as you have php:7.2-cli , :您可以保留与php:7.2-cli相同的基本图像,:

FROM php:7.2-cli
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ "php", "./index.php" ]

build the image:构建图像:

docker build -t my-php-app .

run it:运行:

docker run --rm --name hello-world-test my-php-app

You will obtain:您将获得:

Hello World

Everything you did was correct except the port mapping ( -p 7000:80 ) which is not necessary because you don't run a web server.除了端口映射( -p 7000:80 )之外,您所做的一切都是正确的,这不是必需的,因为您没有运行 Web 服务器。

== EDIT == 编辑

If you want to run it as a web server, use the following Dockerfile:如果要将其作为 Web 服务器运行,请使用以下 Dockerfile:

FROM php:7.2-apache
COPY . /var/www/html/

build it:构建它:

docker build -t my-php-app .

and run it:并运行它:

docker run -p 8080:80 -d my-php-app

you will then have your PHP script runnnig on 8080.然后,您将在 8080 上运行您的 PHP 脚本。

  1. Create simple php script:创建简单的 php 脚本:

    echo '<?php echo "Working";' > my.php

  2. Run docker:运行泊坞窗:

    docker run -p 8080:8080 --rm -v $(pwd):$(pwd) php:7.4-cli php -S 0.0.0.0:8080 $(pwd)/my.php

  3. Open in browser:在浏览器中打开:

    http://localhost:8080/

Many answers suggest using Apache for this, but that is not required.许多答案建议为此使用 Apache,但这不是必需的。 You need to have your application in the container run continuously on a specific port.您需要让容器中的应用程序在特定端口上持续运行。 You can keep the php:7.2-cli image, but your CMD should be different:您可以保留php:7.2-cli图像,但您的 CMD 应该不同:

CMD [ "php", "-S 0.0.0.0:80", "./index.php" ]

This will run the built-in PHP webserver and after that you can expose it with the docker run command you already had这将运行内置的 PHP 网络服务器,然后您可以使用您已经拥有的 docker run 命令公开它

Here is a quick and simple example with Docker on Windows 11 , assuming you have a similar directory structure as the example below:这是一个在Windows 11上使用Docker的快速简单示例,假设您具有与以下示例类似的目录结构:

C:\Users\YourName\Workspace\MyProject\program.php

And program.php has the following content:program.php的内容如下:

<?php echo "It works!"; ?>

Then, in the Command Prompt, navigate to the project directory:然后,在命令提示符中,导航到项目目录:

cd C:\Users\YourName\Workspace\MyProject

Run with CLI使用 CLI 运行

docker run --rm -p 8080:8080 -v %CD%:/cli php:7.4-cli php -S 0.0.0.0:8080 /cli/program.php

View: http://localhost:8080查看: http://localhost:8080

Run with SERVER使用服务器运行

docker run --rm -d -p 8081:80 -v %CD%:/server --mount type=bind,source="%CD%",target=/var/www/html php:apache

View: http://localhost:8081/program.php查看: http://localhost:8081/program.php

Then feel free to modify program.php and refresh the page.然后随意修改program.php并刷新页面。

Environment环境

  • Docker version 20.10.16, build aa7e414 Docker 版本 20.10.16,构建 aa7e414
  • Windows 11 Home, Version 22H2, OS build 22622.436 Windows 11 家庭版,版本 22H2,操作系统版本 22622.436

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM