Chapter 11 · Docker container lab
Build, publish, and run a Docker image
A student-friendly Docker workflow: create an HTML app, package it with Nginx, run it locally, publish the image to Docker Hub, then pull and run it on another machine or EC2.
Verify Docker
Check the installation, daemon, containers, and images.
docker --version
docker info
docker ps
docker imagesCreate the project
Make a folder and add an index.html file.
mkdir docker-demo
cd docker-demo<!DOCTYPE html>
<html>
<head><title>Docker Demo</title></head>
<body>
<h1>Hello from Docker!</h1>
<p>My first Docker application.</p>
</body>
</html>Create the Dockerfile
Create a file named exactly Dockerfile, without an extension. FROM selects the base image, COPY adds the app, EXPOSE documents the port, and CMD starts Nginx.
FROM nginx:latest
COPY index.html /usr/share/nginx/html/index.html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]Build the image
Run this inside the project directory and verify it appears in the image list.
docker build -t docker-demo:v1 .
docker imagesRun the container
Map host port 8080 to Nginx port 80, then open http://localhost:8080.
docker run -d -p 8080:80 --name my-container docker-demo:v1
docker psPush to Docker Hub
Create a Docker Hub repository, sign in with an access token, tag the image with your username, and push it.
docker login
docker tag docker-demo:v1 YOUR_DOCKERHUB_USERNAME/docker-demo:v1
docker push YOUR_DOCKERHUB_USERNAME/docker-demo:v1Pull and run on another machine
On another computer or EC2 instance, pull the published image and run it on port 8080.
docker pull YOUR_DOCKERHUB_USERNAME/docker-demo:v1
docker images
docker run -d -p 8080:80 --name pulled-container YOUR_DOCKERHUB_USERNAME/docker-demo:v1v1, run it on port 8080, push it to Docker Hub, pull it on another machine, and verify the same page loads. Never commit Docker Hub credentials or access tokens.docker stop my-container
docker rm my-container
docker rmi docker-demo:v1