This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
docker:build_create_run_image_phpinfo [2020/03/05 14:55] admin |
docker:build_create_run_image_phpinfo [2020/03/05 16:20] (current) admin |
||
---|---|---|---|
Line 16: | Line 16: | ||
- | The php:7-apache image set the Apache public directory to /var/www/html. However, in this case, following Laravel’s conventions, we need to set it to the /var/www/public. One way to achieve this is by setting up a virtual host configuration. Create a file called 000-default.conf with the following contents: | + | The php:7-apache image set the Apache public directory to **''%%/var/www/html%%''**. However, in this case, following Laravel’s conventions, we need to set it to the **''%%/var/www/public%%''**. One way to achieve this is by setting up a virtual host configuration. Create a file called **''%%000-default.conf%%''** with the following contents: |
<code bash> | <code bash> | ||
Line 52: | Line 52: | ||
We’re set to create a production-ready image. Create a file called Dockerfile. | We’re set to create a production-ready image. Create a file called Dockerfile. | ||
- | In that folder we will need Dockerfile witch is required to build any Docker image: | + | We’ll use the FROM clause to use the offical php apache images as a starting point: |
+ | <code bash> | ||
+ | # Dockerfile | ||
+ | FROM php:7-apache | ||
+ | </code> | ||
+ | The MAINTAINER instruction should contain the developer or company name. | ||
+ | <code bash> | ||
+ | # Dockerfile | ||
+ | FROM php:7-apache | ||
+ | MAINTAINER SemaphoreCI <dev@example.com> | ||
+ | ... | ||
+ | </code> | ||
+ | Now, we need COPY the file into the image: | ||
+ | <code bash> | ||
+ | ... | ||
+ | COPY 000-default.conf /etc/apache2/sites-available/000-default.conf | ||
+ | ... | ||
+ | </code> | ||
+ | |||
+ | Laravel requires Apache’s mod_rewrite plugin to be enabled, we can do this using the a2enmod utility. With RUN, we run commands inside the container: | ||
+ | |||
+ | <code bash> | ||
+ | ... | ||
+ | RUN a2enmod rewrite | ||
+ | ... | ||
+ | </code> | ||
+ | |||
+ | To get the source files inside the container, we can use the COPY command again: | ||
+ | |||
+ | <code bash> | ||
+ | ... | ||
+ | COPY src /var/www/ | ||
+ | RUN chown -R www-data:www-data /var/www | ||
+ | ... | ||
+ | </code> | ||
+ | |||
+ | The last thing we need to do is to run the Apache server in the background. The CMD command should be used only one time in a Dockerfile, and it needs to have the following form: | ||
+ | |||
+ | <code bash> | ||
+ | CMD ["executable","param1","param2"] | ||
+ | </code> | ||
+ | |||
+ | We’ll call the start script we created earlier: | ||
+ | |||
+ | <code bash> | ||
+ | ... | ||
+ | CMD ["start-apache"] | ||
+ | </code> | ||
+ | |||
+ | The final Dockerfile should look like this: | ||
+ | |||
+ | <code bash> | ||
+ | #Donwloading and creating docker image from GitHub called php:7-apache | ||
+ | FROM php:7-apache | ||
+ | |||
+ | #Your email and information | ||
+ | MAINTAINER YOUR_EMAIL_OR_ORGANIZATION | ||
+ | |||
+ | #Copy local created file 000-default.conf to image-php-server right location | ||
+ | COPY 000-default.conf /etc/apache2/sites-available/000-default.conf | ||
+ | |||
+ | #Copy start-apache script to executible linux directory | ||
+ | COPY start-apache /usr/local/bin | ||
+ | |||
+ | #Laravel requires Apache’s mod_rewrite plugin to be enabled, we can do this using the a2enmod utility. With RUN, we run commands inside the container | ||
+ | RUN a2enmod rewrite | ||
+ | |||
+ | # Copy application source and permissions for source folder | ||
+ | COPY index.php /var/www/public/ | ||
+ | RUN chown -R www-data:www-data /var/www | ||
+ | |||
+ | CMD ["start-apache"] | ||
+ | </code> | ||
+ | Our folder contains a Dockerfile, a 000-default.conf and start-apache. The docker build command will build the Dockerfile inside the current directory: | ||
+ | |||
+ | <code bash> | ||
+ | [root@localhost v1]# docker build -t php-7-apache:v1 . | ||
+ | Sending build context to Docker daemon 6.144kB | ||
+ | Step 1/8 : FROM php:7-apache | ||
+ | 7-apache: Pulling from library/php | ||
+ | 68ced04f60ab: Pull complete | ||
+ | 1d2a5d8fa585: Pull complete | ||
+ | 5d59ec4ae241: Pull complete | ||
+ | d42331ef4d44: Pull complete | ||
+ | 408b7b7ee112: Pull complete | ||
+ | 570cd47896d5: Pull complete | ||
+ | 2419413b2a16: Pull complete | ||
+ | 2c7832852643: Pull complete | ||
+ | 8b0b209a25bc: Pull complete | ||
+ | 46011418685f: Pull complete | ||
+ | 68be3748ea55: Pull complete | ||
+ | 4e3af655ec1e: Pull complete | ||
+ | 9f579d3b7159: Pull complete | ||
+ | Digest: sha256:c496c0f962eaaea0f52d9c068bf331fe477703d4657f99b955a2a35a4c3486c4 | ||
+ | Status: Downloaded newer image for php:7-apache | ||
+ | ---> d753d5b380a1 | ||
+ | Step 2/8 : MAINTAINER YOUR_EMAIL_OR_ORGANIZATION | ||
+ | ---> Running in 39e672a90b5a | ||
+ | Removing intermediate container 39e672a90b5a | ||
+ | ---> 4d25a2d00117 | ||
+ | Step 3/8 : COPY 000-default.conf /etc/apache2/sites-available/000-default.conf | ||
+ | ---> 19a2cdc56a9f | ||
+ | Step 4/8 : COPY start-apache /usr/local/bin | ||
+ | ---> 603ec78bbed1 | ||
+ | Step 5/8 : RUN a2enmod rewrite | ||
+ | ---> Running in a7fff2718075 | ||
+ | Enabling module rewrite. | ||
+ | To activate the new configuration, you need to run: | ||
+ | service apache2 restart | ||
+ | Removing intermediate container a7fff2718075 | ||
+ | ---> 9bdfd2fefddd | ||
+ | Step 6/8 : COPY index.php /var/www/public/ | ||
+ | ---> 1c116f043a28 | ||
+ | Step 7/8 : RUN chown -R www-data:www-data /var/www | ||
+ | ---> Running in 84452265396e | ||
+ | Removing intermediate container 84452265396e | ||
+ | ---> d059277e4af0 | ||
+ | Step 8/8 : CMD ["start-apache"] | ||
+ | ---> Running in a82d06ffb356 | ||
+ | Removing intermediate container a82d06ffb356 | ||
+ | ---> 6f4b2f7f4ec3 | ||
+ | Successfully built 6f4b2f7f4ec3 | ||
+ | Successfully tagged php-7-apache:v1 | ||
+ | </code> | ||
+ | |||
+ | If we list our Docker images now, we’ll see our new built image: | ||
+ | |||
+ | <code bash> | ||
+ | [root@localhost v1]# docker images | ||
+ | REPOSITORY TAG IMAGE ID CREATED SIZE | ||
+ | php-7-apache v1 6f4b2f7f4ec3 2 hours ago 414MB | ||
+ | </code> | ||
+ | |||
+ | Finally run a container on port 80: | ||
+ | |||
+ | <code bash> | ||
+ | [root@localhost v1]# docker run -d -p 80:80 php-7-apache:v1 | ||
+ | 051f5da1a58e483ff672ef32b7a6124a809893b31a3caa27c907a100e8d935e9 | ||
+ | </code> | ||
+ | |||
+ | Check is container running: | ||
+ | <code bash> | ||
+ | [root@localhost v1]# docker ps -a | ||
+ | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | ||
+ | 051f5da1a58e php-7-apache:v1 "docker-php-entrypoi…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp jovial_vaughan | ||
+ | </code> | ||
+ | Result : | ||
+ | {{ :linux:docker_phpinfo_v1.png |}} | ||
+ | |||