Docker Commands
Docker provides a comprehensive set of commands that enable you to manage containers, images, networks, and other Docker objects effectively. Mastering these commands is essential for optimizing your Docker workflows, whether you're building images, running containers, or configuring networks.
Basic Docker Commands
These fundamental Docker commands are crucial for working with containers and images:
docker run
- Purpose: Creates and starts a new container from a Docker image.
- Example:
Runs an Nginx container in detached mode (
-d
), mapping port 80 of the container to port 80 on the host (-p
).
docker ps
- Purpose: Lists all running containers.
- Example:
Use
-a
to view all containers, including stopped ones:
docker stop
- Purpose: Stops a running container.
- Example:
Replace
container_id
with the container's ID or name.
docker rm
- Purpose: Removes a stopped container.
- Example: To remove all stopped containers:
docker rmi
- Purpose: Removes one or more Docker images from your local system.
- Example: To remove unused (dangling) images:
docker images
- Purpose: Lists all Docker images on your local machine.
- Example:
docker build
- Purpose: Creates a Docker image from a Dockerfile.
- Example:
Builds an image named
myapp
from the Dockerfile in the current directory.
docker pull
- Purpose: Downloads a Docker image from a registry.
- Example: Pulls the Redis image from Docker Hub.
docker push
- Purpose: Uploads a Docker image to a registry.
- Example:
Pushes the
myapp
image to themyrepo
repository on Docker Hub or another registry.
docker exec
- Purpose: Runs a command inside a running container.
- Example:
Opens an interactive shell (
bash
) inside the specified container.
docker logs
- Purpose: Fetches the logs of a running container.
- Example:
Use
-f
to follow the log output in real-time:
Docker Networking Commands
Manage and configure Docker networks with these commands:
docker network ls
- Purpose: Lists all Docker networks.
- Example:
docker network create
- Purpose: Creates a new Docker network.
- Example:
docker network connect
- Purpose: Connects a container to a network.
- Example:
docker network inspect
- Purpose: Displays detailed information about a network.
- Example:
docker network rm
- Purpose: Removes one or more Docker networks.
- Example:
Learn more about container networking.
Docker Volume Commands
Docker volumes persist data generated by and used by containers. These commands help you manage Docker volumes:
docker volume ls
- Purpose: Lists all Docker volumes.
- Example:
docker volume create
- Purpose: Creates a new volume.
- Example:
docker volume inspect
- Purpose: Displays detailed information about a volume.
- Example:
docker volume rm
- Purpose: Removes a Docker volume.
- Example:
docker volume prune
- Purpose: Removes all unused volumes.
- Example:
Dive deeper into Docker volumes.
Advanced Docker Commands
These commands are useful for more advanced Docker operations:
docker-compose
- Purpose: Interacts with Docker Compose files to define and run multi-container applications.
- Example:
Starts all the services defined in a
docker-compose.yml
file.
docker inspect
- Purpose: Returns detailed information about a Docker object, such as a container or image.
- Example:
docker stats
- Purpose: Displays real-time resource usage statistics for containers.
- Example:
docker system prune
- Purpose: Removes all unused containers, networks, images, and optionally, volumes.
- Example:
Use
-a
to remove all unused images, not just dangling ones:
docker tag
- Purpose: Creates a new tag for an existing Docker image.
- Example:
Tags the image
myimage:latest
with a new name and version,myrepo/myimage:v1.0
.
docker cp
- Purpose: Copies files/folders between a container and the local filesystem.
- Example: Copies a file from the container to the local machine.