HOWTO: Use Docker CLI

0) Start and setup ContainerD (to enable support for multiplatform builds):

colima start --runtime containerd

a) Build image:

docker build -t todo .

a) Build image for given platform:

docker build --platform linux/amd64 -t todo .

b) Start container:

docker run -d -p 127.0.0.1:3000:3000 todo

c) List containers (obtain container ID):

docker ps

d) Stop container with given ID:

docker stop <container_id>

e) Remove container with given ID:

docker rm <container_id>

f) Stop and remove container with given ID:

docker rm -f <container_id>

g) List Docker images:

docker image ls

h) Sign in to DockerHub:

docker login -u <dockerhub_username>

i) Give Docker image new name:

docker tag <local_image_name> <dockerhub_username>/<dockerhub_image_name>

j) Push Docker image to DockerHub:

docker push <dockerhub_username>/<dockerhub_repository_name>

k) Create (named) volume:

docker volume create <volume_name>

l) Start Docker container with (named) volume mount:

docker run -dp 127.0.0.1:3000:3000 --mount type=volume,src=<docker_volume_name>,target=<directory_inside_container> <container_name>

m) Inspect Docker (named) volume:

docker volume inspect <volume_name>

n) Start Docker container with bind mount:

docker run --it --mount type=bind,src="$(pwd)",target=/src ubuntu bash

o) Watch (container) logs:

docker logs -f <container-id>  // exit with Ctrl+C

p) Create Docker network:

docker network create <network_name>

q) Run Docker container and attach it to existing Docker network:

docker run -d --network <network_name> --network-alias <network_alias> \
  -v <volume_name>:<volume_mount_directory_inside_container> \
  -e MYSQL_ROOT_PASSWORD=<password> -e MYSQL_DATABASE=<db_name> mysql:8.0

r) Connect to database inside container:

docker exec -it <mysql_container_id> mysql -u root -p

s) Start container, connect it to given network:

docker run -it --network <network_name> <image_name>

t) Find out IP address of container with given network alias:

Start that container as in s). Execute 'dig <network_alias>'

u) Run application stack with Docker Compose:

Create compose.yaml file. Launch stack with Docker Compose:

docker compose up -d

v) View logs of application stack:

docker compose logs -f
docker compose logs -f <service_name>

w) Tear down Docker Compose application stack:

docker compose down OR
docker compose down --volumes  // to remove all volumes created

x) View layers in given image:

docker image history <image_name> OR
docker image history --no-trunc <image_name>  // to disable line truncation in output

https://docs.docker.com/get-started/workshop/02_our_app/