How to write and explain Dockerfile, docker-compose

Introduction

It was created by myself as a docker beginner for docker beginners as well. I wrote this article by excerpting terms commonly used in Dockerfile and docker-compose. I hope it will be helpful to everyone. ✊

Target person

--Docker, docker-compose Beginners --For those who have forgotten how to write the file

What is Dockerfile

Dockerfile is a text file that takes instructions and builds an image automatically. You can create the image as you like by writing her own Dockerfile without pulling.

Dockerfile Commonly used instruction explanation (with example)

FROM Specify Docker image (create it without permission even if there is no image locally)

FROM python:3.7

ENV Set environment variables (key, value)

ENV PYTHONUNBUFFERED 1

RUN Run the shell before creating the image

RUN mkdir /code

CMD Run the shell after creating the image, run only the last one even if you use more than one

CMD python3 app.py

WORKDIR Specifies the directory where the docker container starts commands

WORKDIR /usr/share/nginx/html

COPY Copy files from host to Docker image, not decompressed for compressed files

# /~/Absolute path if enclosed in, relative path of WORKDIR if not
COPY index.html index.html

ADD Copy files from host to Docker image, decompress for compressed files

# /~/Absolute path if enclosed in, relative path of WORKDIR if not
ADD requirements.txt /code/

VOLUME Specify the directory to mount the data on the host side

VOLUME /code

What is docker-compose?

docker-compose is a tool for Docker applications that use YAML files to define and run multiple containers.

docker-compose commentary (with example)

sample

version: '3'

services:
    db:
        image: postgres
        container_name: docker_compose_postgres
        ports:
            - "5555"
        environment:
          - POSTGRES_DB=postgres
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres
        networks:
          - docker_link

    web:
        build: .
        command: python3 manage.py runserver 0.0.0.0:8000
        volumes:
            - .:/code
        ports:
            - "8000:8000"
        depends_on:
            - db
        networks:
            - docker_link

    networks:
        docker_link:

--version: version of docker-compose --service: Running application --image: Docker image specification --container_name: container name --ports: You can specify not only both host side and container side ports but also public port only. --environment: Add environment variables --build: Specify the directory where Dockerfile exists --command: Override the default command --volumes: Specify the directory to mount the host directory --depends_on: Specify dependencies between services --network: Specify which network to connect to --networks: Specifying networks

Referenced site

-Dockerfile Reference

-Docker-compose overview -Compose File Reference

Recommended Posts

How to write and explain Dockerfile, docker-compose
How to write dockerfile
How to write docker-compose
[Basic] How to write a Dockerfile Self-learning ②
[Java] How to output and write files!
How to write Rails
How to create docker-compose
How to write migrationfile
JDBC promises and examples of how to write
How to write good code
[Java] Types of comments and how to write them
Bit Tetris (how to write)
How to write java comments
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
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
How to write and notes when migrating from VB to JAVA
How to create and launch a Dockerfile for Payara Micro
How to use EventBus3 and ThreadMode
Studying Java # 6 (How to write blocks)
[Rails] How to write in Japanese
How to call classes and methods
[Rails] How to speed up docker-compose
Baseball ball count (how to write)
How to use equality and equality (how to use equals)
How to connect Heroku and Sequel
How to convert LocalDate and Timestamp
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
[Docker-compose] How to use unnamed and named volumes of volumes. Bind mount
Offline real-time how to write F03 ruby and C implementation example
How to use OrientJS and OrientDB together
Summary of how to write annotation arguments
[Introduction to Java] How to write a Java program
How to set up and use kapt
How to build SquashTM and how to support Japanese
How to find the tens and ones
[Easy] How to upgrade Ruby and bundler
How to monitor nginx with docker-compose with datadog
How to use substring and substr methods
How to write Spring AOP pointcut specifier
Note: [Docker] How to start and stop
How to write an RSpec controller test
[SpringBoot] How to write a controller test
How to use @Builder and @NoArgsConstructor together
How to deploy to AWS using NUXTJS official S3 and CloudFront? With docker-compose
Java beginner escape boot camp Part 1 Java class structure and how to write
AtCoder is called TLE and talks about how to write beautiful code
How to use args :, environment :, env_file: and .env files with docker-compose command
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
How to output Excel and PDF using Excella
[Java] How to use FileReader class and BufferedReader class