How to Run an Application In A Docker Container: A Step-by-Step Guide
Publish Date: Sep 12 '24
26 3
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:
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.
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.
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.
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:
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.