Containers and images
What a container is and how to run one.
A container is an isolated process that carries everything it needs: code, runtime and libraries. It starts in seconds and behaves the same on your laptop and on a server. It is not a full virtual machine: containers share the host kernel.
- Image: a read-only template (for example
nginxorpython:3.12). - Container: a running instance of an image.
- Registry: where images are stored, such as Docker Hub.
Run your first container
docker run hello-world # downloads the image if needed, runs it, exits
docker run -d -p 8080:80 --name web nginx # run in background, map host port 8080 to container port 80
docker ps # running containers
docker ps -a # including stopped ones
docker logs web # output of the container
docker stop web && docker rm web # stop and remove itAfter the nginx command, open http://localhost:8080 to see the web server. -p HOST:CONTAINER maps ports.
Images
docker pull python:3.12 # download an image
docker images # list local images
docker rmi python:3.12 # remove an imageNoteAlways use a specific tag such as python:3.12 instead of relying on latest, so builds are repeatable.
Try it
Test yourself
Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.