This repository contains notes and practical examples for learning the fundamentals of Docker. It covers basic commands for working with images and containers, essential instructions for building images with a Dockerfile, and the core concepts of Docker volumes, networks, and Compose.
The examples are intended for local practice with the Dockerfile, the docker-compose.yml file, and the web content available in the html/ directory.
- Docker and basic commands
- Dockerfile instructions
- Docker volumes
- Docker networks
- Docker Compose
| Command | Description | Example |
|---|---|---|
docker --version |
Check Docker version | docker --version |
docker pull <image_name> |
Download an image | docker pull python:3.8 |
docker images |
List downloaded images | docker images |
docker run <image_name> |
Run a container | docker run -d -p 8080:80 nginx |
docker ps |
List running containers | docker ps |
docker ps -a |
List all containers (running and stopped) | docker ps -a |
docker stop <container_id> |
Stop a running container | docker stop 1a2b3c4d5e6f |
docker start <container_id> |
Start a stopped container | docker start 1a2b3c4d5e6f |
docker rm <container_id> |
Remove a stopped container | docker rm 1a2b3c4d5e6f |
docker rmi <image_name> |
Remove an image | docker rmi python:3.8 |
docker logs <container_id> |
View logs of a container | docker logs 1a2b3c4d5e6f |
docker exec -it <container_id> <command> |
Execute a command inside a running container | docker exec -it 1a2b3c4d5e6f bash |
docker build -t <image_name> <path> |
Build an image from a Dockerfile | docker build -t myapp:latest . |
docker inspect <container_id> |
Get detailed information about a container | docker inspect 1a2b3c4d5e6f |
A Dockerfile is a plain text file without an extension that contains a list of step-by-step instructions for automatically building a Docker image.
-
FROM: Uses an existing image
-
WORKDIR: Defines the working directory inside the container
-
COPY: Copies selected files from the host system into the container
-
RUN: Executes a command
-
EXPOSE: Exposes a port to the outside world
-
CMD: The command that runs when the container starts
In Docker, a volume is a persistent storage mechanism that allows you to store data outside of the container's filesystem. Volumes are used to persist data generated by and used by Docker containers. They are managed by Docker and can be shared between multiple containers, making them ideal for scenarios where you need to maintain state or share data.
Volumes persist data generated by containers and can be shared between multiple containers. They are managed by Docker and can be used to persist data even after the container is stopped or removed. Volumes are stored in a part of the host filesystem managed by Docker (/var/lib/docker/volumes/ on Linux).
| Command | Description | Example |
|---|---|---|
docker volume create <volume_name> |
Create a new volume | docker volume create myvolume |
docker run -d -v <volume_name>:/path/in/container <image_name> |
Run a container with a volume attached | docker run -d -v myvolume:/data nginx |
docker volume ls |
List all volumes | docker volume ls |
docker volume inspect <volume_name> |
Get detailed information about a volume | docker volume inspect myvolume |
docker volume rm <volume_name> |
Remove a volume | docker volume rm myvolume |
In Docker, a network is a virtual layer that allows containers to communicate with each other and with the outside world. Docker provides several built-in network drivers, such as bridge, host, overlay, and macvlan, each serving different use cases. Networks enable containers to discover and interact with each other using container names or IP addresses.
Networks allow containers to communicate with each other securely. By default, containers on the same network can communicate with each other using container names.
| Network Type | Description | Use Case |
|---|---|---|
| bridge | Default network driver for standalone containers. It creates a private internal network on the host | Suitable for single-host container communication |
| host | Removes network isolation between the container and the Docker host, allowing the container to use the host's network directly | Useful for performance-sensitive applications that require direct access to the host network |
| overlay | Enables communication between containers across multiple Docker hosts, typically used in Docker Swarm or Kubernetes | Ideal for multi-host container communication in a cluster environment |
| macvlan | Assigns a MAC address to a container, allowing it to appear as a physical device on the network | Useful for scenarios where containers need to be treated as physical devices on the network |
| Command | Description | Example |
|---|---|---|
docker network create <network_name> |
Create a new network | docker network create mynetwork |
docker run --network <network_name> <image_name> |
Run a container attached to a specific network | docker run --network mynetwork nginx |
docker network ls |
List all networks | docker network ls |
docker network inspect <network_name> |
Get detailed information about a network | docker network inspect mynetwork |
docker network connect <network_name> <container_name> |
Connect a container to a network | docker network connect mynetwork mycontainer |
docker network disconnect <network_name> <container_name> |
Disconnect a container from a network | docker network disconnect mynetwork mycontainer |
Port mapping allows you to expose container ports to the host machine, enabling external access to services running inside containers. This is typically done using the -p or --publish flag when running a container.
docker run -d -p <host_port>:<container_port> <image_name>Docker Compose is a tool that allows you to define and manage multi-container Docker applications. It uses a YAML file to configure the application's services, networks, and volumes, making it easier to set up and run complex applications with multiple interconnected containers. With Docker Compose, you can start all the services defined in the configuration file with a single command, simplifying the development and deployment process.
Docker Compose allows you to define multi-container applications in a single file, making it easier to manage and deploy complex applications. It provides a simple way to start, stop, and scale services, as well as manage networks and volumes.
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: mydb
MYSQL_USER: user
MYSQL_PASSWORD: password| Command | Description |
|---|---|
docker compose up |
Starts the services defined in the docker-compose.yml file. |
docker compose down |
Stops and removes the containers, networks, and volumes created by up. |
docker compose up -d |
Starts the services in detached mode, running them in the background. |
docker compose ps |
Displays the status of the services defined in the docker-compose.yml file. |
docker compose logs |
View logs of services |
docker compose build |
Builds or rebuilds the services defined in the docker-compose.yml file. |
docker compose restart |
Restarts the services defined in the docker-compose.yml file. |