Docker

What are the Steps to Run Nginx in a Docker Container on Ubuntu 22.04?

In Docker, an Nginx image is a pre-built container image that runs the nginx web server software. It is an official image available and accessible on the Docker Hub. It can be used to serve dynamic or static web content or load balance traffic. Moreover, it can be customized by using different tags, environment variables, or configuration files.

This article will demonstrate the step-by-step procedure to run the Nginx in a Docker container on Ubuntu 22.04.

What are the Steps to Run Nginx in a Docker Container on Ubuntu 22.04?

To run the Nginx image in a container on Ubuntu 22.04, look at the following steps:

  • Launch Ubuntu Terminal
  • Pull the Nginx image through the “docker pull nginx” command.
  • Run the Nginx image as a container using the “docker run –name <container-name> -p <port-no> nginx” command.
  • Verify container status.

Step 1: Launch Ubuntu Terminal
From the Start menu, search for the “Ubuntu” terminal and run it as an administrator:

Step 2: Pull Nginx Image
Then, pull the nginx image from Docker Hub through the below-listed command:

docker pull nginx

Upon doing so, the nginx image will be downloaded to the local repository.

Step 3: Run Nginx Image in Docker Container
To run the Docker container using the “nginx” image, use the below-listed command:

docker run --name cont1 -p 80:80 nginx

Here:

  • The “–name” option is used for defining the container’s name which is “cont1” in our case.
  • The “-p” option is utilized to allocate the port i.e., “80:80”.
  • The “nginx” is the Docker image to build a container:

Moreover, the “-d” option can also be sued with the same command to run the container in a detached mode:

docker run --name cont1 -p 80:80 -d nginx

This will run the Nginx image as a container.

Step 4: Verify Container Status
To verify whether the Nginx image is running as a container or not, run the provided command:

docker ps

The below output indicates that the Nginx image is successfully running as a container on Ubuntu 22.04.

Finally, redirect to the allocated port and view the running container:

It can be seen that the Nginx image is successfully running as a Docker container.

Conclusion

To run the Nginx in a Docker container on Ubuntu 22.04, first, launch the Ubuntu terminal. Then, execute the “docker pull nginx” command to pull the Nginx image in the local repository. Next, run the Nginx image as a container using the “docker run –name <container-name> -p <port-no> nginx” command. Lastly, users can view the container status to verify whether the Nginx image is running as a container or not. This write-up has explained the procedure to run the Nginx in a Docker container on Ubuntu 22.04.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.