Dockerfile is a file that uses as the script with a defined set of instructions to build a docker image. Docker official docs describe Dockerfile as a “text document that contains all the commands a user could call on the command line to assemble an image”.
You should have a properly installed and configured docker installation for this tutorial. Refer to this article to install docker in Ubuntu.
Yes, you are right. As the name suggests this file used to exclude files or directories from adding to the creating image. There are some hidden files such as .git, .vscode, .vimrc which are used in developing an application, But should not be included in the final deployment environment. Therefore you can create a .dockeringore file and include excluding from the docker image build.
Let’s understand Dockerfile instructions before writing a Dockerfile.
This instruction used to set the base image for the new docker image. As defined in the official documentation, a valid Dockerfile must start with a FROM instruction when building a docker image from Dockerfile. It is recommended always to try to choose a lightweight base image such as alpine since they have included only necessary binaries.
You can find various kind of docker images according to your requirement in docker hub. It is a service provided by Docker to share docker images.
You can use this instruction to provide the owner/author of the docker image. However, this is deprecated instruction now. Use LABEL instruction to provide metadata instead of this.
This instruction adds metadata about your image as key-value pairs. You can use
docker inspect command to view these metadata when running as a docker container.
LABEL "Release"="version 1.0"
The base image only has a file system with a limited set of binaries and libraries. Therefore You have to modify the underlying file system as your application requires. RUN instruction is used for this purpose, such as installing packages, execute shell commands, etc. You can use this instruction in 2 different methods.
RUN mkdir /var/www/html/site
Every RUN Instruction creates a new layer on top of the existing image. Therefore as a best practice, always try to combine commands into one RUN instruction to reduce the Docker image size.
You can set the default running command when the container starting from the image. There should be only one CMD instruction in a Dockerfile. But remember this can be overwritten at the docker run command.
CMD ["nginx", "-g", "daemon off;"]
This instruction defines the initial running command when the container gets started. Same as CMD instruction, right? Yes but there are several use cases where you can use both instructions together. CMD can append values to the ENTRYPOINT instruction.
ENTRYPOINT ["echo", "HELLO WORLD"]
This Instruction sets the working directory when creating an image. And also, it will be the default working directory when you access the running docker containers shell.
You can use this instruction to copy files and directories from a source to the creating docker image file system.
COPY /opt/app1 /app/source
Instruct service listening ports when the image runs as a container. It can be a single or several ports. You can define it as a TCP or UDP.
EXPOSE 80 8080
Now you know the most commonly used instructions in Dockerfile. Let’s create a simple web server using Nginx base image.
$ cat Dockerfile FROM nginx LABEL maintainer="GeekDosage" COPY index.html /usr/share/nginx/html EXPOSE 80
Create an index.html file in the same path where Dockerfile is located.
$ cat index.html Hello GEEKS!
Build docker image from Dockerfile
You can create the docker image from the created Dockerfile by running the following command. you can use -t to tag your Docker image accordingly.
$ docker build -t first-docker-image .
Use -f if you wish to specify a different path to the Dockerfile
$ docker build -f /path-to-Dockerfile/ .
OUTPUT Sending build context to Docker daemon 3.072kB Step 1/4 : FROM nginx ---> 0901fa9da894 Step 2/4 : LABEL maintainer="GeekDosage" ---> Using cache ---> 342dd6c7ac45 Step 3/4 : COPY index.html /usr/share/nginx/html ---> a8fac61cb37b Step 4/4 : EXPOSE 80 ---> Running in dd68a449e8e0 Removing intermediate container dd68a449e8e0 ---> fe5d4dcb32b1 Successfully built fe5d4dcb32b1 Successfully tagged first-docker-image:latest
We can list all the available docker images in our computer by runniing follwing command.
$ docker images
You can refer to this tutorial to learn more about docker basic commands.
Let’s run a docker container from the created docker image. Use -d to run the container in the background and -p to bind host 80 port with container 80 port.
$ docker run -d --name first-nginx -p 80:80 first-docker-image:latest
Check the Nginx server using a browser. http://127.0.0.1. You will see it is working fine.
In this tutorial, we have explained how to build a docker image using a Dockerfile. It is better to understand the usability of each Dockerfile instructions. Refer docker official documentation here.