Skip to content

Docker

Docker is a platform for developing, shipping, and running applications in lightweight, portable containers. Since you're using Ubuntu, Docker works seamlessly with Linux-based systems.

Key Docker Concepts

  • Container: A lightweight, standalone executable package that includes everything needed to run an application.

  • mage: A template used to create containers. It contains the application code and dependencies.

  • Dockerfile: A script that contains instructions to build a Docker image.

  • Volume: A way to persist data outside the container’s lifecycle.
  • Network: Allows communication between containers.
  • Docker Hub: A cloud-based registry to store and share Docker images.

Installing Docker on Ubuntu

Run the following commands:

sudo apt update
sudo apt install -y docker.io
Verify installation:
docker --version
To enable and start Docker:
sudo systemctl enable --now docker
To allow running Docker without sudo:
sudo usermod -aG docker $USER
newgrp docker
Basic Docker Commands

  1. Working with Images

Search for images on Docker Hub:

docker search ubuntu

Download an image:

docker pull ubuntu
List downloaded images:
docker images
Remove an image:
docker rmi image_id
2. Working with Containers

Run a container interactively:

docker run -it ubuntu bash
Run a container in the background:
docker run -d --name my_container ubuntu
List running containers:
docker ps
List all containers (including stopped ones):
docker ps -a
Stop a running container:
docker stop container_id
Remove a container:
docker rm container_id
View container logs:
docker logs container_id
3. Building Custom Images

Create a Dockerfile:

# Use Ubuntu as the base image
FROM ubuntu:latest

# Install necessary packages
RUN apt update && apt install -y python3

# Set the default command
CMD ["python3", "--version"]
Build an image:
docker build -t my_custom_image .
Run a container from the custom image:
docker run my_custom_image
4. Managing Volumes (Persistent Storage)

Create a volume:

docker volume create my_volume
Mount a volume to a container:
docker run -v my_volume:/data ubuntu
5. Networking in Docker

List available networks:

docker network ls
Create a new network:
docker network create my_network
Run a container in a network:
docker run --network my_network --name container1 ubuntu

Docker vs Docker Compose

Both Docker and Docker Compose help in containerization, but they serve different purposes. 1. Docker:

Docker is a tool that allows you to build, run, and manage containers. It operates at the container level (one container at a time). Requires you to manually start, stop, and configure containers.

Example: Running a single container

docker run -d --name my_app -p 5000:5000 my_image
2. Docker Compose:

Docker Compose is a tool for managing multiple containers as a single application. Uses a YAML file (docker-compose.yml) to define and configure services. Simplifies the process of starting and stopping multi-container applications.

Example: Running multiple containers with Docker Compose

Instead of running multiple docker run commands, define everything in a docker-compose.yml file.

docker-compose.yml

version: '3'
services:
  app:
    image: my_app
    ports:
      - "5000:5000"
  db:
    image: postgres
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
Run the entire stack with:
docker-compose up -d
Stop all services with:
docker-compose down

Key Differences

Docker vs Docker Compose

Both Docker and Docker Compose help in containerization, but they serve different purposes. 1. Docker:

Docker is a tool that allows you to build, run, and manage containers.
It operates at the container level (one container at a time).
Requires you to manually start, stop, and configure containers.

Example: Running a single container

docker run -d --name my_app -p 5000:5000 my_image

  1. Docker Compose:

    Docker Compose is a tool for managing multiple containers as a single application. Uses a YAML file (docker-compose.yml) to define and configure services. Simplifies the process of starting and stopping multi-container applications.

Example: Running multiple containers with Docker Compose

Instead of running multiple docker run commands, define everything in a docker-compose.yml file. docker-compose.yml

version: '3'
services:
  app:
    image: my_app
    ports:
      - "5000:5000"
  db:
    image: postgres
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
Run the entire stack with:
docker-compose up -d
Stop all services with:
docker-compose down
Key Differences

Feature Docker Docker Compose
Scope Single container Multi-container applications
Configuration Command-line flags YAML file (docker-compose.yml)
Startup Command docker run docker-compose up
Scaling Manual Built-in scaling (docker-compose up --scale app=3)
Best for Simple apps Complex, multi-container setups

When to Use Which?

Use Docker for single-container applications or quick testing.
Use Docker Compose when dealing with multiple services (e.g., app + database + cache).

Would you like a hands-on example with Docker Compose? You said: Provide me a syntax and rules to be followed to create a docker compose file. ChatGPT said: ChatGPT Docker Compose Syntax & Rules

A docker-compose.yml file is used to define multi-container applications. It follows YAML syntax and consists of structured sections. Basic Syntax

version: '3' # Specify Docker Compose version services: service_name: image: image_name # Use an existing image or build from Dockerfile build: ./path-to-dockerfile # (Optional) Build an image from a Dockerfile container_name: custom_name # (Optional) Name the container restart: always # Restart policy (always, unless-stopped, on-failure, no) ports: - "host_port:container_port" # Expose ports environment: - VAR_NAME=value # Environment variables volumes: - host_path:container_path # Mount volumes networks: - my_network # Connect to a custom network depends_on: - other_service # Define dependencies

networks: my_network: # Define a custom network

volumes: my_volume: # Define a named volume

Rules for Writing a Docker Compose File

Use Correct Indentation
    YAML is indentation-sensitive (use spaces, not tabs).
Define the Version
    Use a supported Docker Compose version (e.g., '3', '3.8').
List Services Under services:
    Each container (service) must be defined under the services: section.
Use Proper Data Types
    Strings: "string_value"
    Integers: 123
    Booleans: true or false
    Lists: - item1
Use ports: Correctly
    "8000:80" → Maps host port 8000 to container port 80.
Use volumes: for Persistent Data
    /data inside a container should map to a host directory or a named volume.
Define networks: for Container Communication
    Containers on the same network can communicate using service names.

Example: Running a Flask App with PostgreSQL docker-compose.yml

version: '3.8'

services: web: build: . container_name: flask_app ports: - "5000:5000" depends_on: - db environment: - DATABASE_URL=postgresql://user:password@db:5432/mydatabase networks: - app_network

db: image: postgres container_name: postgres_db restart: always environment: POSTGRES_USER: user POSTGRES_PASSWORD: password POSTGRES_DB: mydatabase volumes: - pg_data:/var/lib/postgresql/data networks: - app_network

networks: app_network:

volumes: pg_data:

How to Use Docker Compose

Start Services (detached mode):

docker-compose up -d

Stop Services:

docker-compose down

View Running Containers:

docker-compose ps

Check Logs:

docker-compose logs -f

Rebuild the Images (if changes are made):

docker-compose up --build