How to dockerize an application & build a Docker file image
How to dockerize an application & build a Docker file image
Description
With Civo Academy, learn about practical containers by dockerizing an application and building an image from a docker file.
Transcription
Introduction
Now that we know what a container is let us create one. Before creating a container, we should have an image from where a container can be spawned. This particular system that we can see is a virtual machine sent to us based on Civo Infrastructure as a Service. I have a sample flask Hello World application. I will use the command python main.py
to run it, which will spin up a server. And when I load this with the vmip at port 8080, I can see the application running. If I write "Saiyam" in the dialogue box, you will see the response as "hello Saiyam," so the application is up and running.
Dockerizing our application
If we want to dockerize our application, create an image, and then spin containers on different environments, we need to write a docker file. Docker file is a set of instructions to tell what image should be composed of, including the application code, its libraries, and dependencies. So I already have a docker file created.
Let's go through every step of the docker file. So the first instruction is "FROM," which tells which base image we have to use. So all the layering concepts come from here. So this will be the initial layer that we have the base image. Now, as a best practice, make sure the base image that you're choosing for creating the docker file should be verified so that you are secure from the vulnerabilities.
The next command is the COPY instruction. So COPY will copy the current directories code into the /app directory of the image base image that you have taken. Then comes the command WORKDIR, which means work directory. It is switching the directory to /app. RUN instruction will run this particular command, which is pip install flask
. If you are traditionally working on a server, you will write the pip install flask
command.
Similarly, if you want to run that inside image, you will write a run instruction for that particular command. If you have multiple commands to run, you should have used the “&&”
symbol to attach it to a single layer. If you have multiple RUN commands, it will result in multiple layers and thus increasing the size and the complexity of the image.
Then there is the EXPOSE command which specifies the port that has to be exported during the container runtime. So this will be the 8080 port that the container will listen to on the runtime. Now entry point and command are the same, but they are different in that command line instructions can be overridden on runtime, but entry point instructions cannot be overridden. So at the entry point, we have Python. At the CMD level, we have written main.py. Suppose we do not specify anything on the runtime. In that case, it will run Python main.py, which is the file that we have over here because this particular directory is copied inside the image that we have taken of Python Alpine 3.7.
Building an image from a docker file
To build an image from a docker file, the command used will be docker build -t
, which is the tag, then you give the registry name where you actually will be pushing the image. So my Docker hub account ID is saiyam 911/ then I'll provide the image name I have to give, dockerfile-demo:v1. And we provide where the Docker file is, so it will automatically take the docker file from the current directory that is specified. As you can see, it is sending the build context to the Docker daemon. Step one pulls the image Alpine 3.7, copies the current directory to the /app inside the image, then switches to the /app directory. It runs the command pip install flask, and now it has installed flask, then it runs the EXPOSE 8080 command, and it also has created the entry point and the CMD. So the image is successfully built.
Now, if we run the command Docker image ls
, we can view the images on the system. I have created a file image called saiyam911/dockerfile-demo and tag. If we don't specify the tag, the tag goes as latest. So to keep the changes drag, we must have all the tagging in place. So to run a container from the image, the command will be docker run -it
means in the interactive mode, the image name saiyam911/dockerfile-demo:v1 the tag. We will also give the port because this particular application is expecting a port. So the left side will be the host port, the right side will be the container port, and we have already exposed 8080. So the full command will be "docker run -it -p 8080:8080 saiyam911/dockerfile-demo:v1
.
You can see the flask server has already started, and it is running on localhost 8080, which in case we go back again, and you can see the application works again. This time the application is working from a container. Now, if I Ctrl C and I see docker ps, I do not see any container, so the container gets an exit because I was running in interactive mode, and the application is no longer there.
If you want to run the container in the detached mode and continue working with your development environment, we can use the same command and remove -it and put D, which is the detached mode. The whole command will now look like docker run -d -p 8080:8080 saiyam911/dockerfile-demo:v1
. So let's run this. It gives me the container running with the container ID. You can see the complete container ID and the short container ID, and you can see the command that is getting used is Python main.py, which was created five seconds ago. The other command for the newer version is Docker container ls
, which shows the same result. So you can see the container is running. If you want to list all the running and exited containers, run the docker ps -a
command.
In conclusion
So you can see, there were a few containers that we ran, and those got an exit. So it will show you all the containers. Our container is now running. If we go back, we refresh. We see our application is still running. We type in our application, and we can see that our application is up and running. So we have dockerized a python image, a python application image, and we have run the code from a container. So we have run the application from the container, and the container is spawned from the newly created image. So if we use the command docker image ls
, you can see that we have used this particular image to spawn the container. That's it for this lecture. See you in the next one.