DOCKER - 7 Nginx v2 Web Server
In this lesson we clarify the difference between images and containers, then jump straight into running, stopping, deleting and inspecting a real container. An image is the bundle of binaries, libraries and source code that makes up your application. A container is a running instance of that image — and you can run many containers from the same image. We get our images from registries, the container equivalent of GitHub for source code. Docker's default registry is Docker Hub at hub.docker.com.
Running and managing an Nginx container
Let's launch our first container with the open-source Nginx web server:
docker container run --publish 80:80 nginx
Opening localhost in a browser and refreshing a few times prints log entries in the terminal. Behind the scenes, the Docker engine looked for an image named nginx, pulled the latest tag from Docker Hub, then started a new process inside a new container. The --publish 80:80 flag forwards traffic from host port 80 to container port 80, where Nginx listens by default. Because we ran it in the foreground, Ctrl+C stops the process.
To run in the background, add --detach. Docker prints the unique container ID and returns the prompt. List active containers with docker container ls, and add -a to see stopped ones too. Stop a container with docker container stop <id> — you only need the first few characters of the ID, as long as it's unique.
docker container run --publish 80:80 --detach --name webhost nginx— named, detacheddocker container logs webhost— tail the latest logsdocker container top webhost— list processes inside the containerdocker container rm <id> <id> <id>— bulk deletedocker container rm -f <id>— force-delete a running container
Deleting a running container fails by design (a safety net): you must either stop it first or pass -f to force the removal. After cleanup, docker container ls -a returns empty. In just a few commands we downloaded an Nginx image, ran multiple containers, inspected their logs, and tore everything down cleanly.
Summary
This Docker lesson introduces the essential difference between Docker images and containers, then demonstrates how to create, manage, and remove Nginx web server containers. Participants learn practical Docker commands to launch an Nginx web server, map ports, list running containers, and clean up resources. By the end, students have hands-on experience running a fully functional web server through just a few Docker commands.
Key points
- Images are static packages containing binaries, libraries, and source code; containers are running instances of those images
- Docker registries (like Docker Hub) store container images, similar to how GitHub stores code repositories
- The `docker run` command combined with port mapping (`-p 80:80`) enables local traffic to reach the Nginx server running inside the container
- Use `docker ps` to list running containers and `docker ps -a` to view all containers including stopped ones
- `docker container stop` gracefully halts a running container, while `docker rm -f` forcefully removes containers even if running
- Multiple containers can be created from a single image, and all containers can be cleaned up at once using bulk commands
FAQ
What is the difference between a Docker image and a container?
An image is a static package containing all the files, libraries, and code needed for an application. A container is a running instance of that image—you can have multiple containers from the same image simultaneously.
Why do we need port mapping when running Nginx in Docker?
Port mapping connects a port on your local machine to a port inside the container. For example, `-p 80:80` forwards traffic from local port 80 to port 80 inside the container where Nginx listens, making the web server accessible from your browser.
How do I remove all Docker containers at once?
Use `docker ps -a` to list all containers, then run `docker rm -f <container-id>` for each one. If a container is still running, the `-f` (force) flag allows deletion without stopping it first. Alternatively, you can list their IDs and remove them in a single command.