Docker, developers can seamlessly package their applications along with all dependencies into containers, ensuring consistency across different environments. But to truly harness the full potential of Docker, you need to master its extensive array of commands. In this guide, we’ll delve into the core Docker commands, providing practical examples to help you become a Docker virtuoso.
Getting Started with Docker Commands
1. docker run
The docker run
command is your gateway to launching containers from images. Let’s say you want to spin up a container running an Nginx web server:
docker run -d -p 80:80 nginx
This command runs an Nginx container in detached mode (-d
) and maps port 80 on the host to port 80 on the container.
2. docker pull
Use docker pull
to fetch Docker images from a registry. For instance, to pull the latest Ubuntu image:
docker pull ubuntu
3. docker build
With docker build
, you can build a Docker image from a Dockerfile. Suppose you have a Dockerfile in your current directory:
docker build -t myapp .
This command builds an image tagged myapp
from the Dockerfile in the current directory (.
).
Managing Containers
4. docker ps
To list running containers, employ the docker ps
command:
docker ps
This displays a list of running containers along with their IDs, names, and statuses.
5. docker stop
Need to halt a running container? docker stop
is your ally:
docker stop <container_id>
Replace <container_id>
with the ID of the container you wish to stop.
6. docker rm
To remove a stopped container, simply use docker rm
:
docker rm <container_id>
Working with Images
7. docker images
View all locally available Docker images using docker images
:
docker images
This lists all images along with their tags and sizes.
8. docker rmi
To delete a Docker image, employ docker rmi
:
docker rmi <image_id>
Replace <image_id>
with the ID of the image you want to remove.
Network Management
9. docker network ls
List Docker networks with docker network ls
:
docker network ls
This displays a list of Docker networks along with their IDs and names.
10. docker network create
Create a new Docker network using docker network create
:
docker network create my_network
This creates a new bridge network named my_network
.
Volume Management
11. docker volume ls
List Docker volumes with docker volume ls
:
docker volume ls
This provides a list of Docker volumes along with their names and paths.
12. docker volume create
Create a Docker volume using docker volume create
:
docker volume create my_volume
This creates a new Docker volume named my_volume
.
Docker Compose
13. docker-compose up
Docker Compose simplifies the orchestration of multi-container applications. Use docker-compose up
to start services defined in a docker-compose.yml
file:
docker-compose up -d
This starts services defined in the docker-compose.yml
file in detached mode (-d
).
14. docker-compose down
To stop and remove containers created by docker-compose up
, execute docker-compose down
:
docker-compose down
This stops and removes containers, networks, and volumes associated with services defined in the docker-compose.yml
file.
Docker Swarm
15. docker swarm init
Initiate a Docker Swarm with docker swarm init
:
docker swarm init --advertise-addr <manager_ip>
This initializes a Swarm and designates the current node as a manager.
16. docker service create
Deploy services to a Swarm with docker service create
:
docker service create --replicas 3 --name my_service my_image
This creates a service named my_service
with three replicas using the my_image
image.
Docker Security
17. docker scan
Scan Docker images for vulnerabilities using Docker Security Scan:
docker scan my_image
This command scans the my_image
image for vulnerabilities and provides a detailed report.
Wrapping Up
Docker commands are the building blocks of containerization, empowering developers to streamline their workflows and deploy applications with ease. By mastering these commands and incorporating them into your development toolkit, you can unlock the full potential of Docker and accelerate your journey towards building robust, scalable, and portable applications.
Embark on your Docker journey today and revolutionize the way you develop, ship, and run software. Happy containerizing!