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:
docker run -d -p 80:80 nginx
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:
docker ps
Use -a to view all containers, including stopped ones:
docker ps -adocker stop
- Purpose: Stops a running container.
- Example:
docker stop container_id
Replace container_id with the container's ID or name.
docker rm
- Purpose: Removes a stopped container.
- Example:
docker rm container_id
To remove all stopped containers:
docker rm $(docker ps -a -q)docker rmi
- Purpose: Removes one or more Docker images from your local system.
- Example:
docker rmi image_id
To remove unused (dangling) images:
docker image prunedocker images
- Purpose: Lists all Docker images on your local machine.
- Example:
docker images
docker build
- Purpose: Creates a Docker image from a Dockerfile.
- Example:
docker build -t myapp .
Builds an image named myapp from the Dockerfile in the current directory.
docker pull
- Purpose: Downloads a Docker image from a registry.
- Example:
docker pull redis
Pulls the Redis image from Docker Hub.
docker push
- Purpose: Uploads a Docker image to a registry.
- Example:
docker push myrepo/myapp
Pushes the myapp image to the myrepo repository on Docker Hub or another registry.
docker exec
- Purpose: Runs a command inside a running container.
- Example:
docker exec -it container_id bash
Opens an interactive shell (bash) inside the specified container.
docker logs
- Purpose: Fetches the logs of a running container.
- Example:
docker logs container_id
Use -f to follow the log output in real-time:
docker logs -f container_idDocker Networking Commands
Manage and configure Docker networks with these commands:
docker network ls
- Purpose: Lists all Docker networks.
- Example:
docker network ls
docker network create
- Purpose: Creates a new Docker network.
- Example:
docker network create mynetwork
docker network connect
- Purpose: Connects a container to a network.
- Example:
docker network connect mynetwork container_id
docker network inspect
- Purpose: Displays detailed information about a network.
- Example:
docker network inspect mynetwork
docker network rm
- Purpose: Removes one or more Docker networks.
- Example:
docker network rm mynetwork
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 ls
docker volume create
- Purpose: Creates a new volume.
- Example:
docker volume create myvolume
docker volume inspect
- Purpose: Displays detailed information about a volume.
- Example:
docker volume inspect myvolume
docker volume rm
- Purpose: Removes a Docker volume.
- Example:
docker volume rm myvolume
docker volume prune
- Purpose: Removes all unused volumes.
- Example:
docker volume prune
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:
docker-compose up
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 inspect container_id
docker stats
- Purpose: Displays real-time resource usage statistics for containers.
- Example:
docker stats
docker system prune
- Purpose: Removes all unused containers, networks, images, and optionally, volumes.
- Example:
docker system prune
Use -a to remove all unused images, not just dangling ones:
docker system prune -adocker tag
- Purpose: Creates a new tag for an existing Docker image.
- Example:
docker tag myimage:latest myrepo/myimage:v1.0
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:
docker cp container_id:/path/to/file /local/path
Copies a file from the container to the local machine.