What is `docker-compose up`?

Introduction

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.

Premise

This time, for the purpose of explaining the command, let's assume a simple project that starts one container.

Directory structure

.
├── Dockerfile
├── docker-compose.yml
├── file.txt
└── file2.txt

Contents of Dockerfile and docker-compose.yml

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 "up" for the time being

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.

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. ** **

Modify the Dockerfile and try uploading it again

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

How it should have been 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

Summary

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 or docker-compose up --build. (It was built because the image for service testing did not exist. You need to use docker-compose build or docker-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

What is `docker-compose up`?
Laradock docker-compose up is moss
What is Docker-compose? (Personal memorandum)
What is Cubby
What is Docker?
What is null? ]
What is java
What is Keycloak
What is maven?
What is Jackson?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is Maven Assembly?
What is a constructor?
What is vue cli
What is an interface?
What is Ruby's self?
What is hard coding?
What is a stream
What is Ruby's attr_accessor?
What is Java Encapsulation?
What is permission denied?
What is instance control?
What is an initializer?
What is Spring Tools 4
What is an operator?
What is object orientation?
What is Guava's @VisibleForTesting?
What is MVC model?
What is an annotation?
What is Java technology?
What is Java API-java
What is @ (instance variable)?
What is Gradle's Artifact?
What is JPA Auditing?
[Swift] What is dismiss?
[Java] What is flatMap?
What is a Servlet?
What is web development?
[Java] What is JavaBeans?
[Android] What is Context? ??
[Java] What is ArrayList?
[Ruby] What is true?
What is HttpSession session = request.getSession ();