Skip to content

Instantly share code, notes, and snippets.

@ritik-malik
Created June 26, 2021 18:55
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ritik-malik/045d5fa7c7b314563e99db4fd290e538 to your computer and use it in GitHub Desktop.
Save ritik-malik/045d5fa7c7b314563e99db4fd290e538 to your computer and use it in GitHub Desktop.
A simple cheatsheet for docker

Docker CLI Overview & Cheatsheet

Getting Started

Downloading Docker

Testing

  • Check the docker version: sudo docker version
  • Do a simple test - head on to https://hub.docker.com/r/docker/whalesay
  • We'll use this image for a test run
    • sudo docker pull docker/whalesay Save the whales! - pull the image and run it
  • Next time it will run from the local image

Docker basic commands

Command Description
docker version to check the version of docker installed
docker run nginx to start a container of nginx from image (if already exist, else it will pull from hub)
docker run --name myserver nginx to start a container of nginx with a name
docker ps list all running containers
docker ps -a to list all containers - currently running as well stopped ones
docker stop containerID/name to stop a running container provide either name or ID (first few letter works as long as it unique)
docker rm containerID/name to remove a container permanently
docker rm -f containerID/name to remove a running container permanently
docker images to see a list of available images
docker rmi name to delete a docker image
docker pull nginx to only pull the docker image and not run the container
docker exec ubuntu cat /etc/passwd to execute command in a container
docker run -d ubuntu:latest to run and detach a container
docker attach ID/name to attach back to the running container
docker cp nginx:file.txt data/ to copy a file from container to host [cp container:source target] (just like scp)
docker cp data.txt nginx:file.txt to copy a file from host to container [cp target container:source]

Docker run

Command Description
docker run redis:4.0 to run a container with a different version, specify with name:tag format (will take latest tag by default)
docker run -i name to run in interactive mode (by default won't take input from STDIN)
docker run -it name to attach a psuedo terminal to the container
docker run -p 69:3306 mysql to map the port 69 of localhost/dockerhost to port 3306 of the container running mysql (-p HOST:CONTAINER)
docker run -P mysql to map all ports
docker run -v /opt/datadir:/var/lib/mysql to persist the data : map a dir outside the container on docker host to a dir inside the container (-v HOST:CONTAINER)
docker inspect name/ID to inspect a container (output JSON)
docker logs name/ID to view logs of a container
docker stats to view stats of running containers
docker top name/ID to view processes of a container

Docker environment variables

Command Description
docker run -e BG_COLOR=blue simple-webapp-color to set the environment variable for our container
docker inspect name/ID to find the env var in a running container, refer to config section in output

Docker Images

Command Description
docker build Dockerfile -t webserver/release-one to build a docker image with a Dockerfile and give it a tag
docker tag Ubuntu Ubuntu:20.04 to tag an image
docker build . build an image from Dockerfile in current directory
docker save nginx > nginx.tar save an image to tar file
docker load -i nginx.tar load an image from tar file
docker push webserver/release-one to host it on docker hub community (docker registry)

Docker CMD vs ENTRYPOINT

  • In case of CMD, when we pass a cmd line argument, docker run ubuntu sleep 10, it will replace the CMD in the Dockerfile
  • In case of ENTRYPOINT, when we pass a cmd line argument, docker run ubuntu 10, it will get appended to ENTRYPOINT in the Dockerfile
  • If user doesn't pass an arg, use both of them instead (the cmds in Dockerfile should be in Json format)-
FROM Ubuntu

ENTRYPOINT ["sleep"]

CMD ["5"]

The CMD will get appended to ENTRYPOINT, else with the cmd line arg, if specified.

  • docker run --entrypoint blah ubuntu 10 : to override default entrypoint

Docker networking

  • When installed docker, it creates 3 networks automatically -

    1. Bridge: the default network a container gets attached to (private + internal). Usually in range of 172.17.x.y
    2. None: container not attached to any network; no connection to external host or other containers, they'll run in completely isolated network
    3. Host: connect directly to the host network
  • docker run Ubuntu --network=none : Can specify a different network

To access the containers externally -

  1. Either port map from container to host
  2. OR run the container on host network: docker run Ubuntu --network=host
    • No network isolation left
    • Now we can access it on same port directly
    • BUT can't use the same port for same another container - unlike before
  • By default docker only creates 1 internal bridge network. We can create more and group containers -

    Command Description
    docker network create --driver bridge --subnet 182.18.0.0/16 custom-iso-net to create a new bridge network
    docker network ls to list all networks
    docker inspect name/ID to check a container's IP, mac, subnet, etc.
  • Docker has a built-in DNS server that helps the containers to resolve each other using the container name

    • This DNS server always runs at - 127.0.0.11
  • Docker uses network namespaces that creates a separate namespace for each container. It then uses virtual ethernet to connect containers together.

Docker Storage

Command Description
/var/lib/docker location in file system where docker store its data
docker volume create data_vol create a volume for persistence storage in /var/lib/docker/data_vol
docker run -v data_vol:/var/lib/mysql mysql to mount the vol in the container
docker run --mount type=bind,source=~/Desktop/data,target=/var/lib/mysql mysql Using -v is old style, use this instead
  • volume mount mounts from volume dir in /var/lib/docker while bind mount mounts from any dir

Topics Uncovered

Topic name Cheatsheet Link
Docker-Compose link
Docker-Swarm link

Other Docker Cheatsheets

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