Docker for beginners: Run a college club webpage in a container
Build a tiny website image and learn how containers, ports, and restarts work.
Admin
FOSS Andhra community volunteer writing about free and open source software, education and developer tools from Andhra Pradesh.
Docker starts to make sense when you have something small to run. In this exercise, you'll package an HTML file with a web server, open it in a browser, and see what survives a restart.
Build the smallest useful app
With Docker running, create a folder. Save this as index.html:
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Campus FOSS Club</title>
<h1>Campus FOSS Club</h1>
<p>Bring your laptop to our next Linux workshop.</p>
</html>
In the same folder, create a file named Dockerfile:
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
Open a terminal in that folder and run:
docker build -t campus-foss-web .
docker run -d --name campus-foss-web -p 127.0.0.1:8080:80 campus-foss-web
Open the local webpage. The build produced an image containing Nginx and your HTML. Docker started a container from that image. The port mapping sends requests from port 8080 on your computer to port 80 inside the container. Binding to 127.0.0.1 makes the demo available on your machine. Docker container run reference
Stop it, restart it, and change it
docker stop campus-foss-web
docker start campus-foss-web
Refresh the browser after each command. The page becomes unavailable when the container stops and comes back when it starts. The container keeps its configuration and writable layer. After stopping it again, you can remove this practice container with docker rm campus-foss-web. The image remains available. Docker's first-container tutorial
Change the workshop sentence in index.html, rebuild the image, and create a fresh container. Restarting the old one would still serve the HTML copied into its original image.