I will not touch on the basic operations of Docker. It is assumed that the source code of the application is mounted in the container and developed by using bind mount.
Docker Engine: 19.03.13
Understand the characteristics of bind mount, and you will be able to build and image the entire source of the application.
It is a common technique to bind and mount the source code to a container during development, but it is necessary to devise a way to create an image including the source code when deploying.
The method is simple enough to beat, but you can prepare a separate Dockerfile for build and explicitly COPY
the source.
(I was researching it myself, and I wondered, "Is this really such a simple story ...?")
There are three main volume mount types that can be used with Docker. To explain each difference completely
--Volume --Mount for areas managed by Docker --Multiple containers can be shared --Bind mount --Mount on the path on the host --The host and container can read and write to each other --tmpfs mount --Saved in host memory --What the container uses only while the container is running --Save temporary status and confidential information
Volumes and bind mounts also have the following features: Quoted from Official Storage Overview
Tips for using bind mounts and volumes When using either bind mount or volume, keep the following in mind:
If you are trying to mount an empty volume on a directory inside a container and a file or directory exists in that directory, that file or directory will be copied into the volume. If the specified volume does not already exist when the container is started, an empty volume will be created. It is used as a method of providing data in advance at the request of the container.
If you are trying to mount a bind mount or a non-empty volume on a directory inside a container and the file or directory exists in that directory, the mount will hide the file or directory. It's the same as if you saved the file to / mnt on your Linux machine and then mounted the USB drive on / mnt. What was in / mnt is hidden by the contents of the USB drive and continues until the USB drive is unmounted. Hidden files are not deleted or modified. However, it cannot be accessed unless the bind mount or volume is unmounted.
It is assumed that you are developing by simply building an Nginx container and mounting the source code on the container. The sample directory structure and Dockerfile are shown below.
Directory structure
project-directory/
└html/
└index.html
└Dockerfile
Dockerfile
FROM nginx
COPY ./html /usr/share/nginx/html
EXPOSE 80
index.html
<!DOCTYPE html>
<head>
<title>ContainerA</title>
</head>
<body>
<h1>ContainerA!</h1>
</body>
docker run --name containerA --mount type=bind,source=(pwd)/html,target=/usr/share/nginx/html -d -p 81:80 nginx
Create a container with the following options based on DockerHub official repository nginx image
--Specify the container name with --name
--Mount the volume with --mount
--Volume also has the old option -v or --volume
, but officially recommends --mount
, so follow it.
--type
specifies the mount type (this time it is a bind mount, so bind
)
--source
specifies the mount source path (host)
--target
specifies the mount path (inside the container)
--Detach mode with -d
(start in background)
--Set port forwarding with -p
--Associate loclahost: 80
in the container with localhost: 81
of the host
The following screen is displayed
index.html
<!DOCTYPE html>
~abridgement~
<body>
<h1>ContainerA!</h1>
<p>Add message at Host.</p>
</body>
When you load it again, the added wording is displayed. This also confirms that the host and container are securely bind mounted.
docker build -t build_with_bind_data ./
Dockerfile
exists
↓ is the execution result
docker image ls
The image build_with_bind_data
is built as shown below
docker run --name containerB -d -p 82:80 build_with_bind_data
When you access http: // localhost: 82 I was able to confirm that it was exactly the same as what was displayed on Contaier A.
For the time being, if you also check the source inside containerB
docker exec containerB bash -c "cat /usr/share/nginx/html/index.html"
At build time, you can see that the specified directory / file has been copied by the COPY
command.
COMMIT
command, but the volume mount data is not included.I want to solidify the image with the COMMIT
command, but this is just an image of the settings and changes inside the container, and the volume was mounted [^ 1] on the __ container. Note that the data is not included in the image created by the commit __ </ font>.
Therefore, it is necessary to place the source code of the application with the COPY
command at build time.
[^ 1]: The mount type is either volume / bind mount / tmpfs mount.
Maybe the content is rudimentary.
As an experience of using Docker so far, I did not have the opportunity to build and manage images, so I had a hard time because I did not know the specific method of image conversion including the source.
I didn't notice the characteristics of the COMMIT
command, and was surprised by the wind," Why doesn't the source be included even if I commit? "
However, I read the official commentary carefully and deepened my understanding, so I left it in the article so as not to forget it.
Recommended Posts