Skip to content

The Docker cheat sheet

Published: at 12:00 AM

Useful cheatsheet for managing the docker container, image and volume,.. etc.

Table of contents

Open Table of contents

Manage Container

Start a container in background.

docker run -d IMAGE_NAME

Start an interactive container.

docker run -it IMAGE_NAME bash

Export port from a container.

docker run -p 80:80 -d IMAGE_NAME

Start a named container.

docker run --name my-app IMAGE_NAME

Restart a stopped container.

docker start my-app

Stop a container.

docker stop my-app

Add metadata to container.

docker run -d --label com.example.foo=bar IMAGE_NAME

List running containers.

docker ps

List all containers ( running & stopped ).

docker ps -a

Inspect containers metadatas.

docker inspect INSTANCE_ID

Remove all stopped containers.

docker rm $(docker ps --filter status=exited -q)

List all containers with a specific label.

docker ps --filter label=com.example.foo

Query a specific metadata of a running container.

docker inspect -f '{{ .NetworkSettings.IPAddress }}' INSTANCE_ID

Manage Image

Build an image from Dockerfile in current directory.

docker build --tag IMAGE_NAME .

Force rebuild of Docker image.

docker build --no-cache .

Convert a container to image.

docker commit INSTANCE_ID IMAGE_NAME

Remove all unused images.

docker rmi $(docker images -q -f "dangling=true")

Manage Volumes

Create a local volume.

docker volume create --name VOLUME_NAME

Mounting a volume on container start.

docker run -v VOLUME_NAME:/path/to/ IMAGE_NAME

Remove a volume.

docker volume rm VOLUME_NAME

List volumes.

docker volume ls

Manage docker network

Create a local network.

docker network create NETWORK_NAME

Attach a container to a network on start.

docker run -d --net NETWORK_NAME IMAGE_NAME

Connect a running container from a network.

docker network connect NETWORK_NAME INSTANCE_ID

Disconnect container to a network.

docker network disconnect NETWORK_NAME INSTANCE_ID

Debug the running container

Run another process in running container.

docker exec -it INSTANCE_ID bash
docker exec -it my-app bash

Inspect the live logs of running daemon container.

docker logs -f INSTANCE_ID
docker logs -f my-app

Show exposed ports of a container.

docker port INSTANCE_ID
docker port my-app