Building your first Docker Image ft. Python

Hey everyone, Welcome back! In our previous post, we have seen what exactly we mean by Docker, images and containers. In this blog, we would practically say a Hello to Docker and build our first Docker Image. To do this, we need to have Docker installed, Isn’t it? so Let’s start.

Installing Docker

If you’re working on Windows, you need to turn on some features for Docker to work fine. Click on Windows, and search for Turn Windows Features on or off, now enable Hyper-V. This helps in virtualization, a key feature to run Docker. So why do we need it? We will have a complete blog discussing the architecture of Docker. we will discuss the same in that blog.

We UI right! so head over to docker website and download Docker Desktop for your operating system. Double click on the installer, give necessary permissions and accept to the terms of Docker to get started with containerization of your own applications.

Building your First Docker Image

Here we are, all ready to build and run our first docker image. We will write a simple console application in python that takes in a number and prints out the factorial of given number.

Sample Python Application

def factorial(number):
    if number == 0 or number == 1:
        return 1
    else:
        return number*factorial(number-1)
num = int(input("Enter a number to Find Factorial: "))
if num<0:
    print("Can't find Factorial for number less than zero")
else:
    print("Factorial of",num,"is:",factorial(num))

That’s a very simple factorial program if you are familiar with the syntax of Python.

Now, we want to dockerize this program, and take this image along and run wherever we want. So go ahead and create a file named Dockerfile in the same directory.

Before, writing the Dockerfile, let’s know what exactly is it 🤔? A Dockerfile is a list of instructions given to Docker to build an image out of it.

Dockerfile

FROM python:3.8.6-alpine3.12
WORKDIR /app/
COPY . .
CMD python3 /app/factorial.py

Copy the contents of above file into Dockerfile. Now let’s understand it line by line.

  • The FROM statement is used to select the base image. Base image provides us all the necessary environment we need to run our application. In this example, we need python and an alpine Linux to run our command.
  • The WORKDIR statement is used to set the working directory for all the following commands. So any command will likely take place in the same directory.
  • The COPY statement is used to copy contents from host to container i.e. from your file system to the file system of Docker container.
  • The CMD statement is used to run a command when a Docker container starts up. Only one CMD statement is used inside a Docker container.

So yeah, you have known some nice statements in Docker now. Let’s go and build an image using this Dockerfile.

From the root of the directory, where your python program and Dockerfile lie, run the below command.

docker build -t factorial .

Docker build is the command used to build and create images, which are later used to run containers. The ‘t’ flag specifies the name of image, ‘.’ indicates to look for the Dockerfile in the directory where the command is being run.

Now, run the following command to see if the image is created.

docker images

Running the Container

Use the below command to run the container, and to see your factorial container in action. You can export the same image and run it on possibly any environment.

docker run -it factorial

Docker run is a command used to start images as containers. I want you to know why we have used -it flag, and let me know the same in the comments below. (Hint: Try running without that flag 😉)

Here’s the output, when we run Docker run command on our factorial image.

Checkout our GitHub repo, where you can find all the codes related to this blog and you can explore codes of all blogs on Hello World.

That’s all for this blog. Hope this helps in getting started with Docker. Let me know if you find any difficulties in the comment section. We will discuss more on Docker in our next blog. Until then, stay safe. Cheers ✌

2 responses to “Building your first Docker Image ft. Python”

Leave a reply to Multi stage Docker Builds ft. GoLang – Hello World! Cancel reply

Design a site like this with WordPress.com
Get started