[Note] How to write Dockerfile/docker-compose.yml

1. Dockerfile instructions

order


FROM #Specifying the base image
RUN #Command execution
CMD #Container execution command
LABEL #Set label
EXPOSE #Port export
ENV #Environment variable
ADD #File/Add directory
COPY #Copy file
ENTRYPOINT #Container execution command
VOLUME #Volume mount
USER #User specification
WORKDIR #Working directory
ARG #Variables in Dockerfile
ONBUILD #Commands executed after the build is complete
STOPSIGNAL #System call signal settings
HEALTHCHECK #Container health check
SHELL #Specifying the default shell

1-1. Specifying the base image (FROM instruction)

Required items in the Dockerfile. Described the information of "Which Docker image to generate the Docker container from".

FROM


FROM <IMAGE_NAME>
FROM <IMAGE_NAME>:<TAG_NAME>
FROM <IMAME_NAME>@<DIGEST>
#Description example
FROM centos:centos7

1-2. Command execution (RUN instruction)

Described when executing some command such as installing a library or executing a command for environment construction for the base image specified by the FROM instruction.

RUN


RUN <COMMAND_FOR_IMAGE_CREATION>
#Description example(Shell format)
RUN apt-get install -y nginx
#Description example(Exec format)
RUN ["/bin/bash", "-c", "apt-get install -y nginx"]

1-3. Daemon execution (CMD instruction)

Described when executing a command in a container created based on the image.

CMD


CMD <COMMAND_EXECUTED_AFTER_IMAGE_CREATION>
#Description example(Shell format)
CMD nginx -g 'daemon off;' 
#Description example(Exec format)
CMD ["nginx", "-g", "daemon off;"]

1-4. Daemon execution (ENTRYPOINT instruction)

Describes the command to be executed when the docker container run command is executed to start the Docker container from the image built from the Dockerfile.

ENTRYPOINT


ENTRYPOINT <COMMAND_EXECUTED_WHEN_THE_CONTAINER_RUNS>
#Description example(Shell format)
ENTRYPOINT nginx -g 'daemon off;' 
#Description example(Exec format)
ENTRYPOINT ["nginx", "-g", "daemon off;"]

1-5. Command executed after build is completed (ONBUILD instruction)

Describes the command to be executed when the built image is set as the base image in another Dockerfile and built.

ONBUILD


ONBUILD <COMMAND_EXECUTED_WHEN_THE_NEXT_BUILD>

1-6. System call signal setting (STOP SIGNAL instruction)

Set the signal to send when exiting the container.

STOPSIGNAL


STOPSIGNAL <SIGNAL_NUMBER>
STOPSIGNAL <SIGNALNAME>
#Description example
STOPSIGNAL 9
STOPSIGNAL SIGKILL

1-7. Container health check instruction (HEALTH CHECK instruction)

Check if the processes in the container are working properly.

HEALTHCHECK


HEALTHCHECK <OPTION> CMD <COMMAND_EXECUTED_AFTER_IMAGE_CREATION>
#Description example
HEALTHCHECK --interval=30s CMD ~
HEALTHCHECK --timeout=30s CMD ~
HEALTHCHECK --retries=3 CMD ~
Options that can be set ・ --Interval = n ⇨ Health check interval · --Timeout = n ⇨ Health check timeout ---Retries = n number of timeouts

1-8. Setting environment variables (ENV instruction)

Set environment variables in Dockerfile

ENV


ENV <KEY> <VALUE>
ENV <KEY>=<VALUE>
#Description example
ENV MyName glaceon
ENV MyName=glaceon

1-9. Specifying the working directory (WORKDIR instruction)

Specify the working directory to execute the following instructions written in Dockerfile. If the specified directory does not exist, create a new one. ・ ** RUN command ** ・ ** CMD command ** ・ ** ENTRYPOINT command ** ・ ** COPY command ** ・ ** ADD instruction **

WORKDIR


WORKDIR <PATH_OF_THE_WORKING_DIRECTORY>
#Description example
WORKDIR ./working_dir

1-10. Label specification (LABEL instruction)

Give the image information such as version information, creator information, and comments.

LABEL


LABEL <KEY>=<"VALUE">
#Description example
LABEL maintainer "Glaceon"
LABEL title="docker-container"
LABEL version="1.0"

If you build a Dockerfile based on the above instructions and check the generated image details named sample, The information specified by the LABEL instruction is set.

Image details


$ docker image inspect --formats="{{ .Config.Labels }}" label-sample
map[title:docker-container version:1.0 maintainer:"Glaceon"]

1-11. User specification (USER command)

Specify the user to execute the image and the following commands in the Dockerfile. ・ RUN command ・ CMD command ・ ENTRYPOINT command

USER


USER <USER_NAME/UID>
#Description example
USER glaceon

1-12. Port setting (EXPOSE instruction)

Specify the port number to be published by the container.

EXPOSE


EXPOSE <PORT_NUMBER>
#Description example
EXPOSE 8080

1-13. Setting variables in Dockerfile (ARG instruction)

Define variables to use in Dockerfile.

ARG


ARG <NAME>=<VALUE>
#Description example
ARG YOURNAME="glaceon"

1-14. Setting the default shell (SHELL instruction)

Set the default shell settings when executing commands in shell format.

SHELL


SHELL ["<PATH OF SHELL>", "<PARAMETER>"]
#Description example
SHELL ["/bin/bash", "-c"]

1-15. Add file/directory (ADD instruction)

Add files and directories on the host to the image.

ADD


ADD <HOST_FILE_PATH> <DOCKER_IMAGE_FILE_PATH>
ADD ["<HOST_FILE_PATH>", "<DOCKER_IAMGE_FILE_PATH>"]
#Description example
ADD host.html /docker_dir/
ADD ["host.html", "/docker_dir/"]

1-16. File copy (COPY command)

Copy files and directories on the host to the image.

COPY


COPY <HOST_FILE_PATH> <DOCKER_IMAGE_FILE_PATH>
COPY ["<HOST_FILE_PATH>", "<DOCKER_IAMGE_FILE_PATH>"]
#Description example
COPY host.html /docker_dir/
COPY ["host.html", "/docker_dir/"]

1-17. Volume mounting (VOLUME instruction)

Assign a volume (*) to the image.

VOLUME


VOLUME ["<MOUNT_POINT>"]
#Description example
VOLUME ["/var/log/"]

2. Overview of docker-compose.yml

Docker Compose is a tool for managing multiple containers together. By defining the container configuration information in a file called "docker-compose.yml" Multiple containers on the same host can be managed collectively. In addition, this Compose definition file is roughly divided into the following three definitions.

docker-compose


#Container service
services:
#network
networks:
#volume
volumes:

2-1. Specifying the version (version)

Items that can be described in the docker-compose.yml file differ depending on the version. If no explicit version is specified, it operates as "1.0".

version


version: "<VERSION_NUMBER>"
#Description example
version: "3.3"

2-2. Specifying the image (image)

Specify the base image that is the basis of the Docker container. Use the base image if you have it in your local environment, or automatically from Docker Hub if you don't. It will be downloaded. If no image is specified, the latest version "latest" will be downloaded.

image


image: <IMAGE_NAME>
#Description example
image: ubuntu
image: dockersample:1.0

2-3. Build the image

Describe the image configuration in the Dockerfile, build it automatically, and specify it as the base image. For build, specify the file path of Dockerfile.

build


build: <FILE_PATH_WHERE_THE_DOCKERFILE_IS>
#Description example
build: .

context/dockerfile


services:
  docker-anaconda:
    build:  
      context: .
      dockerfile: ./working-directory/Dockerfile

2-4. Specifying the command to run in the container (command/entrypoint)

The command to be run in the container is specified by command. You can also overwrite the entry point.

command/entrypoint


command: <COMMAND_EXECUTED_AFTER_IMAGE_CREATION>
entrypoint: <COMMAND_EXECUTED_WHEN_THE_CONTAINER_RUNS>
#Description example
command: /bin/bash
entrypoint:
    - php
    - -d
    - memory_limit=-1

2-5. Links between containers

When you want to link to another container using the link function, set the container name of the link destination. If you want to give an alias name separately from the container name, set "Service name: Alias ​​name".

links


links: <SERVICE_NAME>
links: <SERVICE_NAME>:<ALIAS_NAME>
#Description example
links:
  - docker-anaconda
  - docker-anaconda:py01

2-6. Communication between containers (ports/expose)

The port exposed by the container is specified by ports. "Host machine port number: Container port number" or Specify "Container port number only"

ports/expose


ports: "<HOST_MACHINE_PORT_NUMBER>:<CONTAINER_PORT_NUMBER>"
ports: "<CONTAINER_PORT_NUMBER>"
expose: "<CONTAINER_PORT_NUMBER>"
#Description example
ports:
  -  "3000"
  -  "8000:8000"
  -  "49100:22"
  -  "127.0.0.1:8001:8001"
expose:
  - "3000"
  - "8000"

2-7. Definition of service dependencies (depends_on)

Specify depends_on when defining dependencies for multiple containers. The following example describes the py01 and py02 containers when starting docker-anaconda. It is a description to start.

depends_on


depends_on: <CONTAINER_DEPENDED_ON>
#Description example
services:
  docker-anaconda:
    build: .
    depends_on:
      - py01
      - py02
  py01:
    image: py01
  py02:
    image: py02

2-8. Specifying container environment variables (environment/env_file)

To specify the environment variables in the container, specify environment. Specify variables in either YAML array format or hash format. If there are many environment variables you want to specify, define the environment variables in a separate file and Specify the file with env_file and read it.

environment


environment: 
  - <KEY1>=<VALUE1>
  - <KEY2>
environment:
  KEY1: VALUE2
  KEY
env_file: <ENVFILE>
#Description example
environment:
  - HOGE=fuga
  - FOO
environment:
  HOGE: fuga
  FOO
env_file:
  - ./envfile1
  - ./app/envfile2
  - /tmp/envfile3

2-9. Container information settings (container_name/labels)

Name the container generated by Docker Compose with container_name. If you want to label the container, specify labels.

container_name/labels


container_name: <CONTAINER_NAME>
labels: 
  - "<KEY>=<VALUE>"
labels: 
 <KEY>: "<VALUE>"
#Description example
container_name: docker-anaconda
labels:
  - "com.example.department=Finance"
labels:
  com.example.department: "Finance"

2-10. Container data management (volumes/volumes_from)

Specify "volumes" when mounting volumes on the container. To specify the path to mount on the host side "Host directory path: Container directory path" To specify. By adding "ro:" after the volume specification, Volumes can be mounted read-only. Also, when mounting all volumes from another container, Specify the container in volumes_from.

volumes/volumes_from


volumes: <MOUNT_POINT>
volumes: <HOST_DIRECTORY_PATH>:<CONTAINER_DIRECTORY_PATH>
volumes_from: <CONTAINER_NAME>
#Description example
volumes:
 - /var/lib/mysql
 - cache/:/tmp/cache
volumes:
 - ~/configs:/etc/configs/:ro
volumes_from:
 - docker_anaconda


Recommended Posts

[Note] How to write Dockerfile/docker-compose.yml
How to write Rails
How to write dockerfile
How to write docker-compose
How to write Mockito
How to write migrationfile
How to write good code
Bit Tetris (how to write)
How to write java comments
Great poor (how to write)
How to write Junit 5 organized
How to write Rails validation
How to write Rails seed
[Ruby] How to write blocks
How to write Rails routing
java: How to write a generic type list [Note]
[Rails] How to use devise (Note)
Studying Java # 6 (How to write blocks)
[Rails] How to write in Japanese
How to write a ternary operator
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to write Java variable declaration
Y-shaped road tour (how to write)
How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
[Basic] How to write a Dockerfile Self-learning ②
[Note] How to get started with Rspec
[Introduction to Java] How to write a Java program
How to deploy
[Java] How to output and write files!
How to use Java Scanner class (Note)
[Note] How to use Rails 6 Devise + cancancan
How to write Spring AOP pointcut specifier
Ruby: CSV :: How to use Table Note
Note: [Docker] How to start and stop
How to write an RSpec controller test
[SpringBoot] How to write a controller test
How to write and explain Dockerfile, docker-compose
JDBC promises and examples of how to write
Rails: How to write a rake task nicely
[JavaFX] How to write Eclipse permissions in build.gradle
How to write offline 15th reference question answer
[Rails] How to write when making a subquery
Java Development Basics ~ How to Write Programs * Exercise 1 ~
How to write an if statement to improve readability-java
JUnit 5: How to write test cases in enum
Note how to use Swift super basic TableView
How to install NVIDIA driver on Ubuntu 18.04 (Note)
How to use Segmented Control and points to note
Note how to rollback Mysql deployed on Heroku
Offline real-time how to write F06 implementation example
How to write code that thinks object-oriented Ruby
How to write test code with Basic authentication
How to write React Native bridge ~ Android version ~
[Java] Memo on how to write the source
How to write Java String # getBytes in Kotlin?
Notes on how to write comments in English
How to call AmazonSQSAsync
How to use Map
How to use rbenv