[LINUX] How to create a Dockerfile (basic)

Introduction

Docker is a technology that makes it easy to build an environment that has been introduced by many IT development companies. A Dockerfile is like a blueprint for a Docker Image. You can create a Docker image of the environment you want to build by creating a Dockerfile yourself.

Relationship between Dockerfile, DockerImage, and container

Create in the order of Dockerfile => DockerImage => container.

** How to create a Docker Image ** In the directory where the Dockerfile exists, docker build.

** How to create a container ** docker run (DockerImage name or ID)

Dockerfile instructions

Dockerfile creation procedure

FROM Instructions to determine the base image. Write it at the beginning of the Dockerfile. After FROM, basically describe the OS.

FROM OS(Choose from Docker image)

Example)FROM ubuntu:latest
Example)FROM ruby:2.5

RUN This is an instruction to execute the command corresponding to the OS described in FROM. A layer is created for each RUN. (Docker Image is created by stacking layers.)

Commands corresponding to RUN OS

Example)FROM ubuntu:latest
   RUN touch test
=>In Docker Image, a layer of touch test is additionally created in the layer of ubuntu.
(I am creating a Docker Image that creates a file called test.)

Example)FROM ubuntu:latest
   RUN apt-get update && apt-get install -y \
       curl\
       nginx
=>DockerImage will create an additional layer that installs two packages, curl and nginx, on the ubuntu layer.
(apt-get update is written to update the installed package and apt-get install is written to install the package.)
(-y is used to proceed as yes when asked yes or no during installation, and backslashes are used for line breaks to make the package easier to read.)

Since the Dockerfile is built and the DockerImage is created once, it is cached, so you can reduce the build time by additionally describing RUN below.

CMD Instructions that specify the execution command of the container. Describe it at the end of the Dockerfile.

Commands for CMD OS

Example)FROM ubuntu:latest
   RUN apt-get update && apt-get install -y \
       curl\
       nginx
   CMD ["/bin/bash"]
=>After building a Dockerfile and creating a DockerImage, when creating and executing a container with docker run,/bin/bash will be executed.

ENTRYPOINT This is an instruction that has a role similar to CMD.

CMD allows you to overwrite commands when running Docker Image (docker run). (Example: If you do docker run ls, ls will be executed instead of bash in the example at the time of CMD explanation above.)

If you use ENTRYPOINT, you cannot overwrite the command, and CMD will describe the option of ENTRYPOINT.

Commands corresponding to ENTRYPOINT OS

Example)FROM ubuntu:latest
   RUN apt-get update && apt-get install -y \
       curl\
       nginx
   ENTRYPOINT ["ls"]
   CMD ["--help"]
=>By writing the ls command in ENTRYPOINT, commands other than the ls command cannot be executed during docker run, and CMD is an optional option.--help is specified.

COPY Instructions to copy the files in the directory containing the Dockerfile (called BuildContext) to the DockerImage.

Filename in Copy Build Context

Example)FROM ubuntu:latest
   RUN mkdir /new_dir
   COPY test /new_dir
   CMD ["/bin/bash"]
=>When building a Dockerfile, it becomes a DockerImage/new_Create a directory called dir/new_A layer to be copied by the file test to dir is additionally created in the layer of utubtu.
When you create a container from this Docker Image, it will be inside the container/new_A directory called dir and a file called test have been created.

ADD Similar to the COPY command, it is an instruction that can also be copied.

** Difference between ADD and COPY ** When ADD copies a compressed file, it both copies and decompresses it. COPY is just a simple copy. Use ADD when copying and decompressing compressed files, and COPY for simple copies that are not related to compressed files.

Filename in Copy Build Context

Example)FROM ubuntu:latest
   ADD sample.tar /
   CMD ["/bin/bash"]
=>When building a Dockerfile, sample to DockerImage.tar/(Directly below the route)A layer is created to copy and unzip to.
When creating a container, sample directly under the root of the container.The tar is unzipped and created.

ENV Instructions for setting environment variables.

ENV environment variable

Example)FROM ubuntu:latest
   ENV key1 value
   CMD ["/bin/bash"]
=>Key1 when creating a container=A Docker Image with an environment variable called value is created.

WORKDIR This is an instruction that changes the execution directory of the instruction.

WORKIDIR directory

Example)FROM ubuntu:latest
   RUN mkdir sample_folder
   WORKDIR /sample_folder
   RUN touch sample_file
=> sample_Create a directory called folder and sample_sample in folder_It will be the layer that creates the file.
  (Created in a container by using WORKDIR/sample_You can execute RUN in the folder.)

reference

Udemy

Kameleon Lecturer "Docker course taught by US AI developers from scratch"

https://www.udemy.com/share/103aTRAEAdd1pTTHoC/

There is a charge, but it was very easy for me as a beginner to understand.

Finally

We hope that this post will help beginners review.

Recommended Posts

How to create a Dockerfile (basic)
How to create a Conda package
How to create a virtual bridge
How to make a crawler --Basic
How to create a config file
How to create a clone from Github
How to create a git clone folder
How to create a repository from media
How to create a Python virtual environment (venv)
How to create a function object from a string
How to create a JSON file in Python
How to create a shortcut command for LINUX
[Note] How to create a Ruby development environment
How to create a Kivy 1-line input box
How to create a multi-platform app with kivy
How to create a Rest Api in Django
[Note] How to create a Mac development environment
How to call a function
How to hack a terminal
Read the Python-Markdown source: How to create a parser
How to create a submenu with the [Blender] plugin
[Go] How to create a custom error for Sentry
How to create a local repository for Linux OS
How to create a simple TCP server / client script
[Python] How to create a 2D histogram with Matplotlib
How to create a kubernetes pod from python code
How to make a Japanese-English translation
How to write a Python class
How to put a symbolic link
How to make a slack bot
How to create your own Transform
How to create an email user
How to make a crawler --Advanced
How to create a flow mesh around a cylinder with snappyHexMesh
How to make a recursive function
[Python Kivy] How to create a simple pop up window
How to make a deadman's switch
[Blender] How to make a Blender plugin
How to create a SAS token for Azure IoT Hub
How to delete a Docker container
5 Ways to Create a Python Chatbot
I want to create a Dockerfile for the time being.
[Python] How to create a table from list (basic operation of table creation / change of matrix name)
Overview of how to create a server socket and how to establish a client socket
How to create a heatmap with an arbitrary domain in Python
How to create a label (mask) for segmentation with labelme (semantic segmentation mask)
[C language] How to create, avoid, and make a zombie process
How to create a large amount of test data in MySQL? ??
How to create a CSV dummy file containing Japanese using Faker
How to write a docstring to create a named tuple document with sphinx
How to create a serverless machine learning API with AWS Lambda
I tried to create a linebot (implementation)
How to split and save a DataFrame
How to build a sphinx translation environment
Qiita (1) How to write a code name
How to add a package with PyCharm
[Python] How to make a class iterable
How to draw a graph using Matplotlib
How to create * .spec files for pyinstaller.
[Python] How to convert a 2D list to a 1D list
How to create an NVIDIA Docker environment