I think that there are many people who are familiar with Docker and are at the level of "It's about time to develop using Docker image containers."
I am one of them, but even if I tried to use it while reading the reference, it could not be handled well and development was delayed.
This time, I wrote a commentary on docker-compose up
, which is the most basic of all, and I'm quite addicted to it if I don't understand it.
I am also a beginner, so I would appreciate it if you could point out any mistakes.
This time, for the purpose of explaining the command, let's assume a simple project that starts one container.
.
├── Dockerfile
├── docker-compose.yml
├── file.txt
└── file2.txt
FROM ubuntu:18.04
WORKDIR work
ADD ./file.txt /work/file.txt
CMD ["ls"]
docker-compose.yml
version: '3.7'
services:
test:
build: .
image: my-alpine-image
container_name: my-apline
Let's see what happens when we try docker-compose up
with the above configuration.
$ docker-compose up
Building test
//Step is omitted
Successfully built ff8e7d96a995
Successfully tagged my-alpine-image:latest
WARNING: Image for service test was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating my-apline ... done
Attaching to my-apline
my-apline | file.txt
my-apline exited with code 0
The following two points are important here.
my-alpine-image
my-alpine
In other words, it is the same as docker run
. Let's actually check the image and container.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d14fde119f0 my-alpine-image "/bin/bash" 10 minutes ago Exited (0) 10 minutes ago my-apline
$ REPOSITORY TAG IMAGE ID CREATED SIZE
my-alpine-image latest ff8e7d96a995 10 minutes ago 64.2MB
You can also see that the console contains file.txt, as it prints my-apline | file.txt
.
This behavior does not change even if it is a multiple container. Therefore, ** Here, "I see, I understand Docker Compose", which leads to a misunderstanding after that. ** **
Next, let's modify the Dockerfile so that test2.txt is also included in the container.
FROM ubuntu:18.04
WORKDIR work
ADD ./file.txt /work/file.txt
ADD ./file2.txt /work/file2.txt
CMD ["ls"]
Now, try running docker-compose up
. Then, the result was as follows.
$ docker-compose up
Starting my-apline ... done
Attaching to my-apline
my-apline | file.txt
my-apline exited with code 0
As you can see from the output, the container (my-apline) does not contain test2.txt.
Because this time docker-compose up
is ** because I just started the container I created earlier **.
As evidence, you can see that there is no difference when checking the container and the image.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d14fde119f0 my-alpine-image "/bin/bash" 10 minutes ago Exited (0) 10 minutes ago my-apline
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-alpine-image latest ff8e7d96a995 10 minutes ago 64.2MB
up
So how do you get a container with the latest image, including test2.txt? ** **
The correct answer is to use the --build
option, which explicitly states that you want to "build again".
$ docker-compose up --build
Building test
//Step is omitted
Successfully built 7218cdd4f17a
Successfully tagged my-alpine-image:latest
Recreating my-apline ... done
Attaching to my-apline
my-apline | file.txt
my-apline | file2.txt
my-apline exited with code 0
You can see that both the image and the container are new.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28c26826bf20 my-alpine-image "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago my-apline
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-alpine-image latest 7218cdd4f17a About a minute ago 64.2MB
In summary, the command docker-compose up
is
I found out that.
By the way, if you docker-compose up
without an image, it will be displayed on the console.
Image for service test was built because it did not already exist. To rebuild this image you must use
docker-compose build
ordocker-compose up --build
. (It was built because the image for service testing did not exist. You need to usedocker-compose build
ordocker-compose up --build
to rebuild this image.)
I noticed that it was out. If you read English properly, you didn't have to get lost so much.
I hope it will be helpful for people who have stumbled in similar places!
Recommended Posts