3 min read

Understanding Docker

Basics

In most of the challenges, you’ll typically see one or two main files:

  1. Dockerfile - This file contains Instructions to build a single container

  2. docker-compose.yml - The file contains Instructions to build multiple containers that work together

đź’ˇ

When to use which file? Always use docker-compose.yml file if given.

Install

If you haven’t installed docker follow these instructions.

add gpg keys of docker in you apt package-manager and update it.

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

install docker and its necessary plugins

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

pull run container

sudo docker run hello-world

For challenges with a docker-compose.yml file:

# Build the containers (the --no-cache makes sure everything is fresh)
docker compose build --no-cache

# Start the containers in the background
docker compose up -d

# See all running containers
docker ps -a

# When you're done, shut everything down
docker compose down -v

For challenges with a Dockerfile file:

# Build the container
docker build -t challenge-name .

# Run the container (maps port 8080 on your computer to port 80 in the container)
docker run -d -p 8080:80 --name challenge-name-container challenge-name

# Stop the container when done
docker stop challenge-name-container

# Remove the container
docker rm challenge-name-container

Debugging

I’ve encountered various problems and learned useful commands along the way:

Always make sure to add sudo before commands, otherwise you might get “permission denied” errors.

To see container logs:

docker logs challenge-name-container
ℹ️

challenge-name is the Docker image (template), while challenge-name-container is the running instance created from that image. You can create multiple different containers from the same image, each with its own settings.

Cleanup

When you want to cleanup everything - containers, networks, volumes, use the following command:

docker system prune -af --volumes

References

Additional Resources