
"It works on my machine" is one of the oldest problems in software. Your app runs on your laptop but fails elsewhere because of a missing library or a different version of a language. Docker solves this by packaging an application together with everything it needs to run, so it behaves the same everywhere.
The idea of isolating programs is old: Unix gained chroot in 1979, and Linux later added namespaces and control groups, which isolate processes from each other. These features were hard to use until a company called dotCloud released Docker as open source in 2013. Docker made containers simple to build, share and start. In 2015 the Open Container Initiative was founded to define common formats, so Docker images also run on tools such as Podman and Kubernetes.
A container is not a virtual machine. A virtual machine runs a complete operating system of its own, while containers share the host's kernel, so they start in seconds and use far less memory.
On Windows and macOS, install Docker Desktop, which includes a small Linux environment (WSL 2 on Windows), the command-line tools and a dashboard. It is free for personal use, education and small businesses; larger companies need a paid subscription. On Linux, install Docker Engine from Docker's official repository. Check the installation with docker --version, then run docker run hello-world. A welcome message means your first container worked.
Try a real web server: docker run -d -p 8080:80 --name web nginx. Open localhost:8080 in your browser and you will see the nginx welcome page. The options mean:
Everyday commands include docker ps to list running containers (add -a to include stopped ones), docker logs web to read output, docker exec -it web sh to open a shell inside, docker stop web and docker rm web to stop and remove it, and docker images to list downloaded images.
For a small Python web app with app.py and requirements.txt, a Dockerfile might look like this, one instruction per line:
Build the image with docker build -t myapp . and run it with docker run -p 5000:5000 myapp. Copying the requirements file before the code is deliberate: Docker caches each layer, so dependencies are reinstalled only when that file changes. Add a .dockerignore file listing folders such as .git and node_modules to keep the image small.
Anything written inside a container disappears when you remove it. For databases and uploads, use a named volume: docker run -d -v dbdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=secret postgres:17. The data in dbdata survives when the container is recreated. During development, a bind mount such as -v ./src:/app/src shares a folder from your computer, so code changes appear immediately.
Real projects often need an app and a database. Instead of long commands, describe them in a compose.yaml file: each service gets its image or build folder, ports, environment variables and volumes. The old version: line at the top is no longer needed. Start everything with docker compose up -d, follow output with docker compose logs -f and stop it with docker compose down. Services reach each other by name, so your app connects to a database at the host name db. Note that the current command is docker compose with a space; the older docker-compose program is retired.
Keep your Dockerfile and compose.yaml in Git with the rest of your code, so anyone can start the whole project with one command.
Once you are comfortable, containerize one of your own projects and run it with Compose. Many platforms that let you deploy a website for free can run a container image directly. Later, explore multi-stage builds and Kubernetes.
Grade 10 Math: Functions and Reasoning - Quiz
1170 questions