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 dockerCheck Docker version
Verify if Docker is installed and display the current version.
docker --versionDocker Images
List local images
Displays all images downloaded on the local system.
docker imagesDownload an image
Downloads an image from Docker Hub.
docker pull image_nameRemove an image
Removes a specific image from the local system.
docker rmi image_nameSearch for an image
Searches for an image on Docker Hub from the terminal.
docker search image_nameBuild 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_nameTag an image
Adds a tag to an image for easier reference.
docker tag image_id image_name:tagContainers
Create and run a container
Runs a container from a specified image.
docker run image_nameRun a container in the background (detached mode)
Runs a container in the background, useful for long-running applications.
docker run -d image_nameAssign a name to a container
Specifies a custom name for the container at creation time.
docker run --name my_container image_nameView running containers
Lists all containers that are currently running.
docker psView all containers
Lists all containers, both running and stopped.
docker ps -aStop a container
Stops the execution of a container.
docker stop container_idStart a stopped container
Starts a previously stopped container.
docker start container_idRemove a container
Removes a container that has been stopped.
docker rm container_idRemove 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/bashRestart a container
docker restart container_idShow logs of a container
Displays the output logs of a container.
docker logs container_idCopy files to/from a container
Copies files from the host system to the container or vice versa.
docker cp host_path container_id:/container_pathStop 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
.envDocker Networking
Create a new network
Creates a custom network.
docker network create network_nameList Docker networks
Displays all networks created in Docker.
docker network lsRun a container on a specific network
docker run -d --name container-name --network network-name image-nameConnect a container to a network
Connects a container to a specific network.
docker network connect network_name container_idDisconnect a container from a network
Disconnects a container from a network.
docker network disconnect network_name container_idRemove a network
Removes a previously created network.
docker network rm network_nameDocker Volumes
Create a volume
Creates a persistent volume that can be shared between containers.
docker volume create volume_nameMount 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_nameList existing volumes
Displays all volumes created on the system.
docker volume lsRemove a volume
Removes a Docker volume.
docker volume rm volume_nameDocker 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: exampleStart services defined in docker-compose.yml
Starts the services defined in the docker-compose.yml file.
docker-compose upStart services in the background (detached mode)
Starts the services in the background.
docker-compose up -dStop all services
Stops the execution of all defined services.
docker-compose downView service logs
Displays the logs of all running services.
docker-compose logsScale services
Increases the number of instances of a service.
docker-compose up --scale service=numberOptimization and Cleanup
Clean resources
Remove stopped containers, unused images, and unused volumes
Automatically cleans up unused resources.
docker system pruneRemove all unused images
Removes all images that are not used by any container.
docker image prune -aRemove unused volumes
Removes all volumes that are not in use.
docker volume pruneMonitoring
View resource usage of a container
Displays the CPU, memory, and other resource usage of a container in real-time.
docker stats container_idView Docker system details
Provides information about the Docker state, such as resources and system usage.
docker infoDocker Swarm
Docker Swarm is a tool for creating and managing Docker clusters.
Initialize a Swarm
docker swarm initCreate a Service
docker service create --name my-service image-nameScale a Service
docker service scale my-service=5Leave a Swarm
docker swarm leave --force