Important Docker Commands
Important Docker Commands
ere's a comprehensive list of commonly used Docker commands, along with their
H
usage:
1.
docker version
: Displays Docker version information.
2.
docker info
: Provides detailed information about theDocker installation.
3.
docker --help
: Lists all available Docker commandsand options.
1. d
ocker pull <image> : Downloads an image from a Dockerregistry (e.g., Docker
Hub).
docker pull nginx
○ Example:
2.
docker images
: Lists all Docker images available onthe system.
docker images
○ Example:
3.
docker rmi <image>
: Deletes a Docker image from thesystem.
docker rmi nginx
○ Example:
4.
docker build -t <name> <path>
: Builds a Docker imagefrom a Dockerfile.
docker build -t myapp:latest .
○ Example:
5. d
ocker tag <source_image> <target_image> : Tags animage with a new
name.
docker tag nginx:latest myrepo/nginx:v1
○ Example:
6.
docker save -o <file> <image>
: Saves an image to atar archive.
docker save -o nginx.tar nginx:latest
○ Example:
7.
docker load -i <file>
: Loads an image from a tar archive.
docker load -i nginx.tar
○ Example:
1.
docker run <image>
: Creates and starts a new containerfrom an image.
docker run nginx
○ Example:
2. d
ocker run -d <image> : Runs a container in detachedmode (in the
background).
docker run -d nginx
○ Example:
3.
docker run -it <image>
: Runs a container interactively with a terminal.
docker run -it ubuntu bash
○ Example:
4.
docker ps
: Lists all running containers.
docker ps
○ Example:
5.
docker ps -a
: Lists all containers, including stoppedones.
docker ps -a
○ Example:
6.
docker stop <container>
: Stops a running container.
docker stop my_container
○ Example:
7.
docker start <container>
: Starts a stopped container.
docker start my_container
○ Example:
8.
docker restart <container>
: Restarts a container.
docker restart my_container
○ Example:
9.
docker rm <container>
: Deletes a stopped container.
docker rm my_container
○ Example:
docker exec -it <container> <command>
10. : Executes acommand in a
running container.
docker exec -it my_container bash
○ Example:
docker logs <container>
11. : Displays logs from a container.
docker logs my_container
○ Example:
docker attach <container>
12. : Attaches to a running container'sconsole.
docker attach my_container
○ Example:
docker kill <container>
13. : Forcefully stops a container.
docker kill my_container
○ Example:
1.
docker network ls
: Lists all Docker networks.
docker network ls
○ Example:
2.
docker network create <name>
: Creates a new Dockernetwork.
docker network create my_network
○ Example:
3.
docker network rm <name>
: Deletes a Docker network.
docker network rm my_network
○ Example:
4. d
ocker network connect <network> <container> : Connectsa container to
a network.
docker network connect my_network my_container
○ Example:
5. d
ocker network disconnect <network> <container> : Disconnectsa
container from a network.
docker network disconnect my_network my_container
○ Example:
1.
docker volume ls
: Lists all Docker volumes.
docker volume ls
○ Example:
2.
docker volume create <name>
: Creates a new Docker volume.
docker volume create my_volume
○ Example:
3.
docker volume rm <name>
: Deletes a Docker volume.
docker volume rm my_volume
○ Example:
4.
docker volume inspect <name>
: Displays detailed informationabout a volume.
docker volume inspect my_volume
○ Example:
5. d
ocker run -v <volume>:/path/in/container <image> : Mounts a volume
into a container.
○ Example:docker run -v my_volume:/data nginx
Dockerfile Commands
1.
docker build -f <Dockerfile>
: Builds an image froma specific Dockerfile.
docker build -f Dockerfile .
○ Example:
1.
docker-compose up docker-compose.ymlfile.
: Starts containers defined in a
docker-compose up
○ Example:
2.
docker-compose down
: Stops and removes containers,networks, and volumes
docker-compose up
created by .
docker-compose down
○ Example:
3.
docker-compose ps
: Lists containers created by DockerCompose.
docker-compose ps
○ Example:
4.
docker-compose logs
: Shows logs for containers managedby Docker Compose.
docker-compose logs
○ Example:
5.
docker-compose build
: Builds or rebuilds servicesdefined in a Compose file.
docker-compose build
○ Example:
1. d
ocker inspect <container_or_image> : Returns low-levelinformation about
a container or image.
docker inspect my_container
○ Example:
2.
docker top <container>
: Displays running processesin a container.
docker top my_container
○ Example:
3.
docker stats
: Displays resource usage statistics ofrunning containers.
docker stats
○ Example:
1.
docker system df
: Displays information about diskusage by Docker.
docker system df
○ Example:
2. d
ocker system prune : Removes unused data (stopped containers, unused
networks, dangling images).
docker system prune
○ Example:
3.
docker image prune
: Removes unused and dangling images.
docker image prune
○ Example:
4.
docker container prune
: Removes all stopped containers.
docker container prune
○ Example:
Other Commands
1. d
ocker commit <container> <image> : Creates a new imagefrom a
container's changes.
docker commit my_container my_image
○ Example:
2.
docker export <container>
: Exports a container's filesystemto a tar archive.
docker export my_container > container.tar
○ Example:
3.
docker import <file>
: Imports a tarball to createan image.
docker import container.tar my_imported_image
○ Example:
hese Docker commands cover the most common activities when working with Docker,
T
ranging from managing containers, images, and volumes to orchestrating multi-container
applications with Docker Compose. Mastering these commands helps in efficiently creating,
deploying, and managing containerized applications.