Docker PHP应用程序示例
我们可以使用docker运行php应用程序。 在以下步骤中,将使用创建Docker并运行php应用程序。
- 创建一个目录
使用以下命令创建一个目录来组织配置和PHP代码文件。
zyiz@ubuntu:~$ mkdir /home/zyiz/docker/php-docker-app
- 创建一个PHP文件
创建一个名称为:index.php
的PHP文件(vi /home/zyiz/docker/php-docker-app/index.php
),内容如下 -
<?php echo "This is first PHP Script build by docker."; ?>
- 创建一个 Dockerfile 文件
FROM php:7.0-apache COPY . /var/www/html RUN rm -rf /etc/apache2/sites-enabled/000-default.conf
创建上面文件之后(vi /home/zyiz/docker/php-docker-app/Dockerfile
),项目有两个文件,如下面所示 -
zyiz@ubuntu:~$ cd /home/zyiz/docker/php-docker-app/ zyiz@ubuntu:~/docker/php-docker-app$ ll total 16 drwxrwxr-x 2 zyiz zyiz 4096 Jun 3 20:10 ./ drwxrwxr-x 4 zyiz zyiz 4096 Jun 3 20:08 ../ -rw-rw-r-- 1 zyiz zyiz 44 Jun 3 20:10 Dockerfile -rw-rw-r-- 1 zyiz zyiz 66 Jun 3 20:08 index.php zyiz@ubuntu:~/docker/php-docker-app$
注意:Dockerfile文件的名称要区分大小写。
- 创建Docker映像
zyiz@ubuntu:~/docker/php-docker-app$ docker build -t php-app .
在下面的屏幕截图中,是正在创建docker映像的输出。
zyiz@ubuntu:~/docker/php-docker-app$ sudo docker build -t php-app . Sending build context to Docker daemon 3.072kB Step 1/2 : FROM php:7.0-apache 7.0-apache: Pulling from library/php 10a267c67f42: Downloading 23.79MB/52.58MB 10a267c67f42: Pull complete 370377701f89: Pull complete 455c73a122bc: Pull complete fb71bac61c47: Pull complete 288a1d91ad4e: Pull complete 86e0256ba4b0: Pull complete f14fbe7a9dfb: Pull complete 0f36dd91c0ab: Pull complete ee4d938396ab: Pull complete a0e5f6ef63c3: Pull complete 4d3b68c01e8a: Pull complete 855c8caa7af5: Pull complete cd53b61c5ad7: Pull complete Digest: sha256:9bec874758f731e5e4e2908744a5e2abfc8bbb6c94425b8abd8d6ef3b5938288 Status: Downloaded newer image for php:7.0-apache ---> a4322279ced1 Step 2/2 : COPY . /var/www/html ---> c2e626f9d9c6 Removing intermediate container 9a44af0ee1bb Successfully built c2e626f9d9c6 Successfully tagged php-app:latest zyiz@ubuntu:~/docker/php-docker-app$
现在可以看看Docker容器中所有可用的映像。使用 docker images
-
zyiz@ubuntu:~/docker/php-docker-app$ sudo docker images [sudo] password for zyiz: REPOSITORY TAG IMAGE ID CREATED SIZE php-app latest c2e626f9d9c6 16 minutes ago 390MB java-app latest bd61e7f49911 21 hours ago 643MB php 7.0-apache a4322279ced1 30 hours ago 390MB java 8 d23bdf5b1b1b 4 months ago 643MB hello-world latest 48b5124b2768 4 months ago 1.84kB zyiz@ubuntu:~/docker/php-docker-app$
上面的输出结果中,它显示了所有创建的可用映像中,包函了php-app
。
- 运行Docker映像
现在运行Docker映像,以下命令用于运行Docker映像。
$ sudo docker run php-app
可以看到我们的Docker映像正在运行。此映像正在IP为172.17.0.2
上运行,现在打开浏览器或使用curl 172.17.0.2
访问测试。
zyiz@ubuntu:~/docker/php-docker-app$ sudo docker build -t php-app . Sending build context to Docker daemon 4.096kB Step 1/3 : FROM php:7.0-apache ---> a4322279ced1 Step 2/3 : COPY . /var/www/html ---> 67b4cdd58ecc Removing intermediate container 506cd5be7ef3 Step 3/3 : RUN rm -rf /etc/apache2/sites-enabled/000-default.conf ---> Running in 3e0751150765 ---> 7bd0234c2184 Removing intermediate container 3e0751150765 Successfully built 7bd0234c2184 Successfully tagged php-app:latest zyiz@ubuntu:~/docker/php-docker-app$ sudo docker run php-app AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message [Sun Jun 04 04:28:11.305554 2017] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/7.0.19 configured -- resuming normal operations [Sun Jun 04 04:28:11.305706 2017] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND' 172.17.0.2:80 172.17.0.1 - - [04/Jun/2017:04:28:23 +0000] "GET / HTTP/1.1" 200 216 "-" "curl/7.47.0"
打开另一个终端,访问:curl 172.17.0.2
输出结果如下 -
zyiz@ubuntu:~$ curl 172.17.0.2 This is first PHP Script build by docker. zyiz@ubuntu:~$
扫描二维码
程序员编程王