Skip to content

Docker Cheat Sheet

Images

Build image

1
2
3
docker build .
docker build -t {tag_name} .
docker build -t {tag_name} Dockerfile

List all images

1
docker images

Remove image

1
docker rmi {image_name:tag_name}

Download image

1
docker pull {image_name}

Containers

List containers

1
2
docker ps # show running containers
docker ps -a # show all container

Get logs

1
docker logs [-f/--follow] {container}

Delete container

1
docker rm {container}

Run container

1
2
3
4
5
docker run {image}
docker run -ti ubuntu:latest # -ti = terminal keyboardInteractive
docker run -d -ti ubuntu bash # -d : (detach) run docker process in background
docker run -e "DB_HOST=db" -e "DB_PORT=3306" -e "DB_USER=wikijs" -e "DB_PASS=wikijsrocks" image_name
docker run --name {container_name} -p {host_port}:{container_port} -v {/host_path}:{/container_path} -it {image_name} /bin/bash

docker run command = docker create command + docker start command

1
2
docker create ubuntu:latest
docker start {container}

Inspect container

1
docker inspect {container}

Stop container

1
docker stop {container}

Convert detach mode to attach mode

1
docker attach {container}

Remove stopped contains

1
docker system prune # Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.

Executing Commands

1
docker exec -it {container} sh

Copy file from host to container

1
docker cp foo.txt mycontainer:/foo.txt

Copy file from container to host

1
docker cp mycontainer:/foo.txt foo.txt

For more information, please refer here