Docker, a popular containerization platform, revolutionizes the way we build, ship, and run applications. Understanding its commands is crucial for any developer or system administrator working with containers. This comprehensive guide delves into 25 commonly used Docker commands, providing detailed explanations, code examples, and practical applications.
1. docker pull
Purpose:
Downloads a Docker image from a registry.
Syntax:
docker pull <image name>
Example:
$ docker pull nginx
2. docker images
Purpose:
Lists all Docker images on the system.
Syntax:
docker images
Example:
$ docker images
Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest <none> 2 days ago 13.6MB
ubuntu latest <none> 3 weeks ago 78.8MB
3. docker run
Purpose:
Runs a Docker container from an image.
Syntax:
docker run <options> <image name>
Example:
$ docker run -it --rm nginx
4. docker ps
Purpose:
Lists running Docker containers.
Syntax:
docker ps
Example:
$ docker ps
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d09343c3de3 nginx "nginx -g 'daemon o" 2 minutes ago Up 2 minutes 80/tcp nginx
5. docker stop
Purpose:
Stops a running Docker container.
Syntax:
docker stop <container id>
Example:
$ docker stop 8d09343c3de3
6. docker rm
Purpose:
Removes a stopped Docker container.
Syntax:
docker rm <container id>
Example:
$ docker rm 8d09343c3de3
7. docker build
Purpose:
Builds a Docker image from a Dockerfile.
Syntax:
docker build <path/to/Dockerfile>
Example:
$ docker build -t my-app .
8. docker push
Purpose:
Pushes a Docker image to a registry.
Syntax:
docker push <image name>
Example:
$ docker push my-app:latest
9. docker tag
Purpose:
Tags a Docker image with a new name.
Syntax:
docker tag <image name> <new image name>
Example:
$ docker tag my-app:latest my-app:v1.0
10. docker exec
Purpose:
Executes a command inside a running Docker container.
Syntax:
docker exec <container id> <command>
Example:
$ docker exec 8d09343c3de3 ls -la
11. docker logs
Purpose:
Displays the logs of a running or stopped Docker container.
Syntax:
docker logs <container id>
Example:
$ docker logs 8d09343c3de3
12. docker inspect
Purpose:
Displays detailed information about a Docker image or container.
Syntax:
docker inspect <image/container id>
Example:
$ docker inspect 8d09343c3de3
13. docker attach
Purpose:
Attaches to a running Docker container's stdin, stdout, and stderr.
Syntax:
docker attach <container id>
Example:
$ docker attach 8d09343c3de3
14. docker commit
Purpose:
Commits changes made to a running container to a new Docker image.
Syntax:
docker commit <container id> <new image name>
Example:
$ docker commit 8d09343c3de3 my-app:v1.1
15. docker network create
Purpose:
Creates a new Docker network.
Syntax:
docker network create <network name>
Example:
$ docker network create my-network
16. docker network connect
Purpose:
Connects a container to a Docker network.
Syntax:
docker network connect <network name> <container id>
Example:
$ docker network connect my-network 8d09343c3de3
17. docker volume create
Purpose:
Creates a new Docker volume.
Syntax:
docker volume create <volume name>
Example:
$ docker volume create my-volume
18. docker volume mount
Purpose:
Mounts a Docker volume to a container.
Syntax:
docker run -v <volume name>:<mount path> <image name>
Example:
$ docker run -v my-volume:/data nginx
19. docker info
Purpose:
Displays information about the Docker host.
Syntax:
docker info
Example:
$ docker info
Output:
Containers: 1
Running: 1
Paused: 0
Stopped: 0
Images: 2
Server Version: 20.10.17
Storage Driver: overlay2
20. docker version
Purpose:
Displays the Docker version.
Syntax:
docker version
Example:
$ docker version
Output:
Client:
Version: 20.10.17
API version: 1.41
[...]
21. docker compose up
Purpose:
Brings up a Docker Compose stack.
Syntax:
docker-compose up
Example:
$ docker-compose up
Output:
Creating network "my-network" with the default driver
Creating volume "my-volume" with default driver
Creating myapp_db_1 ... done
Creating myapp_web_1 ... done
22. docker compose down
Purpose:
Tears down a Docker Compose stack.
Syntax:
docker-compose down
Example:
$ docker-compose down
Output:
Stopping myapp_web_1 ... done
Stopping myapp_db_1 ... done
Removing myapp_web_1 ... done
Removing myapp_db_1 ... done
Removing network "my-network"
Removing volume "my-volume"
23. docker system prune
Purpose:
Removes unused Docker objects, such as images, containers, and volumes.
Syntax:
docker system prune
Example:
$ docker system prune
Output:
Deleted Containers:
Untagged: 8b79fb4c20bdd91c36756c6cf5a2b46513c2e177ef86bbe37e3a2c60e182290f
Deleted Images:
Untagged: sha256:8ca8bddbfdcbcb3799c2b20cf191487214d148cc870a992b9f5dc34c89f46f38
Deleted Volumes:
Unused: d2b5293ac684684cea7c971c66deef064113e518b18faa42c41e04f981e392f6
24. docker config
Purpose:
Sets or gets Docker configuration options.
Syntax:
docker config <option> <value>
Example:
$ docker config set registry-mirrors https://my-private-registry.com
25. docker help
Purpose:
Displays help information for a specific Docker command or Docker in general.
Syntax:
docker help <command>
Example:
$ docker help run
Usage:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container.
Conclusion
Mastering these 25 Docker commands will empower you to effectively manage Docker containers and images. By leveraging these commands, you can build, run, inspect, and debug Dockerized applications, streamline your development workflow, and enhance your understanding of containerization. Remember to experiment with these commands and familiarize yourself with their options and capabilities.
Comments
Post a Comment
Oof!