Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

DK
Docker
0/10 completed
Beginner
Why containers?Install Docker and run your first containerImages vs containers
Intermediate
Writing a DockerfileVolumes and persistent dataNetworking and ports
Advanced
Docker ComposeMulti-stage builds and small images
Pro
Security and production best practicesRegistries and CI/CD
Tracks / Docker / Beginner

Install Docker and run your first container

1. Install Docker

  • Mac / Windows: install Docker Desktop from docker.com. It includes the engine, the CLI, Compose and a GUI.
  • Linux: install Docker Engine with your package manager (Docker's docs have one-liners for Ubuntu, Debian, Fedora and more). Then add yourself to the docker group so you don't need sudo:
bash
sudo usermod -aG docker $USER   # then log out and back in

Check that it works:

bash
docker --version
docker info

2. Hello, world

bash
docker run hello-world

Here's what just happened:

  1. Docker looked for the hello-world image locally and didn't find it.
  2. It pulled (downloaded) the image from Docker Hub.
  3. It created a container from the image and ran it.
  4. The container printed a message and exited.

3. Run a real web server

bash
docker run -d -p 8080:80 --name web nginx

Open http://localhost:8080 and you'll see the Nginx welcome page, served from a container.

Flag Meaning
-d Detached: run in the background
-p 8080:80 Map port 8080 on your machine to port 80 in the container
--name web Give it a friendly name (otherwise Docker picks a random one)
nginx The image to run

4. Manage containers

bash
docker ps              # running containers
docker ps -a           # all containers, including stopped ones
docker logs web        # see the output
docker logs -f web     # follow logs live (Ctrl+C to stop following)
docker stop web        # stop gracefully
docker start web       # start again
docker rm -f web       # remove (force-stops if running)

5. Go inside a container

bash
docker run -it --rm ubuntu bash
  • -it gives you an interactive terminal.
  • --rm deletes the container automatically when you exit.

You're now in a Ubuntu shell, even on a Mac or Windows machine. Try cat /etc/os-release, ls / or apt update. Type exit to leave. Nothing you did affects your computer.

You can also open a shell in an already-running container:

bash
docker run -d --name web nginx
docker exec -it web sh       # run a shell inside it

6. Run tools without installing them

Containers are a clean way to try languages and tools:

bash
docker run --rm python:3.12 python -c "print('Hello from Python in Docker')"
docker run --rm node:22 node -e "console.log(process.version)"
docker run --rm -it postgres:16 psql --version

Clean up

bash
docker container prune   # remove all stopped containers
docker image prune       # remove dangling images
docker system df         # how much disk Docker is using

Practice

  1. Run the httpd (Apache) image on port 9090 in the background with the name apache.
  2. View its logs, then open a shell inside it and find the index.html it serves (hint: /usr/local/apache2/htdocs).
  3. Stop and remove it.
✎ Check your understanding

1.What does -p 3000:80 do?

2.What does --rm do?

← Why containers?