With Docker installed, this hands-on walkthrough covers the core workflow you'll use constantly: pulling images, running containers, checking logs, and managing their lifecycle — using a real Nginx web server as the example.
Prerequisites
- Docker Engine installed and running
- Root or sudo access, or your user added to the
dockergroup
Step 1 — Pull an Image
docker pull nginx:latest
List downloaded images:
docker images
Step 2 — Run Your First Container
docker run -d \
--name webserver \
-p 80:80 \
--restart unless-stopped \
nginx:latest
-d runs it in the background, --name gives it a memorable name, -p 80:80 maps the host's port 80 to the container's port 80, and --restart unless-stopped ensures it survives reboots.
Step 3 — Verify It's Running
docker ps
Visit http://YOUR_SERVER_IP in a browser to see the default Nginx page.
Step 4 — View Container Logs
docker logs webserver
Follow logs live:
docker logs -f webserver
Step 5 — Open a Shell Inside the Container
docker exec -it webserver bash
exit
Step 6 — Stop, Start, and Restart
docker stop webserver
docker start webserver
docker restart webserver
Step 7 — Serve Your Own Files
Mount a local directory into the container instead of the built-in default page:
mkdir ~/website
echo "<h1>Hello from my VPS</h1>" > ~/website/index.html
docker run -d \
--name mysite \
-p 80:80 \
-v ~/website:/usr/share/nginx/html:ro \
nginx:latest
Step 8 — Remove a Container
docker stop webserver
docker rm webserver
Common Errors
"Port is already allocated" — another process or container is using port 80:
sudo ss -tulpn | grep :80
Use a different host port instead, e.g. -p 8080:80.
Container exits immediately — check the logs for the actual error:
docker logs webserver
Best Practices
- Always name your containers with
--namefor easier management - Use volumes for anything that needs to persist beyond the container's lifecycle
- Prefer official images from Docker Hub over unverified third-party sources
FAQ
What happens to data if I delete a container?
Any data not stored in a mounted volume is lost — see Docker Volumes: Persisting Data for how to avoid this.
Related Articles
- How to Install Docker Engine on Ubuntu & Debian
- Docker Volumes: Persisting Data
- Docker Networking Explained
