The creation of images interactively is done in multiple steps.
$ docker run -it --name calpine alpine
# apk update && apk add tree
# exit
$ docker container diff calpine
$ docker container commit calpine ialpine
$ docker image history ialpine
To build an image, you need a Dockerfile and a context.
A Dockerfile is a text file that contains instructions to build images.
The context of a build is the collection of files and directories located under certain path or a url. A dot context represents the current directory (folder) and all its subdirectories in the host.
While building an image, the docker engine can refer to any resource in the specified context.
$ docker build [options] <context>
Options:
-f : Specify the name of the Dockerfile
-t : Specify the name of the image
--rm : Remove intermediate containers
The following command builds an image using a Dockerfile located in the root of the context which is the current working directory.
$ docker build .
$ docker image build .
The following command builds an image using the Dockerfile specified after the -f option. The context is the current working directory.
$ docker build -f /Users/user/Dockerfile-dev .
The below command builds an image with a specified name (tag).
$ docker build -t myimage .
A Dockerfile is a set of instructions of the form:
# Comment here
INSTRUCTION arguments
INSTRUCTIONs are reserved words that are by convention capitalized.
Instructions in the Dockerfile are executed from top to bottom of the file.
Sets the base image used to build the the new image.
FROM alpine:latest
The base image is downloaded from the Docker Hub Registry. However, other registries may be specified.
Defines the working directory in the new image for the RUN, CMD, ENTRYPOINT, COPY and ADD instructions. The WORKDIR applies to the instructions that follow it in the Dockerfile.
WORKDIR /App
After, this instruction, all relative paths in the created image are defined relative to the /App directory.
Execute commands in the image. This instruction allows the installation of packages, setup, and configuration of the image.
The RUN instruction has two forms:
The shell form where the command is run in the shell /bin/sh -c <command>.
RUN <command>
RUN apk add tree
The exec form where the command and its parameters are enclosed into double quotes and between [] (It is a JSON array). This the preferable form for RUN.
RUN ["command","arg1", "arg2", ... ]
RUN ["apk", "add", "tree"]
RUN ["echo", "Hello World"]
A Dockerfile may include multiple RUN instructions.
Sets a default command when the container fro this image is run or started without a command at the prompt.
The CMD command is ignored when the container is run with a command as argument of docker run.
Dockerfile can have only one CMD instruction.
The CMD has three forms:
- CMD ["Command", "arg1", "arg2"] (exec form, preferred way)
- CMD ["arg1", "arg2"] (Default parameters to ENTRYPOINT instruction)
- CMD command arg1 arg2 (shell form, to avoid)
The first CMD form is executed when a command is not provided as argument to docker run. If a command is provided as argument, the CMD command is ignored.
This instruction permits the configuration of a container to run as executable.
The ENTRYPOINT has two forms:
- ENTRYPOINT ["command", "arg1", "arg2"] (exec form, preferable way)
- ENTRYPOINT command arg1 arg2 (shell form, to avoid)
The first ENTRYPOINT form overrides the first form of CMD command if both are present in Dockerfile.
Use the ENTRYPOINT when you don't want the user who runs the container to override the command provided in ENTRYPOINT.
Note: ENTRYPOINT command can be overridden by using the --entrypoint option when running the container.
Copies files from a source to a destination inside the built image.
COPY <source> <destination>
The source path is defined as relative to the context set by the build command.
The source can be a file, a pattern, or a directory.
The destination can be a file or a directory. It can use an absolute path or a path relative to the WORKDIR.
COPY style.css /wwwroot
COPY program.js .
The source and destination rules:
- The source must be inside the context. A source such as ../App is not allowed
- If the source is a directory, its entire content is copied
- Non-existent destinations will be created
- Destinations ending with / are directories
- Destinations not ending with / are files
Informs docker engine that the container listens on the port specified.
EXPOSE 80
EXPOSE 80/tcp
EXPOSE 78654/udp
EXPOSE does not publish the port to the host. This has to be done through the option -p when running the container.
FROM centos:latest
RUN yum update -y
RUN yum install httpd -y
WORKDIR /var/www/html/
COPY index.html .
EXPOSE 80
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
Commands to build and run the created image:
$ docker build -t image_apache .
$ docker run -d -p 8080:80 --name container_apache image_apache