How To Create A Docker Image Using Dockerfile
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed docker volume.
https://cloudaffaire.com/how-to-create-and-manage-docker-volume/
In this blog post, we will discuss Dockerfile. We will also create a docker image using Dockerfile.
Prerequisite for this demo:
- One AWS EC2 instance with Linux 2 AMI and internet access.
- Docker
What is Dockerfile?
Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build with a Dockerfile, users can create an automated build that executes several command-line instructions in succession. The Docker daemon runs the instructions in the Dockerfile one-by-one, committing the result of each instruction to a new image if necessary, before finally outputting the ID of your new image. You can refer below documentation for detail usage instruction of Dockerfile.
https://docs.docker.com/engine/reference/builder/
Dockerfile components:
FROM: The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with a FROM instruction.
1 2 3 |
FROM FROM FROM |
RUN: The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.
1 2 |
RUN RUN ["executable", "param1", "param2"] (exec form) |
CMD: The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well. There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.
1 2 3 |
CMD ["executable","param1","param2"] (exec form, this is the preferred form) CMD ["param1","param2"] (as default parameters to ENTRYPOINT) CMD command param1 param2 (shell form) |
LABEL: The LABEL instruction adds metadata to an image. A LABEL is a key-value pair. To include spaces within a LABEL value, use quotes and backslashes as you would in command-line parsing.
1 |
LABEL |
MAINTAINER (deprecated): The MAINTAINER instruction sets the Author field of the generated images. The LABEL instruction is a much more flexible version of this and you should use it instead.
1 |
MAINTAINER |
EXPOSE: The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.
1 |
EXPOSE |
ENV: The ENV instruction sets the environment variable <key> to the value <value>. This value will be in the environment for all subsequent instructions in the build stage and can be replaced inline in many as well.
1 2 |
ENV ENV |
ADD: The ADD instruction copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the image at the path <dest>.
1 2 |
ADD [--chown= ADD [--chown= |
COPY: The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.
1 2 |
COPY [--chown= COPY [--chown= |
ENTRYPOINT: An ENTRYPOINT allows you to configure a container that will run as an executable.
1 2 |
ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred) ENTRYPOINT command param1 param2 (shell form) |
VOLUME: The VOLUME instruction creates a mount point with the specified name and marks it as holding externally mounted volumes from the native host or other containers.
1 |
VOLUME ["/data"] |
USER: The USER instruction sets the user name (or UID) and optionally the user group (or GID) to use when running the image and for any RUN, CMD and ENTRYPOINT instructions that follow it in the Dockerfile.
1 2 |
USER USER |
WORKDIR: The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.
1 |
WORKDIR /path/to/workdir |
ARG: The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the –build-arg <varname>=<value> flag.
1 |
ARG |
ONBUILD: The ONBUILD instruction adds to the image a trigger instruction to be executed at a later time, when the image is used as the base for another build.
1 |
ONBUILD [INSTRUCTION] |
STOPSIGNAL: The STOPSIGNAL instruction sets the system call signal that will be sent to the container to exit.
1 |
STOPSIGNAL signal |
HEALTHCHECK: The HEALTHCHECK instruction tells Docker how to test a container to check that it is still working.
1 2 |
HEALTHCHECK [OPTIONS] CMD command (check container health by running a command inside the container) HEALTHCHECK NONE (disable any healthcheck inherited from the base image) |
SHELL: The SHELL instruction allows the default shell used for the shell form of commands to be overridden. Next, we are going to create a docker volume and attach it to a container.
1 |
SHELL ["executable", "parameters"] |
Next, we are going to build a docker image using Dockerfile.
How To Create A Docker Image Using Dockerfile Demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
##-------------------- ## Docker: Dockerfile ##-------------------- ## Create a docker file vi Dockerfile -------------------------------------------------- FROM ubuntu:16.04 # Install dependencies RUN apt-get update RUN apt-get -y install apache2 # Install apache and write hello world message RUN echo 'Hello World from docker' > /var/www/html/index.html # Configure apache RUN echo '. /etc/apache2/envvars' > /root/run_apache.sh RUN echo 'mkdir -p /var/run/apache2' >> /root/run_apache.sh RUN echo 'mkdir -p /var/lock/apache2' >> /root/run_apache.sh RUN echo '/usr/sbin/apache2 -D FOREGROUND' >> /root/run_apache.sh RUN chmod 755 /root/run_apache.sh EXPOSE 80 CMD /root/run_apache.sh -------------------------------------------------- :wq ## Build an image from Dockerfile docker image build -t myimage . |
Hope you have enjoyed this article. In the next blog post, we will discuss docker compose.
To get more details on docker, please refer below docker documentation