DEV Community

Cover image for Docker Explained for Backend Developers
Ezeana Micheal
Ezeana Micheal

Posted on

Docker Explained for Backend Developers

As a backend developer(like me), you probably spend most of your time writing application logic, building APIs, connecting databases, handling authentication, and making sure everything works as expected.

But there is another important part of backend development: deployment.

You have built your application, tested it locally, and everything works. Now you need to move it to a server. This is where things can become complicated.

The server may have a different Python version, missing dependencies, a different environment configuration, or other software conflicts.

Docker helps solve many of these problems.

1. What Is Docker?

Docker is a platform that allows you to package an application together with the dependencies and configuration it needs to run. This package is called a container.

For example, imagine you have built a Django application that requires:

  • Python 3.12
  • Django
  • PostgreSQL client libraries
  • Some Python packages
  • Environment variables

Instead of manually installing and configuring everything on every server, you can define the application environment using Docker.

You can then build an image and run it as a container on another machine with Docker installed.

The idea is simple: your application should run in a consistent environment, regardless of where you deploy it.

2. Understanding Images and Containers

These two terms are important when learning Docker.

Docker Image: An image is the packaged blueprint used to create a container. It contains the application code, dependencies, and instructions needed to run it.

Docker Container: A container is a running instance of an image.

Think of an image as a recipe and a container as the actual meal prepared from that recipe. You can create multiple containers from the same image.

3. Creating a Dockerfile

Let's use a simple FastAPI application as an example.

Imagine your project has this structure:

my-api/
├── main.py
├── requirements.txt
└── Dockerfile
Enter fullscreen mode Exit fullscreen mode

Your requirements.txt could contain:

fastapi
uvicorn
Enter fullscreen mode Exit fullscreen mode

Now, create a file called Dockerfile:

FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 8000

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Enter fullscreen mode Exit fullscreen mode

Let's understand each instruction.

  • FROM python:3.12-slim: Specifies the base image. We are using a lightweight Python 3.12 image.
  • WORKDIR /app: Sets /app as the working directory inside the container.
  • COPY requirements.txt .: Copies the requirements file into the container.
  • RUN pip install ...: Installs the Python dependencies.
  • COPY . .: Copies the rest of the application files into the container.
  • EXPOSE 8000: Documents that the application listens on port 8000.
  • CMD [...]: Defines the command that runs when the container starts.

Notice that we use 0.0.0.0 as the host. This allows the application to accept connections from outside the container.

4. Building and Running the Container

Once your Dockerfile is ready, open your terminal in the project directory.

Build the image:

docker build -t my-api .
Enter fullscreen mode Exit fullscreen mode

Here:

  • docker build builds an image.
  • -t my-api gives the image a name.
  • . tells Docker to use the current directory as the build context.

Now run the container:

docker run -d -p 8000:8000 --name my-api-container my-api
Enter fullscreen mode Exit fullscreen mode

What does this command do?

  • docker run creates and starts a container.
  • -d runs it in the background.
  • -p 8000:8000 maps port 8000 on your machine to port 8000 inside the container.
  • --name gives the container a name.
  • my-api specifies the image to run.

You can now access the API through:

http://localhost:8000
Enter fullscreen mode Exit fullscreen mode

5. Why Docker Matters for Backend Deployment

This is where Docker becomes particularly useful to backend developers.

When deploying an application without containers, you may need to manually configure the server, install the right runtime, install dependencies, and manage different versions of software.

With Docker, you can package the application environment and deploy it as a container.

For example, you can:

  • Build your application image locally.
  • Push the image to a container registry such as Docker Hub or Amazon ECR.
  • Pull that image on your production server.
  • Start the application using the same environment you tested.

This makes deployments more predictable and reduces problems caused by differences between development and production environments.

It also makes it easier to roll out new application versions and roll back when something goes wrong.

6. Docker Compose: Running Your Backend and Database Together

Most backend applications need more than one service.

A typical application may have:

  • A Django or FastAPI backend.
  • A PostgreSQL database.
  • A Redis instance for caching or background tasks.

Docker Compose allows you to define and manage multiple containers using one configuration file.

For example:

services:
  backend:
    build: .
    ports:
      - "8000:8000"
    depends_on:
      - db

  db:
    image: postgres:16
    environment:
      POSTGRES_DB: myapp
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: example
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:
Enter fullscreen mode Exit fullscreen mode

You can start both services with:

docker compose up -d
Enter fullscreen mode Exit fullscreen mode

Docker Compose creates and starts the containers based on the configuration.

The database also has a named volume, which helps preserve its data when the container is replaced.

7. Docker and DevOps

Docker is one of the tools that connects backend development with DevOps.

As a backend developer, understanding Docker means you are not limited to writing code and handing it over to someone else for deployment.

You can understand how your application is packaged, how its dependencies are managed, how it communicates with other services, and how it runs in production.

It also prepares you for other DevOps tools and practices, including:

  • CI/CD: Automatically building and testing Docker images whenever code changes.
  • Container registries: Storing and distributing application images.
  • Cloud deployment: Running containers on AWS, Azure, and other platforms.
  • Kubernetes: Managing containers across multiple machines.
  • Infrastructure automation: Provisioning the infrastructure required to run your applications.

Conclusion

Docker is not just about running applications inside containers. It is about making applications easier to package, move, test, and deploy.

For backend developers, this is especially important because building an application is only one part of the job. Understanding how to get that application running reliably in production is equally valuable.

You don't necessarily need to become a DevOps engineer to benefit from Docker. But if you understand Docker, you become a backend developer who understands more of the application lifecycle, from writing the code to getting it running on a server.

Top comments (0)