DEV Community

Cover image for Docker Cheat Sheet
HasOne
HasOne

Posted on • Updated on

Docker Cheat Sheet

docker-cheat-sheet

Docker Commands, Help & Tips

Show commands & management commands

$ docker
Enter fullscreen mode Exit fullscreen mode

Docker version info

$ docker version
Enter fullscreen mode Exit fullscreen mode

Show info like number of containers, etc

$ docker info
Enter fullscreen mode Exit fullscreen mode

WORKING WITH CONTAINERS

Create an run a container in foreground

$ docker container run -it -p 80:80 nginx
Enter fullscreen mode Exit fullscreen mode

Create an run a container in background

$ docker container run -d -p 80:80 nginx
Enter fullscreen mode Exit fullscreen mode

Shorthand

$ docker container run -d -p 80:80 nginx
Enter fullscreen mode Exit fullscreen mode

Naming Containers

$ docker container run -d -p 80:80 --name nginx-server nginx
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

OR

$ docker ps
Enter fullscreen mode Exit fullscreen mode

List all containers (Even if not running)

$ docker container ls -a
Enter fullscreen mode Exit fullscreen mode

Stop container

$ docker container stop [ID]
Enter fullscreen mode Exit fullscreen mode

Stop all running containers

$ docker stop $(docker ps -aq)
Enter fullscreen mode Exit fullscreen mode

Remove container (Can not remove running containers, must stop first)

$ docker container rm [ID]
Enter fullscreen mode Exit fullscreen mode

To remove a running container use force(-f)

$ docker container rm -f [ID]
Enter fullscreen mode Exit fullscreen mode

Remove multiple containers

$ docker container rm [ID] [ID] [ID]
Enter fullscreen mode Exit fullscreen mode

Remove all containers

$ docker rm $(docker ps -aq)
Enter fullscreen mode Exit fullscreen mode

Get logs (Use name or ID)

$ docker container logs [NAME]
Enter fullscreen mode Exit fullscreen mode

List processes running in container

$ docker container top [NAME]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

We can also just pull down images

$ docker pull [IMAGE]
Enter fullscreen mode Exit fullscreen mode

Remove image

$ docker image rm [IMAGE]
Enter fullscreen mode Exit fullscreen mode

Remove all images

$ docker rmi $(docker images -a -q)
Enter fullscreen mode Exit fullscreen mode

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:

$ docker container run -d -p 80:80 --name nginx nginx (-p 80:80 is optional as it runs on 80 by default)
Enter fullscreen mode Exit fullscreen mode

APACHE:

$ docker container run -d -p 8080:80 --name apache httpd
Enter fullscreen mode Exit fullscreen mode

MONGODB:

$ docker container run -d -p 27017:27017 --name mongo mongo
Enter fullscreen mode Exit fullscreen mode

MYSQL:

$ docker container run -d -p 3306:3306 --name mysql --env MYSQL_ROOT_PASSWORD=123456 mysql
Enter fullscreen mode Exit fullscreen mode

CONTAINER INFO

View info on container

$ docker container inspect [NAME]
Enter fullscreen mode Exit fullscreen mode

Specific property (--format)

$ docker container inspect --format '{{ .NetworkSettings.IPAddress }}' [NAME]
Enter fullscreen mode Exit fullscreen mode

Performance stats (cpu, mem, network, disk, etc)

$ docker container stats [NAME]
Enter fullscreen mode Exit fullscreen mode

ACCESSING CONTAINERS

Create new nginx container and bash into

$ docker container run -it --name [NAME] nginx bash
Enter fullscreen mode Exit fullscreen mode
  • i = interactive Keep STDIN open if not attached
  • t = tty - Open prompt

For Git Bash, use "winpty"

$ winpty docker container run -it --name [NAME] nginx bash
Enter fullscreen mode Exit fullscreen mode

Run/Create Ubuntu container

$ docker container run -it --name ubuntu ubuntu
Enter fullscreen mode Exit fullscreen mode

(no bash because ubuntu uses bash by default)

You can also make it so when you exit the container does not stay by using the -rm flag

$ docker container run --rm -it --name [NAME] ubuntu
Enter fullscreen mode Exit fullscreen mode

Access an already created container, start with -ai

$ docker container start -ai ubuntu
Enter fullscreen mode Exit fullscreen mode

Use exec to edit config, etc

$ docker container exec -it mysql bash
Enter fullscreen mode Exit fullscreen mode

Alpine is a very small Linux distro good for docker

$ docker container run -it alpine sh
Enter fullscreen mode Exit fullscreen mode

(use sh because it does not include bash)
(alpine uses apk for its package manager - can install bash if you want)

NETWORKING

"bridge" or "docker0" is the default network

Get port

$ docker container port [NAME]
Enter fullscreen mode Exit fullscreen mode

List networks

$ docker network ls
Enter fullscreen mode Exit fullscreen mode

Inspect network

$ docker network inspect [NETWORK_NAME]
("bridge" is default)
Enter fullscreen mode Exit fullscreen mode

Create network

$ docker network create [NETWORK_NAME]
Enter fullscreen mode Exit fullscreen mode

Create container on network

$ docker container run -d --name [NAME] --network [NETWORK_NAME] nginx
Enter fullscreen mode Exit fullscreen mode

Connect existing container to network

$ docker network connect [NETWORK_NAME] [CONTAINER_NAME]
Enter fullscreen mode Exit fullscreen mode

Disconnect container from network

$ docker network disconnect [NETWORK_NAME] [CONTAINER_NAME]
Enter fullscreen mode Exit fullscreen mode

Detach network from container

$ docker network disconnect
Enter fullscreen mode Exit fullscreen mode

IMAGE TAGGING & PUSHING TO DOCKERHUB

tags are labels that point ot an image ID

$ docker image ls
Enter fullscreen mode Exit fullscreen mode

Youll see that each image has a tag

Retag existing image

$ docker image tag nginx btraversy/nginx
Enter fullscreen mode Exit fullscreen mode

Upload to dockerhub

$ docker image push bradtraversy/nginx
Enter fullscreen mode Exit fullscreen mode

If denied, do

$ docker login
Enter fullscreen mode Exit fullscreen mode

Add tag to new image

$ docker image tag bradtraversy/nginx bradtraversy/nginx:testing
Enter fullscreen mode Exit fullscreen mode

DOCKERFILE PARTS

  • FROM - The os used. Common is alpine, debian, ubuntu
  • ENV - Environment variables
  • RUN - Run commands/shell scripts, etc
  • EXPOSE - Ports to expose
  • CMD - Final command run when you launch a new container from image
  • WORKDIR - Sets working directory (also could use 'RUN cd /some/path')
  • COPY # Copies files from host to container

Build image from dockerfile (reponame can be whatever)

From the same directory as Dockerfile

$ docker image build -t [REPONAME] .
Enter fullscreen mode Exit fullscreen mode

TIP: CACHE & ORDER

  • If you re-run the build, it will be quick because everythging is cached.
  • If you change one line and re-run, that line and everything after will not be cached
  • Keep things that change the most toward the bottom of the Dockerfile

EXTENDING DOCKERFILE

Custom Dockerfile for html paqge with nginx

FROM nginx:latest # Extends nginx so everything included in that image is included here
WORKDIR /usr/share/nginx/html
COPY index.html index.html
Enter fullscreen mode Exit fullscreen mode

Build image from Dockerfile

$ docker image build -t nginx-website
Enter fullscreen mode Exit fullscreen mode

Running it

$ docker container run -p 80:80 --rm nginx-website
Enter fullscreen mode Exit fullscreen mode

Tag and push to Dockerhub

$ docker image tag nginx-website:latest btraversy/nginx-website:latest
Enter fullscreen mode Exit fullscreen mode
$ docker image push bradtraversy/nginx-website
Enter fullscreen mode Exit fullscreen mode

VOLUMES

Volume - Makes special location outside of container UFS. Used for databases

Bind Mount -Link container path to host path

Check volumes

$ docker volume ls
Enter fullscreen mode Exit fullscreen mode

Cleanup unused volumes

$ docker volume prune
Enter fullscreen mode Exit fullscreen mode

Pull down mysql image to test

$ docker pull mysql
Enter fullscreen mode Exit fullscreen mode

Inspect and see volume

$ docker image inspect mysql
Enter fullscreen mode Exit fullscreen mode

Run container

$ docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True mysql
Enter fullscreen mode Exit fullscreen mode

Inspect and see volume in container

$ docker container inspect mysql
Enter fullscreen mode Exit fullscreen mode

TIP: Mounts

  • You will also see the volume under mounts
  • Container gets its own uniqe location on the host to store that data
  • Source: xxx is where it lives on the host

Check volumes

$ docker volume ls
Enter fullscreen mode Exit fullscreen mode

There is no way to tell volumes apart for instance with 2 mysql containers, so we used named volumes

Named volumes (Add -v command)(the name here is mysql-db which could be anything)

$ docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True -v mysql-db:/var/lib/mysql mysql
Enter fullscreen mode Exit fullscreen mode

Inspect new named volume

docker volume inspect mysql-db
Enter fullscreen mode Exit fullscreen mode

BIND MOUNTS

  • Can not use in Dockerfile, specified at run time (uses -v as well)
  • ... run -v /Users/brad/stuff:/path/container (mac/linux)
  • ... run -v //c/Users/brad/stuff:/path/container (windows)

TIP: Instead of typing out local path, for working directory use $(pwd):/path/container - On windows may not work unless you are in your users folder

Run and be able to edit index.html file (local dir should have the Dockerfile and the index.html)

$ docker container run  -p 80:80 -v $(pwd):/usr/share/nginx/html nginx
Enter fullscreen mode Exit fullscreen mode

Go into the container and check

$ docker container exec -it nginx bash
$ cd /usr/share/nginx/html
$ ls -al
Enter fullscreen mode Exit fullscreen mode

You could create a file in the container and it will exiost on the host as well

$ touch test.txt
Enter fullscreen mode Exit fullscreen mode

DOCKER COMPOSE

  • Configure relationships between containers
  • Save our docker container run settings in easy to read file
  • 2 Parts: YAML File (docker.compose.yml) + CLI tool (docker-compose)

1. docker.compose.yml - Describes solutions for

  • containers
  • networks
  • volumes

2. docker-compose CLI - used for local dev/test automation with YAML files

Sample compose file (From Bret Fishers course)

version: '2'

# same as
# docker run -p 80:4000 -v $(pwd):/site bretfisher/jekyll-serve

services:
  jekyll:
    image: bretfisher/jekyll-serve
    volumes:
      - .:/site
    ports:
      - '80:4000'
Enter fullscreen mode Exit fullscreen mode

To run

docker-compose up
Enter fullscreen mode Exit fullscreen mode

You can run in the background with

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

To cleanup

docker-compose down
Enter fullscreen mode Exit fullscreen mode

Git repo: Docker

Top comments (7)

Collapse
 
hasone profile image
HasOne
Collapse
 
rafaelaybar profile image
Rafael Aybar Segura • Edited

I have sent a PR about te DOCKER_BUILDKIT=1 flag, it's very useful if you need build benchmarks github.com/lifeeric/docker-cheat-s...

Collapse
 
hasone profile image
HasOne

I would like to thank you very much!

Collapse
 
rafaelaybar profile image
Rafael Aybar Segura

You are welcome!

Collapse
 
abrandao profile image
Anderson Brandão

Really thanks for this article. Bookmarking right now.

Collapse
 
hasone profile image
HasOne

glad to hear it!

Collapse
 
clipso profile image
Clipso

thanks!