docker-cheat-sheet
Docker Commands, Help & Tips
Show commands & management commands
$ docker
Docker version info
$ docker version
Show info like number of containers, etc
$ docker info
WORKING WITH CONTAINERS
Create an run a container in foreground
$ docker container run -it -p 80:80 nginx
Create an run a container in background
$ docker container run -d -p 80:80 nginx
Shorthand
$ docker container run -d -p 80:80 nginx
Naming Containers
$ docker container run -d -p 80:80 --name nginx-server nginx
TIP: WHAT RUN DID
- Looked for image called nginx in image cache
- If not found in cache, it looks to the default image repo on Dockerhub
- Pulled it down (latest version), stored in the image cache
- Started it in a new container
- We specified to take port 80- on the host and forward to port 80 on the container
- We could do "$ docker container run --publish 8000:80 --detach nginx" to use port 8000
- We can specify versions like "nginx:1.09"
List running containers
$ docker container ls
OR
$ docker ps
List all containers (Even if not running)
$ docker container ls -a
Stop container
$ docker container stop [ID]
Stop all running containers
$ docker stop $(docker ps -aq)
Remove container (Can not remove running containers, must stop first)
$ docker container rm [ID]
To remove a running container use force(-f)
$ docker container rm -f [ID]
Remove multiple containers
$ docker container rm [ID] [ID] [ID]
Remove all containers
$ docker rm $(docker ps -aq)
Get logs (Use name or ID)
$ docker container logs [NAME]
List processes running in container
$ docker container top [NAME]
TIP: ABOUT CONTAINERS
Docker containers are often compared to virtual machines but they are actually just processes running on your host os. In Windows/Mac, Docker runs in a mini-VM so to see the processes youll need to connect directly to that. On Linux however you can run "ps aux" and see the processes directly
IMAGE COMMANDS
List the images we have pulled
$ docker image ls
We can also just pull down images
$ docker pull [IMAGE]
Remove image
$ docker image rm [IMAGE]
Remove all images
$ docker rmi $(docker images -a -q)
TIP: ABOUT IMAGES
- Images are app bianaries and dependencies with meta data about the image data and how to run the image
- Images are no a complete OS. No kernel, kernel modules (drivers)
- Host provides the kernel, big difference between VM
Some sample container creation
NGINX: