DEV Community

Joy Akinyi
Joy Akinyi

Posted on

Getting Started with Docker and Docker Compose: A Beginner’s Guide

Have you ever heard the phrase “But it works on my machine”?
This is one of the most common problems developers face when moving applications from their local computer to a server or sharing projects with teammates.

That’s where Docker comes in. Docker allows you to package your application with all its dependencies into a container so that it can run consistently on any environment say your laptop, a server, or even the cloud.

In this guide, we’ll cover the basics of Docker and introduce Docker Compose, a tool that helps you run multi-container applications with ease.

What is Docker?
Docker is an open source platform that allows you to run applications in isolated environments through containerization. Unlike Virtual Environments that emulate entire operating systems,docker containers share the host OS but isolate applications. They are also lightweight and fast.

Installing Docker:

You can approach this by either:

  1. Installing Docker Desktop

Docker Desktop provides an easy-to-use application with a GUI and the Docker CLI preinstalled.It also integrates well with WSL 2 on Windows.

👉 Download here

Once installed, you’ll be able to run Docker commands directly from your terminal (PowerShell, WSL, or macOS terminal).

2.Installing the Docker CLI on Linux:
If you’re running Linux, you can install the Docker CLI directly.

For example, on WSL 2 with Ubuntu 22.04,you can follow this steps:
Installing Docker on WSL 2 with Ubuntu 22.04

After installation,you can verify if Docker is working by running:
docker version

If it's working, you should see the following output:

Client: Docker Engine - Community
 Version:           27.0.2
 API version:       1.46
 Go version:        go1.21.5
 Git commit:        3ab9da9
 Built:             Tue Jul 18 17:45:00 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.0.2
  API version:      1.46 (minimum version 1.12)
  Go version:       go1.21.5
  Git commit:       3ab9da9
  Built:            Tue Jul 18 17:45:00 2024
  OS/Arch:          linux/amd64
  Experimental:     false 
Enter fullscreen mode Exit fullscreen mode

Basic Docker Concepts:

  • Image → A blueprint for your application.

  • Container → A running instance of an image.

  • Dockerfile → A recipe to build an image.

  • Docker Hub → Public repository of prebuilt images.

Now,let's talk about building and running your first container;
You can run an already existing image by running
docker run hello-world
In this case,docker pulls the image from Docker Hub, creates a container, runs it, then exits.

For listing and stopping containers,you can run:

docker ps # running containers
docker ps -a # all containers (even stopped ones)
docker stop <container_id>
docker rm <container_id>

Running your first Dockerfile:

A Docker file as mentioned before is like a recipe for building images
An example of one is:

## Use official Python image
FROM python:3.13-slim

## Set working directory
WORKDIR /app

## Copies the .txt file to the set working directory
COPY requirements.txt .

## Runs the requirements file
RUN pip install -r requirements.txt

## Copy local files into the container
COPY . .

## Run main
CMD ["python","main.py"]
Enter fullscreen mode Exit fullscreen mode

Now that we have a docker file,we can build an image say image1 using:
docker build -t image1 .

Next is to run the container by running:
docker run image1
This will run a container based on the image you just created

So far, we’ve learned how to run one container at a time. But in real-world projects, applications often rely on multiple containers running together — for example, a web server and a database. Managing these individually can get complicated, and that’s where Docker Compose comes in

Getting Started with Docker Compose

Docker Compose is a tool that lets you define and run multi-container applications using a single configuration file called docker-compose.yml.

Instead of starting containers one by one, you define all your services in YAML and start them together with one command. A sample docker-compose.yml file is:

version: "3.8"

services:
  web:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - db

  db:
    image: postgres:15
    restart: always
    environment:
      POSTGRES_USER: example
      POSTGRES_PASSWORD: example
      POSTGRES_DB: testdb
    ports:
      - "5432:5432"


Enter fullscreen mode Exit fullscreen mode

Running with compose:
To build and start all services, run
docker-compose up
To stop everything,run
docker-compose down

In conclusion,With Docker Compose, you don’t have to remember long docker run commands as everything is in one file and also it super easy to share projects.

Top comments (1)

Collapse
 
levantuy profile image
levantuy

I started using Docker a while back and the biggest win for me was not having to mess around with different local setups anymore. Once you get your first container running, it kinda clicks. My tip: play around with a simple app first (like a Node or Python API) before diving into Docker Compose. It makes the jump to multi-container stuff way less overwhelming.