Common Docker Commands Explained
Docker has become an essential tool in the world of software development and deployment, offering a streamlined way to manage applications and their dependencies. Whether you’re a beginner or an experienced user, understanding the most common Docker commands is crucial for efficient container management. This post will explore the key Docker commands and their functionalities.
Basic Docker Commands
1. docker run
Purpose: Create and start a container from an image.
Usage:
1
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Example:
1
docker run -d -p 80:80 nginx
This command runs an Nginx container in detached mode (-d
) and maps port 80 of the host to port 80 of the container.
2. docker pull
Purpose: Download an image from a Docker registry.
Usage:
1
docker pull IMAGE[:TAG]
Example:
1
docker pull ubuntu:latest
This command pulls the latest Ubuntu image from Docker Hub.
3. docker ps
Purpose: List running containers.
Usage:
1
docker ps [OPTIONS]
Example:
1
docker ps
This command lists all running containers.
4. docker stop
Purpose: Stop a running container.
Usage:
1
docker stop CONTAINER
Example:
1
docker stop my-container
This command stops a container named my-container
.
5. docker rm
Purpose: Remove a container.
Usage:
1
docker rm [OPTIONS] CONTAINER [CONTAINER...]
Example:
1
docker rm my-container
This command removes a container named my-container
.
6. docker rmi
Purpose: Remove an image.
Usage:
1
docker rmi IMAGE [IMAGE...]
Example:
1
docker rmi ubuntu:latest
This command removes the ubuntu:latest
image.
7. docker exec
Purpose: Run a command in a running container.
Usage:
1
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Example:
1
docker exec -it my-container /bin/bash
This command opens an interactive terminal in the my-container
.
Intermediate Docker Commands
1. docker build
Purpose: Build an image from a Dockerfile.
Usage:
1
docker build [OPTIONS] PATH | URL | -
Example:
1
docker build -t my-image:latest .
This command builds an image named my-image
with the latest
tag from the current directory.
2. docker logs
Purpose: Fetch the logs of a container.
Usage:
1
docker logs [OPTIONS] CONTAINER
Example:
1
docker logs my-container
This command fetches the logs from my-container
.
3. docker network ls
Purpose: List all networks.
Usage:
1
docker network ls
Example:
1
docker network ls
This command lists all Docker networks.
4. docker volume ls
Purpose: List all volumes.
Usage:
1
docker volume ls
Example:
1
docker volume ls
This command lists all Docker volumes.
5. docker inspect
Purpose: Return low-level information on Docker objects.
Usage:
1
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Example:
1
docker inspect my-container
This command returns detailed information about my-container
.
Advanced Docker Commands
1. docker-compose up
Purpose: Create and start containers using Docker Compose.
Usage:
1
docker-compose up [OPTIONS] [SERVICE...]
Example:
1
docker-compose up -d
This command starts the services defined in docker-compose.yml
in detached mode.
2. docker swarm init
Purpose: Initialize a swarm.
Usage:
1
docker swarm init [OPTIONS]
Example:
1
docker swarm init
This command initializes the current node as a swarm leader.
3. docker secret create
Purpose: Create a secret.
Usage:
1
docker secret create [OPTIONS] SECRET DATA|-
Example:
1
echo "my_secret_password" | docker secret create db_password -
This command creates a Docker secret named db_password
.
4. docker stack deploy
Purpose: Deploy a new stack or update an existing stack.
Usage:
1
docker stack deploy [OPTIONS] STACK
Example:
1
docker stack deploy -c docker-compose.yml my_stack
This command deploys a stack named my_stack
using the docker-compose.yml
file.
5. docker prune
Purpose: Remove unused data.
Usage:
1
docker system prune [OPTIONS]
Example:
1
docker system prune -a
This command removes all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.