Docker packages an application together with everything it needs, so it runs the same on your laptop, a test server and production. This guide takes you from zero to a running container and your first custom image. You need Docker installed (Docker Desktop on Windows and macOS, or Docker Engine on Linux).
Images and containers in one minute
- An image is a read-only template: the application plus its files and settings.
- A container is a running instance of an image. You can start many containers from one image.
Think of the image as a recipe and the container as the dish.
Step 1: Check that it works
docker run hello-world
Docker downloads a tiny image, runs it and prints a welcome message. If you see it, the installation is fine.
Step 2: Run a real web server
docker run -d --name web -p 8080:80 nginx
-druns it in the background.--name webgives the container a friendly name.-p 8080:80maps port 8080 on your machine to port 80 inside the container.
Open http://localhost:8080 in your browser and you will see the nginx welcome page.
Step 3: Look inside
docker ps # running containers
docker logs web # what nginx printed
docker exec -it web sh # open a shell inside the container
exit # leave the shell
Step 4: Serve your own files
A volume shares a folder from your machine with the container. Create a folder with an index.html, then:
docker rm -f web
docker run -d --name web -p 8080:80 -v "$PWD":/usr/share/nginx/html:ro nginx
Refresh the page: it now shows your file. On Windows PowerShell use ${PWD} instead of "$PWD".
Step 5: Build your own image
Create a file named Dockerfile next to your index.html:
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
Then build and run it:
docker build -t my-site .
docker run -d --name my-site -p 8081:80 my-site
Your site is now a self-contained image you can push to a registry and run anywhere.
Clean up
docker stop web my-site
docker rm web my-site
docker system prune # removes unused containers, networks and dangling images
Data written inside a container is lost when the container is removed. Anything that must survive (databases, uploads) belongs in a volume.
Where to go next
- Learn Docker Compose to run several containers (app, database, cache) from one file.
- Use small base images such as
alpineto keep images fast to download. - Never bake passwords into an image. Pass them at run time with
-eor a secrets file.
