Learn Docker CLI Commands
Contents
Docker Container Management Commands
docker run
- Purpose: Used to create and start a container from a specified image.
- Example: Running an Nginx container can be done with
docker run -d -p 80:80 nginx
. This command runs the container in the background (-d
) and binds port 80 of the container to port 80 on the host machine (-p 80:80
).
docker start
- Purpose: Start containers that have been previously stopped.
- Example: To restart a stopped container named
my_container
, usedocker start my_container
.
docker stop
- Purpose: Gracefully stop a running container.
- Example: To stop a running container named
my_container
, usedocker stop my_container
.
docker restart
- Purpose: Restart one or more containers.
- Example: To restart a container named
my_container
, the command isdocker restart my_container
.
docker kill
- Purpose: Forcefully stop a running container by killing its main process.
- Example: To kill a container immediately, use
docker kill my_container
.
docker rm
- Purpose: Remove stopped containers.
- Example: Use
docker rm my_container
to delete a container that is no longer running.
docker pause
- Purpose: Temporarily suspend all processes in a container.
- Example:
docker pause my_container
will pause a container.
docker unpause
- Purpose: Resume a paused container’s processes.
- Example: Resume the container with
docker unpause my_container
.
docker ps
- Purpose: List all currently running containers.
- Example:
docker ps
shows active containers, whiledocker ps -a
shows all containers, including those that are stopped.
docker logs
- Purpose: Fetch the logs from a container.
- Example: To view the logs of
my_container
, usedocker logs my_container
.
docker exec
- Purpose: Run a command inside a running container.
- Example: To open a bash shell in
my_container
, usedocker exec -it my_container bash
.
docker attach
- Purpose: Attach your terminal to a running container’s main process.
- Example: Use
docker attach my_container
to interact with the container’s standard input, output, and error streams.
docker inspect
- Purpose: Display detailed information about a container or image.
- Example: Get detailed information on
my_container
by runningdocker inspect my_container
.
docker top
- Purpose: View the active processes running within a container.
- Example: See the running processes in
my_container
withdocker top my_container
.
docker stats
- Purpose: Monitor real-time resource usage statistics for containers.
- Example: To get a live stream of resource usage stats for
my_container
, usedocker stats my_container
.
docker commit
- Purpose: Create a new image from a container’s changes.
- Example: Save the state of a container as a new image with
docker commit my_container my_new_image
.
docker rename
- Purpose: Rename an existing container.
- Example: To rename a container from
old_name
tonew_name
, usedocker rename old_name new_name
.
docker update
- Purpose: Update configuration options for an existing container.
- Example: Change the memory limit of
my_container
withdocker update --memory 512m my_container
.
Docker Image Management Commands
docker pull
- Purpose: Download a specific image from a Docker registry.
- Example: To pull the latest Nginx image from Docker Hub, use
docker pull nginx
.
docker push
- Purpose: Upload an image to a Docker registry.
- Example: Push your locally built image to a repository with
docker push my_image:tag
.
docker build
- Purpose: Create an image from a Dockerfile.
- Example: Build an image with
docker build -t my_image:tag .
, where.
specifies the build context.
docker images
- Purpose: List all Docker images stored on your machine.
- Example: Use
docker images
to see a list of all images, along with their repository names, tags, and sizes.
docker rmi
- Purpose: Delete one or more images from your local storage.
- Example: Remove an image by running
docker rmi my_image:tag
.
docker tag
- Purpose: Tag an existing image with a new name or repository.
- Example: Tag an image for a different repository with
docker tag my_image:tag my_repo/my_image:tag
.
docker save
- Purpose: Export one or more images to a tar archive.
- Example: Save an image as a tar file using
docker save -o my_image.tar my_image:tag
.
docker load
- Purpose: Load an image from a tar archive.
- Example: Import an image from a tar file with
docker load -i my_image.tar
.
docker history
- Purpose: Show the history of changes made to an image.
- Example: View the layers of an image with
docker history my_image:tag
.
Docker Volume Management Commands
docker volume create
- Purpose: Create a new volume to store data outside of containers.
- Example: Create a volume named
my_volume
withdocker volume create my_volume
.
docker volume ls
- Purpose: List all Docker volumes on your system.
- Example: Use
docker volume ls
to see a list of all volumes.
docker volume inspect
- Purpose: Display detailed information about a volume.
- Example: To view details of
my_volume
, usedocker volume inspect my_volume
.
docker volume rm
- Purpose: Delete one or more volumes.
- Example: Remove a volume by running
docker volume rm my_volume
.
docker volume prune
- Purpose: Remove all unused volumes.
- Example: Clean up all unused volumes with
docker volume prune
.
Docker Network Management Commands
docker network create
- Purpose: Create a new network for containers to communicate.
- Example: Create a network named
my_network
usingdocker network create my_network
.
docker network ls
- Purpose: List all Docker networks.
- Example: Use
docker network ls
to see all networks available on your system.
docker network inspect
- Purpose: Display detailed information about a network.
- Example: Get detailed info on
my_network
withdocker network inspect my_network
.
docker network connect
- Purpose: Connect a container to an existing network.
- Example: Connect
my_container
tomy_network
usingdocker network connect my_network my_container
.
docker network disconnect
- Purpose: Disconnect a container from a network.
- Example: Disconnect
my_container
frommy_network
withdocker network disconnect my_network my_container
.
docker network rm
- Purpose: Delete one or more networks.
- Example: Remove a network with
docker network rm my_network
.
Docker System and Cleanup Commands
docker system df
- Purpose: Display disk usage statistics for Docker.
- Example: Use
docker system df
to see how much disk space is being used by images, containers, volumes, and more.
docker system prune
- Purpose: Remove all unused data, including stopped containers, unused images, and networks.
- Example: Clean up your system with
docker system prune
.
docker info
- Purpose: Display detailed information about the Docker installation.
- Example: View system-wide Docker information with
docker info
.
docker version
- Purpose: Show Docker version details.
- Example: Use
docker version
to see the version of the Docker client and server.
docker events
- Purpose: Get a real-time stream of Docker events.
- Example: Track Docker activity live with
docker events
.
docker login
- Purpose: Log in to a Docker registry.
- Example: Authenticate with a Docker registry using
docker login
.
docker logout
- Purpose: Log out from a Docker registry.
- Example: To log out from the current registry, use
docker logout
.
Docker Compose Commands
docker-compose up
- Purpose: Start and run containers as defined in a
docker-compose.yml
file. - Example: Start services in the background with
docker-compose up -d
.
- Purpose: Start and run containers as defined in a
docker-compose down
- Purpose: Stop and remove containers, networks, images, and volumes defined in a
docker-compose.yml
file. - Example: Stop and remove all services with
docker-compose down
.
- Purpose: Stop and remove containers, networks, images, and volumes defined in a
docker-compose build
- Purpose: Build or rebuild services as defined in the
docker-compose.yml
file. - Example: Build the services with
docker-compose build
.
- Purpose: Build or rebuild services as defined in the
docker-compose ps
- Purpose: List the status of all services defined in a
docker-compose.yml
file. - Example: View the status of services with
docker-compose ps
.
- Purpose: List the status of all services defined in a
docker-compose logs
- Purpose: View logs output by services.
- Example: Check logs for services with
docker-compose logs
.