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
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
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"]
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;"]
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;"]
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>
Set the signal to send when exiting the container.
STOPSIGNAL
STOPSIGNAL <SIGNAL_NUMBER>
STOPSIGNAL <SIGNALNAME>
#Description example
STOPSIGNAL 9
STOPSIGNAL SIGKILL
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 ~
Set environment variables in Dockerfile
ENV
ENV <KEY> <VALUE>
ENV <KEY>=<VALUE>
#Description example
ENV MyName glaceon
ENV MyName=glaceon
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
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"]
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
Specify the port number to be published by the container.
EXPOSE
EXPOSE <PORT_NUMBER>
#Description example
EXPOSE 8080
Define variables to use in Dockerfile.
ARG
ARG <NAME>=<VALUE>
#Description example
ARG YOURNAME="glaceon"
Set the default shell settings when executing commands in shell format.
SHELL
SHELL ["<PATH OF SHELL>", "<PARAMETER>"]
#Description example
SHELL ["/bin/bash", "-c"]
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/"]
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/"]
Assign a volume (*) to the image.
VOLUME
VOLUME ["<MOUNT_POINT>"]
#Description example
VOLUME ["/var/log/"]
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:
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"
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
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
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
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
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"
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
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
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"
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