#
What is a Dockerfile ?
This tutorial explains to you what the Dockerfile is. At the end of this article you will understand what Dockerfile is.
If you want to create a Docker container on which you want to deploy something, you need to have a Docker image (if you don't have it already).
But, how we can create a Docker image ? One way is to download it from Docker Hub Repository. Another way is to create the Docker image from a Docker container.
Now we will speak about creating a Docker image using the Dockerfile.
A Dockerfile is a text file with instructions written in a format understood by the Docker daemon during the Image creation.
A Dockerfile can have the following instructions/ commands:
ADD : Copies a file from the host system onto the container.
CMD : The command that runs when the container starts.
ENV : Sets an environment variable in the new container.
EXPOSE : Opens a port for linked containers.
FROM : The base image to use in the build. This is mandatory and must be the first command in the file.
MAINTAINER: An optional value for the maintainer of the script.
ONBUILD : A command that is triggered when the image in the Dockerfile is used as a base for another image.
RUN : Executes a command and save the result as a new layer.
USER : Sets the default user within the container.
VOLUME : Creates a shared volume that can be shared among containers or by the host machine.
WORKDIR : Set the default working directory for the container.
The current working directory where you are located when you issue a docker build command is called the build context,
and the Dockerfile must be somewhere within this build context. By default, it is assumed to be in the current directory,
but you can specify a different location by using the -f
flag.
Now we will take a look at the images we have on Docker installation (using docker images command):
We see that we have only one Docker image ("centos") on my host computer (where I have installed Docker software).
Now I will go to a "context" directory (could be any directory created for this role) and I create the
Dockerfile using vi
(under Linux). Here is the content of my file:
FROM centos
CMD ["echo 'Image START ...'"]
CMD ["date"]
Once the Dockerfile is created, I can use it to create a Docker image:
docker built -t my-tag .
Info
- -t is for the name of the tag
- . is for the current directory
Here you can see how a new image is created:
Here you can see the content of my Dockerfile:
Here you can see the new image created in Docker local repository:
The next step is to create a Docker container from a Docker image.