How to dockerize an application & build a Docker file image

Saiyam Pathak avatar
By Saiyam Pathak
Field CTO

Description

Learn how to dockerize a Python application with Civo Academy. This lesson provides a step-by-step approach to creating a Docker file, building an image, and running a container.


Welcome to this comprehensive lesson on how to dockerize applications using Civo Infrastructure as a Service. In this lesson, we'll delve into the process of creating a container from an image, using a virtual machine provided by Civo. We'll work with a simple Flask `Hello World` application, which we'll run using the command `python main.py`. This command will spin up a server, and when we load this with the VM IP at port 8080, we'll see the application running.

Creating a Container

Before we create a container, we need an image from which the container can be spawned. In this lesson, we're using a virtual machine provided by Civo's Infrastructure as a Service. We have a simple Flask "Hello World" application, which we'll run using the command `python main.py`. This command spins up a server, and when we load the server at port 8080 using the virtual machine's IP address, we can see the application running. For instance, if we input "Saiyam" in the dialogue box, the response will be "hello Saiyam," indicating that the application is up and running.

Dockerizing the Application

To dockerize our application, we need to create an image and then spawn containers on different environments. This process requires writing a Dockerfile, which is a set of instructions that define what the image should include, such as the application code, its libraries, and dependencies.

In our Dockerfile, we start with the FROM instruction, which specifies the base image we'll use. This base image forms the initial layer of our Docker image. Choosing a verified base image to secure your application from vulnerabilities is a best practice.

Next, we use the COPY instruction to copy the current directory's code into the /app directory of the base image. The WORKDIR command switches the directory to /app, and the RUN instruction executes the command pip install flask within the image.

The EXPOSE command specifies the port that should be exported during the container runtime, in this case, port 8080. The ENTRYPOINT and CMD instructions define the system's default command, which cannot be overridden during runtime.

Note: If you need to execute multiple commands, you should use the "&&" symbol to consolidate them into a single layer.

Building an Image from a Dockerfile

To build an image from a Dockerfile, we use the command docker build -t, followed by the registry name where the image will be pushed. In our case, the Docker Hub account ID is saiyam911/, and we provide the image name as dockerfile-demo:v1.

Once the image is successfully built, we can view it using the docker image ls command. To run a container from the image, we use the command docker run -it -p 8080:8080 saiyam911/dockerfile-demo:v1. This command runs the container in interactive mode and maps port 8080 of the container to port 8080 of the host.

Running the Container in Detached Mode

If you want to run the container in detached mode and continue working with your development environment, you can use the command docker run -d -p 8080:8080 saiyam911/dockerfile-demo:v1. This command runs the container in the background, allowing you to continue with other tasks.

To list all running and exited containers, you can use the docker ps -a command.

Conclusion

In this lesson, we've dockerized a Python application, created an image, and run the application from a container. This process demonstrates the practicality and efficiency of containerization, a key concept in modern software development.

Don't stop now, check out your next lesson