DEV Community

vinayak
vinayak

Posted on • Originally published at itsvinayak.hashnode.dev on

Dockerizing Python Application

Docker provides the capability to package and run an application in a loosely isolated environment called a container. It is an open platform for developing, shipping, and running applications.

In this article, we will demonstrate, how to dockerize a python flask app. For this purpose, we will use our blockchain project.

Get the Application

For running Application we need to follow certain step :

  • clone the repository from github
git clone https://github.com/itsvinayak/blockchain.git

Enter fullscreen mode Exit fullscreen mode
  • View the contents of the cloned repository ๐Ÿ—‚

Installing Docker in Ubuntu (linux)

sudo apt-get update -y


sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release -y

Enter fullscreen mode Exit fullscreen mode

Adding gpg " GnuPrivacy Guard" key

sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Enter fullscreen mode Exit fullscreen mode

Adding APT Sources

sudo echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null


sudo apt-get update -y && sudo apt-get install docker-ce docker-ce-cli containerd.io -y

Enter fullscreen mode Exit fullscreen mode

Starting docker

systemctl start docker
systemctl enable docker
docker --version

Enter fullscreen mode Exit fullscreen mode

Writing Docker File

In this project, We will create a docker file with name Dockerfile.

FROM python
LABEL maintainer="vinayak"
LABEL version="1.0"
LABEL description="This is a blockchain implementation in python"
LABEL website="https://github.com/itsvinayak/blockchain"
LABEL email="itssvinayak@gmail.com"
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 8080 
CMD ["python", "server.py"]

Enter fullscreen mode Exit fullscreen mode

Building Docker Image

sudo docker build --tag blockchain .

Enter fullscreen mode Exit fullscreen mode

Here --tag name and optionally a tag in the 'name:tag' format.

Running Docker Image

Listing all images

sudo docker image ls

Enter fullscreen mode Exit fullscreen mode


sudo docker run -d -p 8080:8080 blockchain

Enter fullscreen mode Exit fullscreen mode

Server in now running at proof at port 8080.

Stopping docker image

listing running docker container

sudo docker container ls

Enter fullscreen mode Exit fullscreen mode

Stopping running container

sudo docker container stop a2115d1010b5

Enter fullscreen mode Exit fullscreen mode

Where a2115d1010b5 is running container id

All codes with docker files are present on GitHub

Top comments (0)