A thorough and thorough introduction to Docker that you can copy and paste - Dockerfile and Docker build edition

Hello.
I'm Mandai, in charge of Wild on the development team.
As the second part of the Docker series that you can copy and paste, I would like to try the Docker build command.
Once you can do Docker build,
- You can create a new Docker image by modifying an existing Docker image
- When creating a similar Docker image, it makes the process easier if there is a base Docker image
- In the case of a VM image, the packaged image will be distributed using Vagrant, but in the case of a Docker image, only the Dockerfile and the files to be placed in the Docker container will be distributed
There are advantages such as:
Part 1 is here → This will solve the PHP version upgrade! An introduction to Docker that can be done with copy and paste
So let's get started
Use docker build to arrange docker images
First, create a directory on the host OS to store the files used for the build
vagrant@vagrant:~$ mkdir docker_php7 vagrant@vagrant:~$ cd docker_php7
It is very important to note that when creating a new Docker image using a Dockerfile, you cannot access directories above the directory where the Dockerfile is located.
Therefore, files required for COPY and ADD must be placed under the directory where the Dockerfile is located.
Next, create the PHP file on the host OS that will be placed in the Docker container
vagrant@vagrant:~/docker_php7$ echo "<?php phpinfo();" > info.php
Next, create a Dockerfile
vagrant@vagrant:~/docker_php7$ vi Dockerfile # Contents of Dockerfile FROM php:7.0.2-apache # Specify the base docker image when doing docker build (required) MAINTAINER Y.Mandai Beyond Inc. # Dockerfile maintainer information (optional) COPY info.php /var/www/html/ # Copy info.php to /var/www/html/ in the container EXPOSE 80 # Open port 80 of the container to the outside (so that it can be seen from the host OS)
If you look at
the Dockerfile for php:7.0-apache , you'll see that the container build command includes compiling PHP in the container, which is quite helpful.
By referring to this, you can create a Docker image with multibyte-related extension modules.
Now, let's create a Docker image using Docker build
vagrant@vagrant:~/docker_php7$ docker build -t addinfo . Sending build context to Docker daemon 3.072 kB Step 1 : FROM php:7.0.2-apache ---> 797e8c22cdde Step 2 : MAINTAINER Y.Mandai Beyond Inc. ---> Running in 49c22633979a ---> 8550f12577fd Removing intermediate container 49c22633979a Step 3 : COPY info.php /var/www/html/ ---> 9e143b3f9df5 Removing intermediate container b70897d9000e Step 4 : EXPOSE 80 ---> Running in 31b54ea14f9f ---> 38445ad62923 Removing intermediate container 31b54ea14f9f
The -t option sets the name of the Docker image, and the final "." specifies the path to the Dockerfile
docker build -t addinfo:hogehoge .
You can also create it with the tag hogehoge
Let's check it with the docker images command
vagrant@vagrant:~/docker_php7$ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE addinfo latest 31b54ea14f9f About an hour ago 521 MB php 7.0.2-apache 797e8c22cdde 5 weeks ago 521 MB
Now that we have created a new Docker image, we will create a Docker container from it
vagrant@vagrant:~/docker_php7$ docker run -d -p 8080:80 addinfo 862db6201b865a12415d4602219b03714605e0a89f2fa3076ae2161cf271d666
Now that we have mapped port 80 on the container to port 8080 on the host OS, let's check whether we can execute info.php using curl from the host side
vagrant@vagrant:~/docker_php7$ curl http://localhost:8080/info.php
If the contents of phpinfo() are displayed, then this experiment was successful!
That's it.
1