What is Docker - Get Started from Basics - Docker Tutorial
What is Container, What is Docker on Container - Get Started
How to Install Docker on CentOS 7 / RHEL 7
Docker Images Explained with Examples - Docker Tutorial
Dockerfile Instructions Explained with Examples
Docker ONBUILD Command Explained with Examples
Docker ARG vs ENV Command Differences Explained in Detail
Docker CMD & ENTRYPOINT Differences Explained in Detail
But before writing dockerfile, we must have knowledge on instructions available in dockerfile for the use.
Syntax to write instruction and its arguments within a dockerfile is,
Instructions can be given in lowercase or uppercase letters. But to differentiate from the instructions and arguments, we use uppercase letters.
MAINTAINER Devops Engineer
RUN yum update && yum -y install httpd
RUN mkdir -p /data/myscript
CMD python app.py
We have listed all dockerfile instructions and dockerfile explained with examples below.
Dockerfile Explained with Examples
Lets get started.
FROM instruction used to specify the valid docker image name. So specified Docker Image will be downloaded from docker hub registry if it is not exists locally.
If tag "6" is not specfied, FROM instruction will use the latest tag (version).
This is a mandatory instruction in dockerfile, rest all are optional and those can be used based on the requirement.
MAINTAINER instruction is used to specify about the author who creates this new docker image for the support.
MAINTAINER admin @ learnitguide.net
MAINTAINER Devops Engineer(admin @ learnitguide.net)
LABEL instruction is used to specify metadata informations to an image. A LABEL is a key-value pair.
LABEL "Application_Support"="LearnITGuide.net Group"
EXPOSE instruction is used to inform about the network ports that the container listens on runtime. Docker uses this information to interconnect containers using links and to set up port redirection on docker host system.
EXPOSE 80 443
EXPOSE 80/tcp 8080/udp
ADD instruction is used to copy files, directories and remote URL files to the destination (docker container) within the filesystem of the Docker Images. Add instruction also has two forms - Shell Form and Executable Form.
Shell Form - ADD src dest
ADD /root/testfile /data/
Executable Form - ADD ["src","dest"]
ADD /root/testfile /data/
If the "src" argument is a compressed file (tar, gzip, bzip2, etc) then it will extract at the specified "dest" in the container's filesystem.
COPY instruction is used to copy files, directories and remote URL files to the destination within the filesystem of the Docker Images. COPY instruction also has two forms - Shell Form and Executable Form.
COPY src dest
COPY /root/testfile /data/
COPY /root/testfile /data/
If the "src" argument is a compressed file (tar, gzip, bzip2, etc), then it will copy exactly as a compressed file and will not extract.
RUN instruction is used to executes any commands on top of the current image and this will create a new layer. RUN instruction has two forms - Shell Form and Executable Form.
RUN yum update
RUN systemctl start httpd
CMD instruction is used to set a command to be executed when running a container. There must be only one CMD in a Dockerfile. If more than one CMD is listed, only the last CMD takes effect.
CMD instruction has two forms - Shell Form and Executable Form.
CMD ping google.com
CMD python myapplication.py
ENTRYPOINT instruction is used to configure and run a container as an executable. ENTRYPOINT instruction also has two forms - Shell Form and Executable Form.
ENTRYPOINT ping google.com
ENTRYPOINT python myapplication.py
If user specifies any arguments (commands) at the end of "docker run" command, the specified commands override the default in CMD instruction, But ENTRYPOINT instruction are not overwritten by the docker run command and ENTRYPOINT instruction will run as it is.
So Docker CMD and ENTRYPOINT commands are used for same purpose, but both has some different functionality, refer this link to understand the differences between Docker CMD and ENTRYPOINT Command with examples.
VOLUME instruction is used to create or mount a volume to the docker container from the docker host filesystem.
USER instruction is used to set the username,group name, UID and GID for running subsequent commands. Else root user will be used.
WORKDIR instruction is used to set the working directory.
ENV instruction is used to set environment variables with key and value. Lets say, we want to set variables APP_DIR and app_version with the values /data and 2.0 respectively. These variables will be set during the image build also available after the container launched.
ENV APP_DIR /data/
ENV app_version 2.0
ARG instruction is also used to set environment variables with key and value, but this variables will set only during the image build not on the container.
ARG TMP_NAME mycustom_image
ARG TMP_VER 2.0
So both Docker ENV and ARG commands are used to set environment variables, but there are some differences in functionality, refer this link to understand the differences between Docker ENV and ARG Command with examples.
ONBUILD instruction is used to specify a command that runs when the image in the Dockerfile is used as a base image for another image.
ONBUILD ADD . /app/data
ONBUILD RUN yum install httpd
Refer this link to know more in detail about Docker ONBUILD Command with Examples.
Hope this post "Dockerfile Explained" helped you to understand everything about instructions of dockerfile with examples. Going forward we will play more with docker tool.
If you are interested in learning, Request you to go through the below recommended tutorial.
DevOps Full Course Tutorial for Beginners - DevOps Free Training Online
Docker Full Course Tutorial for Beginners - Docker Free Training Online
Kubernetes Full Course Tutorial for Beginners - Kubernetes Free Training Online
Ansible Full Course Tutorial for Beginners - Ansible Free Training Online
Openstack Full Course Tutorial for Beginners - Openstack Free Training Online
Stay connected with us on social networking sites, Thank you.