Very polite! Introduction to docker by copying and pasting ~Dockerfile and docker build edition~
Hello.
I'm Mandai, in charge of Wild on the development team.
As part 2 of the copy-paste docker series, I would like to try the docker build command.
Once you can do docker build,
- You can create new docker images by modifying existing docker images.
- When creating a similar docker image, having a base docker image makes it easier.
- In the case of a VM image, you will be distributing the one packaged with vagrant, but in the case of a docker image, it will only be the Dockerfile and the files placed in the docker container.
There are advantages such as:
Click here for Part 1 → Kindly and politely! Introduction to docker by copying and pasting
So let's get excited.
Arrange docker images using docker build
First, create a directory on the host OS to store the files used for 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, the files required for COPY and ADD must be placed under the directory where the Dockerfile is installed.
Next, create a 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 (visible from the host OS)
Looking at
the Dockerfile for php:7.0-apache , the container building commands include compiling PHP inside the container, which is quite helpful.
By referring to this, you can create a docker image that includes 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 31b54ea14f 9f - --> 38445ad62923 Removing intermediate container 31b54ea14f9f
Set the name of the docker image with the -t option, and specify the path of the Dockerfile with the final ".".
docker build -t addinfo:hogehoge .
You can also create one with the tag hogehoge.
Let's check 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 would like to create a docker container from here.
vagrant@vagrant:~/docker_php7$ docker run -d -p 8080:80 addinfo 862db6201b865a12415d4602219b03714605e0a89f2fa3076ae2161cf271d666
Since we have mapped port 80 of the container to port 8080 of the host OS, let's check if 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() can be displayed, this experiment was a success!
That's it.