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.
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)
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.)
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.
We hope that this post will help beginners review.