Abik Maharjan · Backend Developer

WritingKathmandu--:--:--

← Writing

Docker cheatsheet

Basic docker commands and other docker things I noted while I was learning docker.

Basic commands

download a docker image from registry (dockerhub or others).

docker pull IMAGE
docker pull ubuntu

List available images on local docker host.

docker images
docker image -q

List running containers.

docker ps
docker ps -a

Create a container in a stopped state.

docker create IMAGE
docker create --name my-container nginx

Start container (that is not running).

docker start CONTAINER_\[NAME/ID\]
docker start my-container
docker start abc123

Stop container gracefully.

docker stop CONTAINER
docker stop -t 30 CONTAINER.  (30 seconds limit gracefully stop, then forcefully close)
docker stop CONTAINER1 CONTAINER2

Delete the container after stopped (not the image)

docker rm CONTAINER(S)
docker rm my-container (just remove the container after it has been stopped. Doesnot remove the image from the filesystem)
docker rm -f CONTAINER (force delete, even running ones)

Execute a command inside a running Docker container

docker exec CONTAINER COMMAND
docker exec my-container ls /etc/passwd
docker exec -t my-container ls /app (-t is for pseudo TTY, basically for text coloring/formatting)
docker exec -i my-container CMD (keep STDIN open for input, use as -it for interactive shell)
docker run -it /bin/bash (if a linux container)

Remove the docker image from filesystem

docker rmi IMAGE
docker rmi nginx:latest
docker rmi -f IMAGE

docker pull(if not available locally) + docker create + docker start

docker run IMAGE [COMMAND]
docker run -d nginx (-d : detached mode [in background])
docker run --name cont1 ubuntu:latest

run command and self-destruct container

docker run --rm ubuntu cat /etc/hosts

detailed low-level info about container,images,volumes, or networks in JSON

docker inspect OBJECT   (both on running and stopped container)
docker inspect my-container
docker inspect nginx

retrieve logs of a container

docker logs CONTAINER
docker logs -f CONTAINER  (live logs , NOT FORCE)

image history (image layers and details)

docker history IMAGE

build docker from Dockerfile

docker build PATH
docker build -t name .  (. is the PATH[or build context])

different name for Dockerfile
docker build -f PATH/TO/DOCKERFILE_ALTERNATIVE BUILD_CONTEXT_PATH

map host port to container port

docker run -p 80:80 IMAGE

login to registry

docker login REGISTRY

renaming existing docker image

docker tag SOURCE_IMAGE[:tag] TARGET_IMAGE[:tag]

docker tag SOURCE_IMAGE REGISTRY/USERNAME/REPO[:tag]

push to REGISTRY

docker push IMAGE REGISTRY/USERNAME/REPO[:tag]

Docker Network

list available docker networks

docker network ls

create custom network in docker

docker network create NETWORK_NAME

docker network create --subnet 10.0.0.0/16 NETWORK_NAME

inspect network in docker

docker network inspect NETWORK_NAME/ID

run a container in another network (by default the network is docker0)

docker run --network NETWORK_NAME IMAGE

add a network to an existing container

docker network connect NETWORK_NAME CONTAINER_NAME

disconnect a network from a container

docker network disconnect NETWORK_NAME CONTAINER_NAME 

Docker persistence

Two ways

  • Named Volumes
  • Bind mounting

Named Volumes

create docker volume

docker volume create VOLUME_NAME

list docker volumes

docker volume ls

inspect docker volume

docker volume inspect VOLUME_NAME

remove docker volume

docker volume rm VOLUME_NAME

launch a container with a volume attached

docker run -v [VOLUME_NAME]:[CONTAINER_DIRECTORY]
docker run -v mydata:/app/data ubuntu:latest

docker run -v [VOLUME_NAME]:[CONTAINER_DIRECTORY]:ro    (ro: read-only access for the container)

inherit all volume mounts from another container

docker run --volumes-from CONTAINER IMAGE_NAME
--volumes-from CONTAINER:ro

Bind Mounting

mount host path to container path

docker run -v /Users/USER/config:/app/config IMAGE

Docker Compose

File Structure

services:
  database:
    image: mysql
    container_name: mydatabase
    volumes:
      - /host/path:/container/path
      - volumeName:/container/path
      - :/container/path    (anonymous volume)

  frontend:
    build: .
    ports:
      - "3000:5000"

  backend:
    build:
      context: /usr/project/app
      dockerfile: Customdockerfile

volumes:
  volumeName:

services: define all the containers we need to create
build: built context path (for that service)

build images for the services

docker compose build

run one specific service in docker compose file

docker compose run <service-name>

build images and start containers

docker compose up

specify different compose file

doccker compose -f <file-name> up

stop and remove containers

docker compose down

stop and remove containers as well as volumes

docker compose down --volumes

stop and remove current compose containers and any old (orphan containers) no longer referenced by compose file

docker compose down --remove-orphans

start the stopped compose containers

docker compose start

stop(not destroy) the running compose containers

docker compose stop

Dockerfile

FROM BASE_IMAGE

LABEL maintainer="abik <abik@gmail.com>"

RUN apt-get update && apt-get install -y vim  (executes before container is created, i.e during image creation)

CMD ["/bin/bash"]   (executes during container runtime )

EXPOSE 80    (INFORM that port 80 should be exposed. DOESNOT ACTUALLY EXPOSE THAT PORT. JUST INFORMATIONAL)

COPY SRC DEST   (basically copy host file into IMAGE)

WORKDIR <PATH>      (define working dir  of docker container at any given time)

ENV key=value       (set env vars for docker container)
CMD ["echo", "$key"]
6 views