Learning Docker

Core concepts

(git) repo -> (docker) image -> container (app)

CLI Commands

For all of the following commands, you have to first navigate to the directory that hosts either the Dockerfil or the docker-compose.yml files.

1. Images

1.1. Building

docker build .
# OR to tag an image
docker build -t author/repo:tag .

1.2. Listing

docker images
# OR
docker image ls

1.3. Removing

docker rmi $IMAGE_IDENTIFIER
# OR
docker image rm $IMAGE_IDENTIFIER
# OR to prune/remove unused images
docker image prune

Where $IMAGE_IDENTIFIER is either Image ID or image tag, i.e. author/repo:tag.

2. Containers

2.1. Running

This will essentially CREATE a new container off of an image. You can create multiple containers from a single image.

docker run $IMAGE_IDENTIFIER
# OR to name a container
docker run --name app $IMAGE_IDENTIFIER
# OR to forward the port to host
docker run --name app -p 4000:4000 $IMAGE_IDENTIFIER
# OR to create a volume from a host directory
docker run --name app -p 4000:4000 -v /absolute/path/to/host/dir:/absolute/path/to/docker/dir $IMAGE_IDENTIFIER

# OR to automatically remove the container after being stopped
docker run --name app -p 4000:4000 -v /absolute/path/to/host/dir:/absolute/path/to/docker/dir --rm $IMAGE_IDENTIFIER
# OR to run the container in detached/daemon (background) mode
docker run --name app -p 4000:4000 -v /absolute/path/to/host/dir:/absolute/path/to/docker/dir -d $IMAGE_IDENTIFIER

2.2. Listing

# To list running containers
docker ps
# OR to include stopped containers
docker ps -a

2.3. Viewing logs

docker logs $CONTAINER_IDENTIFIER
# OR to follow logs
docker logs $CONTAINER_IDENTIFIER --follow
# OR for short
docker logs $CONTAINER_IDENTIFIER -f

Where $CONTAINER_IDENTIFIER is either Container ID or randomly generated container name, i.e. scary_ship.

2.4. Stopping

Stopped containers are not entirely removed and they can be started again later, but their initialization commands are already executed.

docker stop $CONTAINER_IDENTIFIER

2.5. Starting

Note that contrary to docker run, in this command you are NOT creating a new image and that you are working with $CONTAINER_IDENTIFIER instead of $IMAGE_IDENTIFIER. Also note that docker start always starts the container in detached mode.

docker start $CONTAINER_IDENTIFIER

2.6. Removing

docker rm $CONTAINER_IDENTIFIER

3. Compose

3.1. Build and run resources

docker compose up
# OR to override the default .env file with another one
docker compose --env-file .development.env up
# OR to run in the detached/daemon (background) mode
docker compose up -d

3.2 View container logs

docker comopse logs
# OR to follow logs
docker compose logs --follow
# OR for short
docker compose logs -f

3.3. List images

docker compose images

3.4. List services

docker compose ls
# OR to list stopped services as well
docker compose ls -a

3.5. Stop resources

docker compose down
# OR to remove volumes as well
docker compose down -v

3.6. Remove resources

docker compose rm

Miscellaneous

Run something inside container

# For starting an interactive shell in Ubuntu flavors
docker exec -it $CONTAINER_IDENTIFIER /bin/bash
# OR for Alpine flavors
docker exec -it $CONTAINER_IDENTIFIER /bin/sh
# OR for shorter version
docker exec -it $CONTAINER_IDENTIFIER sh
# OR to run a specific command
docker exec -it $CONTAINER_IDENTIFIER psql

Volumes

# List volumes:
docker volume ls

# Inspect a volume:
docker inspect $VOLUME_IDENTIFIER

# Remove a particular volume:
docker volume rm $VOLUME_IDENTIFIER

# Prune/remove all unused volumes:
docker volume prune

Networks

# List networks:
docker network ls

# Inspect a network:
docker inspect $NETWORK_IDENTIFIER