Docker Cheatsheet

Most common Docker commands in the nutshell.

1. To list all running containers

$ docker ps -a 

2. To view all running containers IDs

docker ps -aq

3. Pull image using image name

$ docker pull <image_name>

4. To start a container

$ docker run <image_name>

The above command will start a container for that image and will put logs to stdout. If you don’t want to see the logs at stdout use start instead of run.

$ docker start <image_name>

To override the default command in the container:

$ docker run <image_name> <command to run>


➜ ~ docker run busybox echo hello docker
hello docker

5. Stop all running containers.

$ docker stop $(docker ps -aq)

This issues a SIGTERM signal to all the containers. When containers don’t respond, it sends a SIGKILL

$ docker kill $(docker ps -aq)

This command issues  SIGKILL signal to all the containers.

6. Remove all stopped containers.

$ docker rm $(docker ps -aq)

7. Remove all images.

$ docker rmi $(docker images -q)

8. To remove stopped containers

$ docker system prune

9.  To get logs from a container

$ docker logs <containerid>

You can also see or use logs command on a stopped container.

10. To execute a command from the container.

$ docker exec -it <container_id> <command>

➜ ~ docker exec -it 9811888d36f4 ls
data prometheus.yml

When we specify -i in docker exec command, what we are trying to say is to send to stdin for the container, the stuff we are typing in our shell.

-t flag takes  the stdout for the container and sends it to stdout for our shell

Leave a Reply

Your email address will not be published. Required fields are marked *