I recently went through my second study phase around docker containers recently. Docker is a huge topic these days and widly used in the application containerization. I use Docker at home and at work in many projects. I thought it would be useful to create a Docker Cheat Sheet for my reference and share that among the community and that lead to this post. In this post, I’m going to list the most common commands used when playing with Docker containers.
General Listing Commands
List all docker containers
1
docker ps -a
List running docker containers
1
docker ps -a
List docker images in the local store
1
docker images
Download and store an image from the internet
1
docker pull ubuntu
Running Docker Containers
Run an instance of a container image
1
docker run ubuntu
Run an instance of a container image in detached mode (run in background)
1
docker run -d nginx
Run an instance of a Ubuntu container images with terminal and input attached.
1
docker run -it ubuntu bash
Stopping and Removing Docker Containers
Stop a running container instance
1
docker stop ubuntu
Remove a terminated container (Container Id = b5a12307c030)
1
docker rm b5a12307c030
Remove a container image from the local store
1
docker rmi ubuntu
Container interaction
Run a container instance interactively
1
docker run -it ubuntu bash
Remote into a running container instance (Container id = b5a12307c030 )
1
docker exec -it b5a12307c030 bash
Execute a command in a running container Eg: ps -eaf
1
docker exec b5a12307c030 ps -eaf
Map a Port to a Container
- Map a host port to a new container instance (Host Port = 5000, Container Port = 80, Container image = nginx)
1
docker run -p 80:5000 nginx -d
Mount a Volume
- Mount a host volume to a new container (Host Volume Path = /opt/mydata, Container Mount Path = /var/lib/mysql)
1
docker run -v /opt/mydata:/var/lib/mysql mysql
Inspect Container Logs
- Inspect container logs of Container ID = b5a12307c030
1
docker logs b5a12307c030
Other Handy Commands
Remove all excited Containers
1
docker rm -f $(docker ps -q --filter "status=exited")
Monitor Docker disk consumption
1
docker system df