Docker is a powerful tool for containerization, which is essential for modern DevOps practices. Here’s a breakdown of Docker commands from beginner to advanced levels:
Beginner Commands
docker --version
Check the installed Docker version.docker pull <image>
Download a Docker image from Docker Hub.
Example:docker pull ubuntu
docker run <image>
Create and start a container from an image.
Example:docker run ubuntu
docker ps
List running containers.docker ps -a
List all containers, including stopped ones.docker stop <container_id>
Stop a running container.
Example:docker stop my_container
docker start <container_id>
Start a stopped container.docker rm <container_id>
Remove a stopped container.docker rmi <image>
Remove an image.
Example:docker rmi ubuntu
docker exec -it <container_id> /bin/bash
Access a running container’s shell interactively.docker logs <container_id>
View the logs of a container.
Intermediate Commands
docker build -t <image_name>:<tag> .
Build an image from a Dockerfile.
Example:docker build -t myapp:latest .
docker tag <source_image> <target_image>
Tag an image with a new name.
Example:docker tag myapp:latest myapp:v1.0
docker-compose up
Start up services defined in adocker-compose.yml
file.
You may use-d
for detached mode.docker-compose down
Stop and remove containers defined in adocker-compose.yml
file.docker network ls
List Docker networks.docker network inspect <network_name>
Get detailed information about a network.docker volume ls
List Docker volumes.docker volume inspect <volume_name>
Get detailed information about a volume.docker-compose build
Build or rebuild services defined in adocker-compose.yml
file.docker save -o <file>.tar <image>
Save an image to a tar archive.
Example:docker save -o myapp.tar myapp:latest
docker load -i <file>.tar
Load an image from a tar archive.
Example:docker load -i myapp.tar
Advanced Commands
docker run --rm <image>
Automatically remove the container when it exits.docker volume create <volume_name>
Create a new Docker volume.docker network create <network_name>
Create a new Docker network.docker run --network <network_name> <image>
Run a container connected to a specific network.docker-compose -f <file.yml> up
Specify a differentdocker-compose
file.docker exec -it <container_id> <command>
Run a specific command in a running container.
Example:docker exec -it my_container ls /
docker system prune
Remove unused data, including stopped containers, networks, images, and optionally, volumes.docker info
Display system-wide information about Docker.docker inspect <container_or_image>
Return detailed information in JSON format about containers or images.docker-compose logs
View logs from all services in adocker-compose
setup.docker-compose up --build
Build images before starting containers.docker service create --name <service_name> <image>
Create a new service in Docker Swarm mode.docker stack deploy -c <file>.yml <stack_name>
Deploy a stack to Docker Swarm mode.docker swarm init
Initialize a Docker Swarm cluster.docker swarm join
Join a node to a Docker Swarm cluster.docker secret create <secret_name> <file>
Create a Docker secret from a file for use in Docker Swarm.docker secret inspect <secret_name>
Inspect a Docker secret.docker secret rm <secret_name>
Remove a Docker secret.
These commands cover a broad spectrum of Docker operations and should give you a solid foundation for working with Docker in various scenarios.
0 Comments