DOCKER - 7 Nginx v2 Web Server

Before we play with containers, it is important to distinguish between an image and a container. An image is the set of binaries, libraries and source code that compose your application. A container is a running instance of that image, and you can have many containers based on the same image. In this course we use the open-source nginx web server. Container images come from registries; the default one for Docker is Docker Hub (hub.docker.com), kind of what GitHub is for source code.

Let's launch our first container:

docker container run --publish 80:80 nginx

The Docker engine looked for an image named nginx locally, didn't find it, pulled the latest one from Docker Hub and started a process in a new container. The --publish 80:80 option maps host port 80 to container port 80, so visiting http://localhost in your browser hits the nginx container. To run it in the background, add --detach, which returns the unique container ID instead of streaming the foreground logs.

Managing containers

  • List running containers: docker container ls (only running) or docker container ls -a (all).
  • Stop a container: docker container stop <id-prefix>. A few unique characters of the ID are enough.
  • Name a container: docker container run --publish 80:80 --detach --name webhost nginx creates a container named webhost for easier reference.
  • Check logs: docker container logs webhost shows recent log output without attaching.
  • List processes: docker container top webhost shows the processes running inside the container.
  • Remove containers: docker container rm <ids>; running containers refuse to be deleted as a safety measure. Use -f to force-remove a running container.

In just a few commands we downloaded an image, ran several nginx containers on port 80, inspected their logs and processes, then cleaned everything up. That's the essence of the Docker workflow. See you in the next video.

Summary

This lesson explains the fundamental difference between Docker images (static bundles containing binaries, libraries, and source code) and containers (running instances of those images). Through practical demonstrations using Nginx web server, the lesson covers essential Docker operations including starting containers with port mapping, listing running and stopped containers, and removing containers. Viewers learn key Docker commands (docker run, docker ps, docker stop, docker rm) with real-world examples of running a web server on port 80 and managing multiple container instances.

Key points

  • Docker images are static bundles of binaries, libraries, and application code; containers are running instances of those images that can execute independently
  • Docker Hub registry functions as the central repository for pulling container images, similar to how GitHub serves source code repositories
  • Port mapping connects a port on your host machine to a container's internal port, enabling network traffic routing (e.g., localhost:80 to container:80)
  • The 'docker ps' command lists only running containers by default; use 'docker ps -a' to view all containers including stopped ones
  • Docker enforces safety by preventing deletion of running containers with 'docker rm'; use 'docker stop' first or force removal with 'docker rm -f'
  • Multiple independent containers can be launched from the same image, with each execution creating a fresh container instance

FAQ

What is the practical difference between a Docker image and a container?

A Docker image is a static collection of files, binaries, libraries, and source code that comprises your application. A container is a running instance created from that image. You can launch multiple containers from a single image, each operating independently with its own isolated environment.

Why is port mapping necessary when running a containerized web server?

Port mapping bridges your host machine's network interface to the container's internal network. When you map port 80 on your machine to port 80 in the container, web traffic arriving at localhost:80 is automatically routed to the Nginx server running inside the container, making the web server accessible through your browser.

How do you safely remove a Docker container?

First stop the container using 'docker stop [container-id]', then remove it with 'docker rm [container-id]'. Alternatively, use 'docker rm -f [container-id]' to force immediate removal. Docker prevents accidental deletion of running containers by default as a safety measure.