DEV Community

Randika Madhushan Perera
Randika Madhushan Perera

Posted on

Docker Hands-On Part 06

06. Docker Container Networking


The process of containerizing applications represents a significant leap forward in how we deploy and manage software. The Flask framework, popular for its simplicity and flexibility for web application development, is an excellent candidate for Dockerization. This transformative journey from a centralized server setup to a containerized deployment offers numerous advantages, including streamlined deployment processes, improved scalability, and enhanced isolation for development and production environments.

The Journey to Dockerize a Flask Application

The task at hand involves moving a Flask application, aptly named "Notes App," into a Docker container. This application, designed for employees to store notes on current projects, will benefit from the portability and efficiency that containers provide.

Step-by-Step Guide to Containerization

01. Initial Configuration:

Begin by writing the necessary configuration code, including a Dockerfile and .dockerignore file, to build a clean and efficient Docker image for the Notes App.

Step 01: Create Dokcerignore file

$ vim .dockerignore
Enter fullscreen mode Exit fullscreen mode

add below

# Exclude files
Enter fullscreen mode Exit fullscreen mode

Create Dockerfile and add below

# version 01
FROM python:3
ENV PYBASE /pybase
#install pipenv
RUN pip install pipenv

COPY Pipfile .
RUN pipenv lock
RUN PIP_USER=1 PIP_IGNORE_INSTALLED=1 pipenv install -d --system --ignore-pipfile

COPY . /app/notes
WORKDIR /app/notes
CMD ["flask", "run", "--port=80", "--host="]
Enter fullscreen mode Exit fullscreen mode

Building the First Image:

With the Dockerfile in place, build the initial image version of the Notes App. This step includes setting up the database within a container derived from the first image, ensuring that the application's data layer is ready for use.

Step 02: Build the image and container

$ docker build -t notesapp:0.1 .
Enter fullscreen mode Exit fullscreen mode

Step 03: Create and run a container for postgres:12.1-alpine

$ docker pull postgres:12.1-alpine

$ docker run -d --name notesdb postgres:12.1-alpine
Enter fullscreen mode Exit fullscreen mode

Step 04: Create a network and attach it to the bridge network

$ docker network create -d bridge notes
Enter fullscreen mode Exit fullscreen mode

Step 05: List the docker networks

$ docker network ls

df7f5f15c7ab   bridge    bridge    local
7405f25b96f6   host      host      local
ee4c71834f71   none      null      local
14219a8cf079   notes     bridge    local
Enter fullscreen mode Exit fullscreen mode

Step 06: Run a container using the notesapp image and mount the migrations directory

$ docker run --rm -it --network notes -v /home/ec2-user/notes/migrations:/app/notes/migrations notesapp:0.1 bash
Enter fullscreen mode Exit fullscreen mode

Step 07:

# flask db migrate
Enter fullscreen mode Exit fullscreen mode

Running the Application:

Deploy the Notes App container to verify its functionality. During this phase, monitoring the logs for any issues, such as the application running in debug mode, is crucial. Debug mode is not suitable for production due to potential security and performance implications.
Enhancing for Production:

Address the debug mode issue by switching the Flask application to run on a more robust WSGI (Web Server Gateway Interface) server, such as Gunicorn. This change involves updating the environment variables, modifying the pip file to include Gunicorn, and adjusting the Dockerfile to execute Gunicorn instead of Flask directly.

Top comments (0)