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

Hello.
I'm Mandai, the Wild team member in charge of development.

As part two of our "Docker You Can Do with Copy and Paste" series, we'll try out the `docker build` command.
Once you can run `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 → PHP version upgrade problem solved! Docker basics you can learn by 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

This is quite important: when creating a new Docker image using a Dockerfile, you cannot access directories higher up than the directory where the Dockerfile is located.
Therefore, any files needed for COPY or ADD must be placed in a directory or subdirectory of the Dockerfile.

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)

the Dockerfile for php:7.0-apacheLooking at
, the commands for building the container include how to compile PHP within the container, which is quite helpful.
Using this as a reference, 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 all

If you found this article helpful,please give it a "Like"!
1
Loading...
1 vote, average: 1.00 / 11
1,011
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Yoichi Bandai

My main job is developing web APIs for social games, but thankfully I'm also given the opportunity to work on various other tasks, including marketing.
My image rights within Beyond are treated as CC0.