Docker Basics: How to Deploy NGINX in a Docker Container

Brandi McCall
5 min readFeb 27, 2023

This tutorial reviews basic Docker commands and how to start NGINX in a Docker container. These notes are paraphrased from Bret Fisher’s Udemy course “Docker Mastery: with Kubernetes +Swarm from a Docker Captain” which can be found here.

Prerequisites:

  • Docker Desktop installed
  • Access to CLI

Install Docker Desktop

If you do not yet have Docker Desktop installed, click the appropriate link for your local machine and follow the installation instructions:

Install Docker Desktop on Linux

Install Docker Desktop on Windows

Install Docker Desktop on Mac

Basic Docker Commands

The following commands will give you information about the Docker version running on your machine, configuration details, and available commands you can run in your CLI.

docker version Tells the version of Docker running on your local machine

docker info Tells detailed config info about the Docker engine

docker Lists available commands

Image vs. Container

Docker image — read-only immutable template that contains the binaries, libraries, and source code that serve as a set of instructions for how to create a container

Docker container — a running instance of a Docker image

Start a Docker Container that Runs an NGINX Web Server

When using the docker container runcommand, the Docker engine first looks for the specified image in a local image cache. If it does not find the image, it then looks for it in Docker Hub, the official cloud-based Docker image registry. Once it finds the image in Docker Hub, it downloads the latest version of the image to the local machine (unless a different version is otherwise specified in the CLI command). It creates a new container based on that image, and starts the container. The container listens for traffic on a specified port, and forwards that traffic to a specified port.

To create a Docker container that runs NGINX, use the following command in your CLI:

docker container run --publish 80:80 nginx

In this command, we are telling Docker to run or start a container from the NGINX image that listens for traffic on port 80 and forwards that traffic to port 80 in the container. After the command is finished running, open a web browser and type in “localhost” then press enter. You should see the NGINX landing page.

So what happened here? We requested to run a container with NGINX. The Docker engine searched for and found an NGINX image from Docker Hub, it pulled that image to your local host, and started a new container from it. This container is running in the foreground.

Stop a Docker Container from Running in Foreground

To stop the NGINX web server from running in the foreground, use ctrl +c in your CLI.

Start a Docker Container that Runs an NGINX Web Server in the Background

Now we want to run NGINX in the background. To do this, we will add --detach to the run command.

docker container run --publish 80:80 --detach nginx

The output of this command will be a unique container ID.

List All Running Containers

docker container ls Lists all running containers

Notice under “NAMES” in our container list there is a randomly generated container name, since we didn’t specify a name when we created the container.

List All Containers

docker container ls -a Lists all containers, whether running or not

Stop a Container

docker container stop <container id> Stops the container indicated by container ID (only have to put the first few characters of the container, not the entire ID)

Create Container With a Specific Name

To give your container a specific name upon creation, add --name to the command:

docker container run --publish 80:80 --detach --name <name_of_container>

You can then use docker container ls -a to see the container you created with a specific name (mine is called “webhost”) and the containers you created earlier that were assigned random names (“stoic_kalam” and “clever_tesla”).

Container Logs

To view container logs, use the following command:

docker container logs <name_of_container>

View Processes Running Inside a Container

To see processes running inside a specified container, use the following command:

docker container top <name_of_container>

Remove Containers

To remove the containers we just created, we first need to make sure the containers are stopped, as you cannot remove a running container. To stop the containers, use the docker container stop command and include the first few unique characters of the containers you want to stop.

Now use the docker container rm command and specify the first few unique characters of the containers you want to remove. If you do docker container ls -a after removing the containers, you will see that there are no containers remaining.

This was a very quick tutorial on basic Docker commands. Thank you for reading!

--

--