A blueprint for creating a Docker image. Creating from a Dockerfile makes it easier to understand the contents of the container. Create a Dockerfile and create an image with the following command.
$ docker build -t <name> <directory>
The Dockerfile basically consists of three instructions ・ FROM ・ RUN ・ CMD
Start writing from FROM.
FROM ubuntu:latest
RUN touch test
CMD ["ls"]
In this example, create an OS image called ubuntu
in FROM and use the Linux touch
command.
In most cases, the OS is specified for FROM
, or a Docker image created by another person is specified.
The work specified by RUN
is piled up as a layer.
However, if you use RUN a lot, the number of layers will increase and the Docker image will become heavier, so be careful.
CMD
is described at the end of the Dockerfile.
The command specified in CMD is executed when creating a container.
Here the ls
command is executed.
In short, RUN
creates a layer.
CMD
does not create a layer.
In other words, CMD is not reflected in Docker image.
If you want to save it in Dockerimage, describe it in RUN, and the content you want to execute will be the image described in CMD.
There are three commands to create a layer. ・ RUN ・ COPY ・ ADD
In actual business, RUN is mainly used and the main thing is to manage packages. The command to manage the package is as follows.
RUN apt-get update -Get new package list
RUN apt-get install <package> -<package>Install
By connecting these with &&
, the layer can be made lighter in one line.
RUN apt-get update && apt-get install test
This time is over.
Recommended Posts