Useful docker commands that we may use daily

1.List all running containers

$ docker ps -a 

2. List all running containers (only IDs)

$ docker ps -aq

3. Pull an image

$ docker pull <image_name>

4. Start/Run 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 systemout use start instead of run.

$ docker start <image_name>

Another variation of docker run can be used to override the default command in the container

$ docker run <image_name> <command to run>

➜ ~ docker run busybox echo hey there
hey there

5. Stop all running containers.

$ docker stop $(docker ps -aq)

This issues a SIGTERM signal to all the containers. However if containers don't respond it automatically sends a SIGKILL.

$ docker kill $(docker ps -aq)

This issues a 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. Docker Cleanup Stopped containers, unused networks, dangling container,images

$ docker system prune

9.  Get logs from a container

$ docker logs <containerid>

       Note you can use docker logs on even a stopped container.

10. Execute a command from inside 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

 


0 Comments

What do you think?