Dockerizing Python Application
Tags: python, docker, coding, software
Published: 2022-12-27 17:24:37

---
title: Dockerizing Python Application
published: true
date: 2022-12-27 17:21:05 UTC
tags: #python #docker #coding #software
canonical_url: https://itsvinayak.hashnode.dev/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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672161094305/21e81861-09f1-494f-bfc1-3a6c3e855a20.png)

In this article, we will demonstrate, how to dockerize a python flask app. For this purpose, we will use our [blockchain project](https://dev.to/itsvinayak/how-to-build-a-blockchain-in-python-1e2b).

# Get the Application

For running Application we need to follow certain step :

- clone the repository from [github](https://github.com/itsvinayak/blockchain)

```
git clone https://github.com/itsvinayak/blockchain.git

```

- 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

```

### 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

```

### 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

```

### Starting docker

```
systemctl start docker
systemctl enable docker
docker --version

```

# 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"]

```

# Building Docker Image

```
sudo docker build --tag blockchain .

```

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672160567258/1ab55642-13db-4d28-ae3d-e77f8045147e.png)

# Running Docker Image

### Listing all images

```
sudo docker image ls

```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672160764453/c2a7b9c8-def4-44e1-8291-044cb0e392b2.png)

```
sudo docker run -d -p 8080:8080 blockchain

```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672160866357/5033329b-bdf5-4301-8cdb-7197c288d58d.png)

Server in now running at proof at port 8080.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672161176237/ab774381-7743-46a2-90a6-17373cc6988a.png)

# Stopping docker image

### listing running docker container

```
sudo docker container ls

```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672161371918/c53432cf-7fbc-4e29-8863-20bfafbe5af9.png)

### Stopping running container

```
sudo docker container stop a2115d1010b5

```

Where **a2115d1010b5** is running container id

[**All codes with docker files are present on GitHub**](https://github.com/itsvinayak/blockchain)