Docker has revolutionized how we deploy applications by encapsulating them in lightweight, portable containers. In this guide, we'll walk through deploying a Python application using Docker, covering everything from creating a Dockerfile to running and cleaning up containers.
1. Understanding the Project Structure
Before diving into Docker, let’s understand the project structure:
- Dockerfile: The recipe for building your Docker image.
- requirements.txt: Lists the Python dependencies.
- my_docker_build_and_run.sh: A script to build and run the Docker container.
- my_docker_clean.sh: A script to stop and remove Docker containers and images.
- my_docker_terminal.sh: A script to access the running container's terminal.
2. Writing the Dockerfile
The Dockerfile is the heart of your Docker setup. Here's a breakdown of the provided Dockerfile:
FROM ubuntu:24.04
ARG APP_VERSION="1.0"
ARG APP_NAME="my-application-1"
# Install dependencies and python3-venv for virtual environment
RUN apt-get update && apt-get install -y \
python3 \
python3-pip \
python3-venv
# Create a working directory
WORKDIR /app
# Copy dependency file
COPY src/requirements.txt requirements.txt
# Create a virtual environment and install dependencies
RUN python3 -m venv venv && \
venv/bin/pip install --upgrade pip && \
venv/bin/pip install -r requirements.txt
# Copy remaining files
COPY src/my-application-1.py ./
# Create the output and logs directories
RUN mkdir -p /app/output /app/logs
# Set the entry point with output redirection
ENTRYPOINT ["sh", "-c", "venv/bin/python my-application-1.py > /app/logs/logfile.trc 2>&1"]
Key Points:
- Base Image: FROM ubuntu:24.04 sets the base image to Ubuntu 24.04.
- Dependencies: Installs Python and necessary packages.
- Virtual Environment: Creates and uses a Python virtual environment to install dependencies.
- Working Directory: Sets /app as the working directory.
- Copy Files: Copies application files into the Docker image.
- Entrypoint: Specifies the command to run the application and redirect logs.
3. Building the Docker Image
Create a script named my_docker_build_and_run.sh to build and run the Docker container:
#!/bin/bash
APP_VERSION="1.0"
APP_NAME="my-application-1"
docker build -t ${APP_NAME}:${APP_VERSION} .
docker run -d \
--name ${APP_NAME}-${APP_VERSION} \
-v /home/dmitry/my_docker/my-application-1/output:/app/output \
-v /home/dmitry/my_docker/my-application-1/logs:/app/logs \
${APP_NAME}:${APP_VERSION}
docker image ls ${APP_NAME}
docker ps --filter "name=${APP_NAME}_${APP_VERSION}"
Explanation:
- Build Image: docker build -t ${APP_NAME}:${APP_VERSION} . builds the Docker image.
- Run Container: docker run -d starts a container in detached mode.
- Volume Mounts: -v mounts local directories to the container, allowing persistent storage.
- Check Image and Container: Lists images and running containers.
4. Cleaning Up Docker Resources
Use my_docker_clean.sh to stop and remove Docker containers and images:
#!/bin/bash
APP_VERSION="1.0"
APP_NAME="my-application-1"
CONTAINER_ID=$(docker ps -a --filter "ancestor=${APP_NAME}:${APP_VERSION}" --format "{{.ID}}")
if [ -n "$CONTAINER_ID" ]; then
echo "Stopping container $CONTAINER_ID..."
docker stop "$CONTAINER_ID"
echo "Removing container $CONTAINER_ID..."
docker rm "$CONTAINER_ID"
fi
# Remove the image
IMAGE_ID=$(docker images --format '{{.Repository}}:{{.Tag}}' | grep ${APP_NAME}:${APP_VERSION})
if [ -n "$IMAGE_ID" ]; then
echo "Removing image $IMAGE_ID..."
docker rmi "$IMAGE_ID"
fi
Key Points:
- Stop Container: Stops the running container.
- Remove Container: Deletes the stopped container.
- Remove Image: Deletes the Docker image to free up space.
5. Accessing the Running Container
To access the container’s terminal, use my_docker_terminal.sh:
APP_VERSION="1.0"
APP_NAME="my-application-1"
docker exec -it ${APP_NAME}-${APP_VERSION} /bin/bash
Explanation:
docker exec -it: Opens an interactive terminal session inside the running container.
Conclusion
Deploying an application using Docker simplifies many aspects of software deployment, such as dependency management and environment consistency. By following these steps, you can effectively build, run, and manage your Dockerized applications.
Whether you’re deploying a simple Python script or a complex application, Docker provides a robust and scalable solution.
Top comments (3)
Docker is awesome! 👏
This post is a great introduction, in case that anyone wants to learn more you can checkout this free ebook here:
bobbyiliev / introduction-to-docker-ebook
Free Introduction to Docker eBook
💡 Introduction to Docker
This is an open-source introduction to Docker guide that will help you learn the basics of Docker and how to start using containers for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you will most likely have to use Docker at some point in your career.
The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Docker.
🚀 Download
To download a copy of the ebook use one of the following links:
Dark mode
Light mode
📘 Chapters
I think the title is misleading: deploy in my mind is to put it somewhere in the cloud. What it should be is how to run your application in a docker container. For that, it is a perfect guide.
Thanks! Docker's great, yes, it is more difficult to configure, but you get a container that always has the necessary dependencies and it is easier to scale horizontally as the load increases.