CLOUDLAUNCH/LAB

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.

1

Verify Docker

Check the installation, daemon, containers, and images.

Terminal
docker --version
docker info
docker ps
docker images
2

Create the project

Make a folder and add an index.html file.

Terminal
mkdir docker-demo
cd docker-demo
index.html
<!DOCTYPE html>
<html>
<head><title>Docker Demo</title></head>
<body>
  <h1>Hello from Docker!</h1>
  <p>My first Docker application.</p>
</body>
</html>
3

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.

Dockerfile
FROM nginx:latest

COPY index.html /usr/share/nginx/html/index.html

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
4

Build the image

Run this inside the project directory and verify it appears in the image list.

Terminal
docker build -t docker-demo:v1 .
docker images
5

Run the container

Map host port 8080 to Nginx port 80, then open http://localhost:8080.

Terminal
docker run -d -p 8080:80 --name my-container docker-demo:v1
docker ps
6

Push to Docker Hub

Create a Docker Hub repository, sign in with an access token, tag the image with your username, and push it.

Terminal
docker login
docker tag docker-demo:v1 YOUR_DOCKERHUB_USERNAME/docker-demo:v1
docker push YOUR_DOCKERHUB_USERNAME/docker-demo:v1
7

Pull and run on another machine

On another computer or EC2 instance, pull the published image and run it on port 8080.

Terminal
docker pull YOUR_DOCKERHUB_USERNAME/docker-demo:v1
docker images
docker run -d -p 8080:80 --name pulled-container YOUR_DOCKERHUB_USERNAME/docker-demo:v1
Practical exercise: create the HTML app, build version v1, 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.
Cleanup
docker stop my-container
docker rm my-container
docker rmi docker-demo:v1