Skip to content

Instantly share code, notes, and snippets.

@addityasingh
Last active February 18, 2019 16:50
Show Gist options
  • Save addityasingh/77457394b593b087e599bb594a342afa to your computer and use it in GitHub Desktop.
Save addityasingh/77457394b593b087e599bb594a342afa to your computer and use it in GitHub Desktop.
Docker cheatsheet

Basic commands

// Run docker image in interactive mode
docker run -it <image name>:<image tag>  

// Run docker image in interactive mode with host port 8080 mapped to container port 80
docker run -it -p 8080:80 <image name>:<image tag> 

// Run docker with the 3001 port exposed, 
docker run -it --expose=3001 <image name>:<image tag>
  
// Run docker with an environment variable set
docker run -it	\
  -e RUST_BACKTRACE=1 \
  <image name>:<image tag>

// Run a docker command in container and remove container after finish
docker run --rm -it --expose=3001 <image name>:<image tag>

// Check the process running inside the container
docker top <container name or id>

// Check resource utilization
docker stats

// Check docker logs with multiple time options
docker logs -f --since <1s|5m|1d|UTC date time> <container name>

// Start and stop a container by name
docker start <container name>
docker stop <container name>

// Update container configurations
docker update --restart=always <container name>
docker update --kernel-memory 80M <container name>

// Inspect container configurations
docker inspect

// docker system information
docker system df (Show disk info)
docker system events (Show system events)
docker system info (Show system info)
docker system prune (Prune unused data)

https://docs.docker.com/engine/reference/run/

Docker new API 17.0

With the new docker reoragnisation of modules, all commands are re-structured similar to Kubernetes. E.g.

Below 2 are same:

docker run -it -e RUST_BACKTRACE=1 <image name>:<image tag>
docker container run -it -e RUST_BACKTRACE=1 <image name>:<image tag>

And so are the below 2 commands to list containers,

docker ps -a
docker container ls

Cleanup unused or dangling containers or images

docker rm $(docker ps -q -f 'status=exited')
docker rmi $(docker images -q -f "dangling=true")
docker volume rm $(docker volume ls -qf dangling=true)

Remove images with a certain text in name

docker images | grep <pattern> | xargs docker rmi

Docker snoop network settings of a running container

docker inspect $container_id | $container_name

Difference between -p, -P, EXPOSE and, --link

https://www.ctl.io/developers/blog/post/docker-networking-rules/

New docker commands to prune unused docker artifacts

docker system prune

Or

docker container prune
docker image prune
docker network prune
docker volume prune

https://stackoverflow.com/a/32723127/3878940

Run bash with docker alpine

Try any of

docker run -it --rm alpine /bin/ash
docker run -it --rm alpine /bin/sh
docker run -it --rm alpine ash
docker run -it --rm alpine sh

Other links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment