This content originally appeared on DEV Community and was authored by Hilary Wambwa
DOCKER
Docker is a tool to develop and run applications in containers.
A container provides an isolated environment for an application and its dependencies(libraries) to ensure it runs across environments (OS systems/machines). It is in itself a running instance of an image.
An image is a blueprint containing everything needed to run an application (code, libraries e.t.c).
Installation
Docker has two installation options.
Docker desktop:
https://docs.docker.com/desktop/setup/install/windows-install/
Docker engine via WSL Linux distribution: https://gist.github.com/dehsilvadeveloper/c3bdf0f4cdcc5c177e2fe9be671820c7
Let’s do it practically:
In this example, we will have three files.
Requirements text file
This file contains libraries needed to run our application (e.g pandas)
Python file(main.py)
Import pandas
print(“Trump”)
Dockerfile
This is a script/instruction that guide how an image is created.
Specify an official Python base image. Instead of installing python, we pull this image from the registry
FROM python:3.13-slim
Set working directory
WORKDIR /app
Copy requirements file and install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
Copy contents in project directory to our app directory.
COPY . .
Run the application
CMD [“python”, “main.py”]
Docker Compose
Docker compose is a tool that makes it easy for data engineers/developers to define and run multi container Docker applications.
Imagine you’re building a complex data pipeline via docker that needs multiple databases, Kafka and a CDC tool e.g. Debezium. Without Docker compose you’d be typing in multiple commands to get each of these containers up and running for every service needed to run your application for instance you’d start with something like Docker run for your databases adding in all those parameters for ports volumes and networks again diving back into the terminal typing another lengthy docker run command with even more parameters to ensure that it runs your app.
This process can be very error prone as there is more potential for typos or mistakes that could send you back to square one.
Enter Docker compose where you define your multi container setup in a single yaml file that outlines which images to user build the ports the volumes and how these containers should talk to one another configurations for services are defined.
In a .yml file which uses yaml syntax a service definition in Docker compose tells Docker how to run a specific container based on an image including configurations like ports volumes environment variables and dependencies on other services. With just a simple docker-compose up command Docker compose gets to work bringing your entire application to life seamlessly. When you’re done you can simply run docker compose down to tear it all down.
Here are some commands for managing containers and images:
docker build -t app: Build an image from a Dockerfile
docker images: List downloaded images.
docker run : Start a container from an image.
docker ps: List running containers.
docker ps -a: List all containers.
docker stop : Stop a container.
docker rm : Remove a container.
docker rmi : Remove an image.
docker-compose up: Start all services defined in compose
docker-compose down: Stop and remove all services
This content originally appeared on DEV Community and was authored by Hilary Wambwa