How to Run NGINX on Docker

Developed by Igor Sysoev in 2002, NGINX (pronounced “engine-x”) is an open-source and lightweight web server used to power websites and webpages. NGINX is capable of:

  • Reverse proxy
  • Load balancing
  • Mail proxy
  • HTTP cache

You can download NGINX here. Note that NGINX will be installed in /usr/local/nginx by default. The NGINX configuration file is located at /etc/nginx/ directory.

In this article, we walk through the steps to run NGINX on Docker, and how you can run NGINX as a Docker container.

Docker overview

Docker is a platform that enables system admins, developers, operational managers, and more to seamlessly deploy their applications at any moment in a consistent environment. It promises a simplified “Build, Ship, and Run” workflow. Docker supports NGINX, so to run an HTML file on Docker you need NGINX.

A Dockerfile is a text-based document that includes all the commands that can be called on the command line to put together an image. A Docker image is a read-only template that comprises a set of instructions to create a container so that it can run on the Docker platform.

Docker containers are units of software that bundle up code and all its dependencies so that the app can run promptly and accurately from one computing environment to another environment. Containers let developers direct their attention and efforts on application “content” as it separates applications from the hassles of infrastructure.

So, why use Docker? It minimizes the need to have more infrastructure resources during development.

Prerequisites for running NGINX on Docker

Before you run an NGINX Docker container, make sure you install the Docker engine and create a Docker Hub account.

1. Install Docker Engine

1. Go to the website and download the Docker installer. Visit this link to choose the option that suits you the best. If you use Ubuntu, view the tutorial to install Docker Engine.

2. Once downloaded, run the installer.

2. Create a Docker Hub account by visiting the Docker Hub signup page.

Docker

  1. Enter your username (this is also your Docker ID). A Docker ID can only contain lowercase letters and numbers.
  2. Add a valid email address.
  3. Enter a password. Passwords must be 6 - 128 characters long.
  4. Complete the captcha verification.
  5. Now, click the link in the email to verify your email address.

When done, this is what your screen will look like. Since you have already downloaded the docker installer, no need to repeat the process again.

How to run NGINX as a Docker container

If you want to run NGINX as a Docker container and expose it to your local network, here’s how to do it.

1. Use the default NGINX configuration with the following command. In order to launch an instance of NGINX running in a container and use the default NGINX configuration, run this command:

$ docker run --name mynginx1 -p 80:80 -d nginx

  • mynginx1: According to the NGINX image, this is the name of the container that is created.
  • -d: This specifies that the running container is in a detached mode, which means it will not respond to commands that are run on the command line.
  • -p: This option tells Docker to map out the ports that are exposed in the container by the NGINX image (container port 80) to the designated port on the Docker host.

The former parameter specifies the port within the Docker host, and the other parameter is mapped to the port that is exposed in the container.

You get this container ID: fcd1fb01b14557c7c9d991238f2558ae2704d129cf9fb97bb4fadf673a58580d.

This form of ID is used in the name of log files.

2. Next step is to verify whether the container has been created, and is running properly. To confirm that the container was successfully created and is running, and to see the port mappings, run docker ps. Here’s the command:

--CODE language-markup line-numbers--
$ docker ps
CONTAINER ID  IMAGE         COMMAND               CREATED         STATUS        ...  
fcd1fb01b145  nginx:latest  "nginx -g 'daemon of  16 seconds ago  Up 15 seconds ... 

  ... PORTS              NAMES
   ... 0.0.0.0:80->80/tcp mynginx1

There is a second way to verify whether NGINX is running: make an HTTP request to that port. Here’s the code that will appear:

--CODE language-markup line-numbers--
# curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
 body {
 width: 35em;
 margin: 0 auto;
 font-family: Tahoma, Verdana, Arial, sans-serif;
 }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="https://www.nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

To create a new container, refer to this tutorial. To create a new image, refer to this tutorial.

Use the docker stop command in case you want to stop one or more running Docker containers. You can do so by using the --force or -f flag along with the docker rm command.

Work as a Docker specialist using Upwork

Developing apps can be a complicated and time-consuming process since it’s more than just writing code.

Software engineers need to consider multiple languages, frameworks, and architectures to create web apps. Moreover, the complexities increase as they have to dabble between irregular interfaces and deploy different tools for different stages of app development. That is where Docker Specialists come in, as they help overcome such challenges.

In 2019 alone, there was a 50% increase in job postings for Docker Specialists. Clearly, software engineers with Docker skills are a valuable asset to any company.  Whether you are a seasoned Docker specialist or wish to jump-start your career as one, Upwork lets you do it.

You can sign-up on Upwork and start working as a Docker Specialist today. Add your details or continue with Google, fill out your Upwork profile. Work as a Docker Specialist today!

asdassdsad
Projects related to this article:
No items found.

Author Spotlight

How to Run NGINX on Docker
The Upwork Team

Upwork is the world’s work marketplace that connects businesses with independent talent from across the globe. We serve everyone from one-person startups to large, Fortune 100 enterprises with a powerful, trust-driven platform that enables companies and talent to work together in new ways that unlock their potential.

Get This Article as a PDF

For easy printing, reading, and sharing.

Download PDF

Latest articles

X Icon
Hide