Skip to content

Docker cheatsheet

List containers

List currently running containers

docker ps

List all containers

docker ps -a

List images

docker images

Starting containers

Start a container with an interactive shell

  docker run -ti <image_name> /bin/bash

Start a container in the background

  docker run -d <image_name>

Start a container and redirect local port to container port

docker run -p 4000:80 <image_name>

Attach to running container

Run shell in the container

docker exec -ti <container_name> bash

Copying files to/from container

Copy files/folders between a container and your host

docker cp foo.txt <container_name>:/foo.txt

Inspecting containers

Inspect a running container

  docker inspect <container_name> (or <container_id>)

Get the process ID for a container

docker inspect --format {{.State.Pid}} <container_name_or_ID>

List the current mounted volumes for a container

docker inspect --format='{{json .Volumes}}' <container_id> | python -mjson.tool

Save/load image

Save docker image as tarball

docker save ubuntu -o /tmp/ubuntu.tgz

Load tarball as docker image

docker load < ubuntu.tgz

Dockerfiles

Dockerfile examples

Using intermediate container for build

FROM ubuntu as intermediate

WORKDIR /app
COPY secret/key /tmp/
RUN scp -i /tmp/key build@acme/files .

FROM ubuntu
WORKDIR /app
COPY --from=intermediate /app .

Configuring docker client

Configure docker CLI to use proxy server

export http_proxy="http://127.0.0.1:8080"
export https_proxy="http://127.0.0.1:8080"

Configure Docker client to use remote host

export DOCKER_HOST=tcp://10.1.1.1:2376
docker ps

Docker daemon

Start docker daemon

docker -d

Docker API

List images

APIv1

http://host:5000/v1/search

with parameters

http://host:5000/v1/search?q=ubuntu&n=20&page=1

where

  • q - search term
  • n - number of results per page
  • page - page number

APIv2

http://host:5000/v2/_catalog?

Check status

http://host:5000/v1/_ping