A step-by-step guide to create Dockerfile
This blog is about Dockerfile: Understanding, Benefits, and Creation. Whether you’re new to Docker or have been using it for a while, this blog will provide valuable information and tips for working with Dockerfiles.
Below are the topics, that will be covered as part of the blog:
- What is Dockerfile
- Benefits of using a Dockerfile
- How to create a docker file from scratch
- How to run the docker image created from Dockerfile
What is Dockerfile
A Dockerfile is a script that contains instructions for building a customized docker image. Each instruction in a Dockerfile creates a new layer in the image, and the final image is composed of all the layers stacked on top of each other.
It includes instructions for installing dependencies, copying files, setting environment variables, and configuring the container.
Dockerfile uses a simple, easy-to-read syntax that can be created and edited with any text editor. Once a Dockerfile has been created, it can be used to build an image using the docker build command. The resulting image can then be run as a container using the docker run command.
The structure of a Dockerfile is based on a set of simple instructions, such as “FROM”, “RUN”, “COPY”, “ENV”, etc. Each instruction adds a new layer to the image and each layer includes the instructions specified in the previous layer. The final image is a result of all the instructions specified in the Dockerfile.
Benefits of using a Dockerfile
Dockerfiles enable faster and more efficient deployment of applications. Once a Docker image has been built, it can be easily deployed to any environment that supports Docker.
Dockerfiles can be used in automation testing to build and run test environments for different applications and services. Using a Dockerfile, you can create an image of a specific test environment that can be easily and consistently recreated, without needing to manually set up and configure the environment on each test run.
You can easily integrate your Dockerfile with a continuous integration and continuous deployment (CI/CD) pipeline, which enables you to automatically build, test and deploy your application with every code change. This helps to reduce the risk of errors and ensures that your application is always up-to-date.
How to create a docker file from scratch
Pre-Requisite: Docker should be installed on your system
Below are the steps to create Dockerfile from scratch
This is the sample Dockerfile .
Let’s see, step by step what is the use of each command and how can we create it from scratch
1)Open your preferred IDE (such as VS Code) and navigate to your project directory. Create a new file in your project directory (at the root level) and name it Dockerfile (no file extension).
Go to Dockerfile and let’s start building it from scratch
2) FROM
The FROM instruction specifies the base image that the container will be built on top of. This instruction is typically the first one in a Dockerfile and is used to set the base image for the container. The format of the instruction is:
FROM <image>
For example,
FROM node:14-alpine3.16
This instruction tells Docker to use the node:14-alpine3.16 image as the base image for the container. To use a specific version or tag of an image you can use:<version>
or:<tag>
syntax.
The node:14-alpine3.16 is the official Node.js 14 image based on Alpine Linux version 3.16.
3) WORKDIR
In a Dockerfile, the WORKDIR instruction sets the working directory for any command that follows it in the Dockerfile. This means that any commands that are run in the container will be executed relative to the specified directory. Below is the format of the instruction :
WORKDIR <directory>
For example,
WORKDIR /app
This instruction tells Docker to set the working directory of the container to /app . Any subsequent commands in the Dockerfile, such as COPY
, RUN
, or CMD
, will be executed in this directory.
It’s important to note that the WORKDIR instruction creates the directory if it does not already exist. And the subsequent COPY and ADD commands will be executed relative to the WORKDIR specified.
4) COPY
Use the COPY instruction to copy local files from the host machine to the current working directory. For example, to copy a file named package.json from the host machine’s current directory to the image’s /app directory, you would use the following command:
COPY package.json /app/
If you want to copy all the files from the host’s current directory to the container’s current directory, you can use the below command:
COPY . .
It is used to copy all files and directories from the current directory on the host machine (indicated by “.”) to the current directory within the container.
The first “.” refers to the source directory on the host machine, and the second “.” refers to the destination directory within the container.
5) RUN
Use the “RUN” instruction to execute commands that will run during the image build process. The format of instruction is :
RUN <command_name>
For example, to update the package manager and install a specific package, you would use the following command:
RUN npm install
6) CMD
In a Dockerfile, the CMD instruction sets the command that will be executed when a container is run from the image. The format of the instruction is:
CMD ["executable","param1","param2",...]
For example,
CMD ["npm", "start"]
This instruction tells Docker to run the command npm start when a container is created from the image. This command will start the Node.js application that was installed in the container using the npm install command.
Apart from all the instructions mentioned, there are some more instructions like ENV, and EXPOSE which are also used in creating Dockerfile. Below is a detailed description of the same:
7) ENV
Use the ENV instruction to set environment variables inside the image which will be available during build time as well as in a running container. For example, to set the NODE_ENV environment variable to production, you would use the following command:
ENV NODE_ENV production
8) EXPOSE:
Use the EXPOSE command to tell Docker which ports the container will listen on at runtime. For example, if your application listens on port 9000, you would use the following command:
EXPOSE 9000
Sample Docker File would like below:
FROM node:14-alpine3.16
WORKDIR /app
COPY . .
RUN npm install
CMD [ "npm", "start" ]
Run the Dockerfile:
After creating a Dockerfile, now let’s explore the process of building a Dockerfile, pushing it to a registry, and then running it.
Pre-Requisite for push: You need to login ( use command: docker login)
Build Docker Image:
Once you have finished building your Dockerfile, you can build the image by running the below command from the Terminal :
docker build -t <image_name> .
Example: docker build -t testing:v1 .
When you run the above command, it should display the building process just as shown in the screenshot below:
Once, it is successfully built, you would be able to see FINISHED message just like shown below in the screenshot:
It means the docker image is built successfully.
Tip: Make sure docker is running on your system
Verify Docker Image:
Once the image is built, you can check if it got created by using the below command:
docker images
The above command will show you all the docker images.
You can see from the screenshot, the image got created successfully with the same name and the tag which I passed while building the docker image.
Push Docker Image:
Once, the docker image is built and now you can push your docker image. You can tag the image first and then push it or you can push it directly as well. Sometimes, there is an access denied issue even though you are logged in. In that scenario, you can tag the image first and then push it.
Make sure you are logged in ( use command: docker login)
Tag the image using the below command:
docker tag imagename:tag username/imagename:tag
Example:
docker tag testing:v1 username/testing:v1
Push the image:
Once you tag the image, now you can push the image using the below commands:
docker push username/imagename:tag
If you are facing an “access denied” issue even after login, you can try the above approach.
Example:
docker push username/testing:v1
OR you can use the below command to push without providing the username.
Syntax:
docker push imagename:tag
The above approach is the one we use if we don’t have any issues related to “access denied”.
Example:
docker push testing:V1
Run Docker Image:
In the end, you can run the docker image just to be sure that the image built is working fine. Use the below command:
docker run <image_name>:<tag_name>
For Example docker run testing:v1
Conclusion:
Creating a Dockerfile can be useful in the automation project or in development as it makes it easy to manage dependencies and system requirements for our application. Instead of having to worry about ensuring that your application has the correct versions of libraries and dependencies installed on the host system, we can easily specify them in the Dockerfile and they will be included in the container image.
This makes it easy to manage and control the dependencies of your application, and it also makes it easy to test your application on different environments.
Hope you like the article. Thanks for reading!
Happy learning! AB
Thanks, Naveen AutomationLabs for your guidance and support.
Ref: https://docs.docker.com/, https://docs.docker.com/engine/reference/builder/