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.