Docker Cheat Sheet

Here is a list of some basic Docker Commands to help you get started with Docker. This helped me a lot and still does from time to time. Note, this not meant to be a complete Docker reference. For complete list, please visit docs.docker.com

Containers

Pull a base image.
docker pull ubuntu

Start a container
docker run --name docker-nginx -p 80:80 -d nginx
docker run --name docker-nginx -p 80:80 -d -v /app/html:/usr/share/nginx/html nginx

For list of running containers
docker ps

For list of all containers
docker ps - a

Restart a Container.
docker restart container_id

Show all running process in a Container.
docker top container_id

Stop container
docker stop container_id

Remove container
docker rm container_id>

Kill containers and remove them
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
docker rm $(docker kill $(docker ps -aq))

Clean up old containers that are weeks old (via Stackoverflow)
docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm

Print the last 100 lines of a container’s logs
docker logs --tail 100 container_id

Create a new bash process inside the container and connect it to the terminal
docker exec -it container_id bash

Docker Container link
Docker run -d -p 5000:5000 --link redis dockerapp:v0.3

Docker Images

Remove all images
docker rmi $(docker images -q) -f
docker rmi $(docker images -qf "dangling=true")

Remove all images except “my-image”
You could use grep to remove all except my-image and ubuntu
docker rmi $(docker images | grep -v ‘ubuntu\|my-image’ | awk {‘print $3’})

Save a running container as an image
docker commit -m "commit message" -a "author" container_id username/image_name:tag

Other Docker Commands

Run commands inside an existing container
docker exec -it [containerID] bash

Push to Images to Docker
docker tag 2f98ca2e63ab docker-nginx:1.01
docker login --username muralibala
docker push docker-nginx:1.01

To find Ip address
docker machine ls

List the networks
docker network ls

List the volumes
docker volume ls