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:


5. Stop all running containers.

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

This command issues  SIGKILL signal to all the containers.

6. Remove all stopped containers.

7. Remove all images.

8. To remove stopped containers

9.  To get logs from a container

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