Docker is a platform that allows you to create, deploy, and run applications inside containers, providing an isolated and consistent environment.
Docker Installation
Install Docker on Windows
Download Docker Desktop from the official site.
Install Docker on Linux
For Ubuntu, you can use the following commands:
sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
Check Docker version
Verify if Docker is installed and display the current version.
docker --version
Docker Images
List local images
Displays all images downloaded on the local system.
docker images
Download an image
Downloads an image from Docker Hub.
docker pull image_name
Remove an image
Removes a specific image from the local system.
docker rmi image_name
Search for an image
Searches for an image on Docker Hub from the terminal.
docker search image_name
Build an image from a Dockerfile
Creates a new image from a Dockerfile
.
docker build -t image_name:tag .
View the history of an image
Displays the layers of an image and how it was built.
docker history image_name
Tag an image
Adds a tag to an image for easier reference.
docker tag image_id image_name:tag
Containers
Create and run a container
Runs a container from a specified image.
docker run image_name
Run a container in the background (detached mode)
Runs a container in the background, useful for long-running applications.
docker run -d image_name
Assign a name to a container
Specifies a custom name for the container at creation time.
docker run --name my_container image_name
View running containers
Lists all containers that are currently running.
docker ps
View all containers
Lists all containers, both running and stopped.
docker ps -a
Stop a container
Stops the execution of a container.
docker stop container_id
Start a stopped container
Starts a previously stopped container.
docker start container_id
Remove a container
Removes a container that has been stopped.
docker rm container_id
Remove all stopped containers
Removes all containers that have been stopped.
docker rm $(docker ps -a -q)
Interacting with Containers
Access a running container
Opens an interactive terminal inside a running container.
docker exec -it container_id /bin/bash
Restart a container
docker restart container_id
Show logs of a container
Displays the output logs of a container.
docker logs container_id
Copy files to/from a container
Copies files from the host system to the container or vice versa.
docker cp host_path container_id:/container_path
Stop all running containers
Stops all active containers.
docker stop $(docker ps -q)
Dockerfile
A Dockerfile
is a text file that contains all the instructions to build a Docker image.
Create a Dockerfile
A Dockerfile
defines the runtime environment of an application inside a container.
FROM
: Defines the base image.RUN
: Executes commands during the image build.COPY
: Copies files from the host to the container.WORKDIR
: Sets the working directory.CMD
: Specifies the command to be executed when the container starts.
# Use a base image
FROM ubuntu:20.04
# Install dependencies
RUN apt-get update && apt-get install -y \
python3 \
python3-pip
# Copy files to the container
COPY . /app
# Set the working directory
WORKDIR /app
# Default command
CMD ["python3", "app.py"]
Build an image from a Dockerfile
Creates an image from a Dockerfile.
docker build -t image_name .
Ignore files with .dockerignore
Defines files that should not be copied into the Docker image.
node_modules
.git
.env
Docker Networking
Create a new network
Creates a custom network.
docker network create network_name
List Docker networks
Displays all networks created in Docker.
docker network ls
Run a container on a specific network
docker run -d --name container-name --network network-name image-name
Connect a container to a network
Connects a container to a specific network.
docker network connect network_name container_id
Disconnect a container from a network
Disconnects a container from a network.
docker network disconnect network_name container_id
Remove a network
Removes a previously created network.
docker network rm network_name
Docker Volumes
Create a volume
Creates a persistent volume that can be shared between containers.
docker volume create volume_name
Mount a volume in a container
Mounts a volume in a container to store persistent data.
docker run -d --name my_container -v volume_name:/path_inside_container image_name
List existing volumes
Displays all volumes created on the system.
docker volume ls
Remove a volume
Removes a Docker volume.
docker volume rm volume_name
Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications.
Basic example of docker-compose.yml
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: postgres
environment:
POSTGRES_PASSWORD: example
Start services defined in docker-compose.yml
Starts the services defined in the docker-compose.yml
file.
docker-compose up
Start services in the background (detached mode)
Starts the services in the background.
docker-compose up -d
Stop all services
Stops the execution of all defined services.
docker-compose down
View service logs
Displays the logs of all running services.
docker-compose logs
Scale services
Increases the number of instances of a service.
docker-compose up --scale service=number
Optimization and Cleanup
Clean resources
Remove stopped containers, unused images, and unused volumes
Automatically cleans up unused resources.
docker system prune
Remove all unused images
Removes all images that are not used by any container.
docker image prune -a
Remove unused volumes
Removes all volumes that are not in use.
docker volume prune
Monitoring
View resource usage of a container
Displays the CPU, memory, and other resource usage of a container in real-time.
docker stats container_id
View Docker system details
Provides information about the Docker state, such as resources and system usage.
docker info
Docker Swarm
Docker Swarm is a tool for creating and managing Docker clusters.
Initialize a Swarm
docker swarm init
Create a Service
docker service create --name my-service image-name
Scale a Service
docker service scale my-service=5
Leave a Swarm
docker swarm leave --force