The Ops Community ⚙️

Cover image for Docker Cheat sheet
kingsathurthi
kingsathurthi

Posted on

Docker Cheat sheet

Hi folks in this post we will see some important docker commands to manage docker image and containers

Process Management

Show all running docker containers

docker ps
Enter fullscreen mode Exit fullscreen mode

Show all docker containers

docker ps -a
Enter fullscreen mode Exit fullscreen mode

Run a container

docker run <image>:<tag>
Enter fullscreen mode Exit fullscreen mode

Run a container and connect to it

docker run -it <image>:<tag>
Enter fullscreen mode Exit fullscreen mode

Run a container in the background

docker run -d <image>:<tag>
Enter fullscreen mode Exit fullscreen mode

Stop a container

docker stop <container>
Enter fullscreen mode Exit fullscreen mode

Kill a container

docker kill <container>
Enter fullscreen mode Exit fullscreen mode

Remove the stopped container

Docker rm <container>
Enter fullscreen mode Exit fullscreen mode

Images/Repository

List available local images

docker image ls
Enter fullscreen mode Exit fullscreen mode

Remove docker image

docker rmi <image>
Enter fullscreen mode Exit fullscreen mode

Search for docker images

docker search <image>
Enter fullscreen mode Exit fullscreen mode

Pull a docker image

docker pull <image>
Enter fullscreen mode Exit fullscreen mode

Build an image with a dockerfile

docker build -t <image>:<tag> <run_directory> -f
<dockerfile>
Enter fullscreen mode Exit fullscreen mode

Login to a remote repository

docker login <repository>
Enter fullscreen mode Exit fullscreen mode

Push an image to your remote repository

docker push <image>:<tag>
Enter fullscreen mode Exit fullscreen mode

Show metadata for an image

docker image inspect <image>
Enter fullscreen mode Exit fullscreen mode

Remove all unused docker images

docker image prune
Enter fullscreen mode Exit fullscreen mode

Docker Compose

Start your docker-compose defined resources in detached mode

docker-compose up -d -f <docker_compose_yaml>
Enter fullscreen mode Exit fullscreen mode

Stop all docker-compose resources

docker-compose stop
Enter fullscreen mode Exit fullscreen mode

Destroy all docker-compose resources

docker-compose down 
Enter fullscreen mode Exit fullscreen mode

Show docker-compose processes

docker-compose ps
Enter fullscreen mode Exit fullscreen mode

Show docker-compose logs

docker-compose logs
Enter fullscreen mode Exit fullscreen mode

Show docker-compose resource consumption

docker-compose top
Enter fullscreen mode Exit fullscreen mode

Pull latest docker-compose images

docker-compose pull
Enter fullscreen mode Exit fullscreen mode

Volumes & Ports

List volumes

docker volume ls
Enter fullscreen mode Exit fullscreen mode

Create a volume

docker volume create <volume>
Enter fullscreen mode Exit fullscreen mode

Delete a volume

docker volume rm <volume>
Enter fullscreen mode Exit fullscreen mode

Show volume metadata

docker volume inspect <volume>
Enter fullscreen mode Exit fullscreen mode

Delete all volumes not attached to a container

docker volume prune
Enter fullscreen mode Exit fullscreen mode

Mount a local directory to your container

docker run -v <local_dir>:<container_dir> <image>
Enter fullscreen mode Exit fullscreen mode

Copy file or folder from a docker container to host machine

docker cp <container>:<container_dir> <local_dir>
Enter fullscreen mode Exit fullscreen mode

Copy file or folder from local machine onto a container

docker cp <local_dir> <container>:<container_dir>
Enter fullscreen mode Exit fullscreen mode

Map a local port to a docker instance

docker run -d -p <local_port>:<docker_port> <image>
Enter fullscreen mode Exit fullscreen mode

List the ports a docker container is running on

docker port <container>
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

Show the logs of a container

docker logs <container>
Enter fullscreen mode Exit fullscreen mode

Follow/tail the logs of a container

docker logs -f <container>
Enter fullscreen mode Exit fullscreen mode

Show timestamps on docker logs

docker logs -t <container>
Enter fullscreen mode Exit fullscreen mode

Show details/metadata of a container

docker inspect <container>
Enter fullscreen mode Exit fullscreen mode

Show a 'top' view of processes running on a container

docker top <container>
Enter fullscreen mode Exit fullscreen mode

Show a 'top' view of all docker containers

docker stats
Enter fullscreen mode Exit fullscreen mode

Show any files that have changed since startup

docker diff <container>
Enter fullscreen mode Exit fullscreen mode

Connect to an already running container

docker attach <container>
Enter fullscreen mode Exit fullscreen mode

Execute a command on a container

docker exec-it <container_id> /bin/bash
Enter fullscreen mode Exit fullscreen mode

Show docker system wide information

docker system info
Enter fullscreen mode Exit fullscreen mode

Show docker disk space used

docker system df
Enter fullscreen mode Exit fullscreen mode

Remove build cache

docker builder prune
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
hrittikhere profile image
Hrittik Roy

🔥