This article was published as a part of the Data Science Blogathon.
Introduction on Dockerfile
This article contains about Dockerfile that we are commonly using in DevOps Engineering. DevOps is nothing but it is a set of practices that ensures systems development life cycle and provides continuous delivery with high software quality, that combines software development and IT operations. Most companies use DevOps engineering for their continuous deliveries. In this case, there are depending on Docker. To work with Docker we are using Dockerfile. If you are not clear with Dockerfile, don’t worry this article explains the docker file in simple terms. Also, the examples help you to have the best experience. Let’s move into the article.
What is the Dockerfile?
A Dockerfile is a simple text file with instructions to build an image. If we do not have the docker file, to build the image and run the container word by word we used the command line interface according to our requirements. But this docker file helps us to provide the instruction on what needs to be pulled, what arguments need to be run after building the image, and providing some configurations. If you don’t know about docker you might be confused about this. So I would like to give some basic idea about Docker before going into Dockerfile.
What is Docker?
Docker is an open-source platform for building, deploying, and managing
containerized applications. This ecosystem around creating and running
containers. Docker is software used as a container runtime. Docker ecosystem contains Client, Server, Machine, Images, Hub and Compose.
In this image you can see the flow when we run the command in the Command-line Interface.
This image shows the command-line interface of the previous flow. Let us think, if we need to do multiple works with images and containers what do we do, we use the commands one by one right? Also in the future, we may have multiple images and multiple containers, So to deal with that we also need to run the commands one by one. Do you think it is a good practice? No, we may waste lots of time, and we may miss some commands and configurations for some images. Also, we have no records of the image of what we are doing. To overcome these issues, We are using Dockerfile.
Dockerfile is used to create clean images by removing unnecessary content from an image. Used to execute the same steps to create and recreate the images several times.
Note: Recreating the image with the same file will cause some errors, like if we mention in the code in the Dockerfile to download Python’s latest version, if a user runs the Dockerfile and gets the Python 3.1 version, and another user gets Python 3.2 at another time , in this case, there might be a break in the system because of the dependencies. So the best approach is to have small modifications in the Dockerfile from time to time.
How does it work?
This picture shows the skeleton of the Docker file. We need to create the file Dockerfile or dockerfile so the Docker will understand it is a Docker file. If we need to create a file with a different name (any file), we have to follow the given commands.
Here I am showing the basic commands inside of the docker file. These commands are frequently used in the docker file.
Here I mentioned mostly using commands.
1. FROM— The base image can be Ubuntu, Redis, MySQL, etc.
2. LABEL— Labeling like EMAIL, AUTHOR, etc.
3. RUN— It is used to tell the container what to do after creating the container from the image. Such as, apk add — update-redis, rm -fr
Note: If we need to run the file externally that is inside of the container use cp command in the RUN command RUN cp /usr/share/zoneinfo/Asia/Colombo /etc/localtime && echo “Asia/Colombo” > /etc/ timezone && apk del tzdata
4. COPY— Copy the files from our host system. src: source path dest: container destination path
5. ADD— It is like a COPY command, but it downloads tar, zip, or web file and extracts and copies inside of our image.
6. WORKDIR— It is used to set the directory that we are going to work. If we are adding some files from host local machine and saves in the container, the working directory path is the default directory
7. ENTRYPOINT— The command the executes inside of container. like server running command in httpd container, bash shell in Ubuntu
To get the better understanding about dockerfile the best way is doing much practical. Because of that I provided 4 examples here.
Creating Image & Container using Docker file — Example 1
docker build -t myhttpd:0.1
List the images after the image was created
docker image ls
Creating Image & Container Using Docker file — Example 2
1. Create a file & Config Dockerfile
# Use an existing image as a base FROM alpine
# Download and install dependencies
RUN apk add –update redis
# Tell the image what to do when it start as a container
CMD [ “redis-server” ]
2. Build File — On the docker file directory
docker build .
3. Run Container — docker run
Creating Image & Container using Docker file — Example 3
In my third example, I am creating a wget application using Dockerfile. wget is an application that is used to download files. If we give the URL of the webpage, it will download the full web page file. In Linux, it will be the default application. In Windows, we need to install it. Alpine is a lightweight OS without having to have extra applications or packages. Our ultimate plan is to pull an Alpine image and install the wget application, then give the webpage URL and get the download file, and finally kill the wget application.
Optimizing the Layers
After downloading, we cannot see our file on the local machine, because we downloaded the file inside of the container when the container deletes that file also deleted.
So we can copy the file to our local machine by using different solutions
Copy file to local Machine – Solution 1
Downside: We are doing unnecessary steps
Copy file to local Machine – Solution 2
Downside: It is also like Solution1, but it is a technique for the already exited container.
Copy file to local Machine – Solution 3 (Recommended)
This command looks like ADD or COPY command, but here if we use ADD or COPY command it will take the copy of the host machine files (by using file path) and paste it to the container file (to the destination path), it means the later modifications of the file not available in the host or container. So we use the showing command to, make all modifications available between the host & the container
Note: To get the current directory path of the host machine we can use $(pwd)
Creating Image & Container using Docker file — Example 4
Create and build spring boot application and Create Dockerfile & build (docker build .) file as shown in previous and run the file (docker run ).
ENV ADMIN_SERVER_PORT=8081 USER_SERVICE_URL=http://localhost:8082 AWS_END_POINT=https://s3.us-east-2.amazonaws.com AWS_ACCESS_KEY=AKIA5FGC3KVJ4AFG56NI AWS_SECRET_KEY=/yYBUdR3RvPtae4rghyd3v8hwZBGAGgdQcn8 AWS_BUCKET_NAME=sample.tech
RUN apk add --no-cache tzdata && cp /usr/share/zoneinfo/Asia/Colombo /etc/localtime && echo "Asia/Colombo" > /etc/timezone && apk del tzdata
ADD target/*.jar admin-service.jar
CMD java -jar admin-service.jar
ENTRYPOINT ["java","-jar","admin-service.jar"] EXPOSE 8080
Here I used the environment variable, So I added ENV as like this. Also added dummy values.
Note : Options for connecting multiple containers
1. Use docker CLI’s Networking Features
2. Use Docker Compose
Commands that We Commonly Use
To see images = docker images / docker image ls
To create image = docker create
To remove image = docker rmi
To list running containers =docker ps
To list all containers = docker ps -a
To run container by using image = docker run -it -d
To stop container = docker stop
To kill container = docker kill
To remove container = docker rm
To start container = docker start
To start container & show output = docker start -a
To run docker=docker create + docker start
To remove all stopped containers = docker system prune
To retrieving log outputs = docker logs
To interact with container to cmd prompt = docker exec -it sh
To set tag = docker build -t . (@current folder)
To search = docker search
As I mentioned on the top, by looking at these examples, you might have confidence in creating a Dockerfile and configuring the instruction, which can finally be built and run as the image. Also, now you understand that rather than having an application, we can use a lightweight image to do our work. As previously stated, we can use a Dockerfile to create an application multiple times. If you deep dive into the Dockerfile, you can create a big application by configuring the Dockerfile. Thank you for reading this article. Let’s meet in another article.
The key takeaways from what we learned:
- Understand Docker images and Containers.
- The basic flow of Docker
- Dockerfile Writing Techniques.
- Create a wget application (Sample Application) and run the images and containers using Dockerfile with the arguments.
- Pull and Push the images to the Docker hub.
- Copy and share the Dockerfile Locally.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.