Getting Started with NGINX on Docker


The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

NGINX is an open-source software that can be installed on any Linux server or in a virtual machine. NGINX has become the most popular web server for high traffic websites like Facebook and Twitter, but it’s also commonly used as proxy servers. This guide will show you how to install, configure, and manage NGINX with Docker containers from scratch.

In this article, we will be discussing how to get started with NGINX on Docker. This is a good place to start if you want to learn about how to use the software.

Getting Started with NGINX on Docker

Containerizing web server workloads like NGINX has become commonplace in today’s IT world. NGINX is ideal for container workflows, and integrating it with Docker is a natural step.

This video will show you how to create a new NGINX Docker container on any operating system, including Windows, macOS, and Linux!


Make sure you have the following items in order to follow along with this tutorial:

  • Docker Desktop 3.5.1 is used in this tutorial.
  • Windows 10 is a new operating system from Microsoft. The tutorial runs Docker on Windows, but the same fundamental techniques may be applied to Linux or macOS as well.
  • If you’re using Linux, you’ll need the Docker engine.

Creating a Docker Container for NGINX

Let’s begin by generating a Linux Docker image that already has NGINX installed. This tutorial will utilize Alpine Linux, a basic Linux distribution that runs the most recent NGINX version available, which is 1.21.1 in this case.

1. Open a terminal session in which you will perform all of the required Docker commands.

2. Using the mainlin-alpine branch, run the docker pull command to get the latest nginx image from the NGINX Docker Hub repository. Docker Hub is an open-source repository of Docker Images that Docker users may access.

nginx:mainline-alpine docker pull

nginx:mainline-alpine Docker image is being pulled. nginx:mainline-alpine Docker image is being pulled.

While retrieving the most recent image is helpful for development and testing, specifying a particular version, such as docker pull nginx:1.21.0-alpine, is frequently the best production practice.

3. Use the docker run command to start a container based on the nginx:mainline-alpine image.

  • -i — This option activates an interactive console that displays internal container output to the terminal.
  • -t — This option requests a Pseudo-TTY terminal for the operating container, allowing you to quit the interactive container with the key command ctrl-c.
  • —name – For each new container, the name of this container run must be different (unless prior containers of the same name are removed).
  • -p – Which internal to external port to use, in this example 80 for the internal port and 80 for the exterior port.
  • “image name” – The Docker image to use to provision the container, in this case, the nginx:mainline-alpine image that was previously pulled.

nginx:mainline-alpine docker run -i -t —name nginx-mainline -p 80:80

Interactively start the NGINX container.Interactively start the NGINX container.

4. Go to http://localhost in a web browser to access the running Docker container. If everything went well, you should see the default NGINX welcome page. NGINX should be accessible because you launched the container mapping the external port 80 to the container’s port 80 (-p 80:80) in the previous step.

Demonstrating the availability of the NGINX welcome page on an external system.Demonstrating the availability of the NGINX welcome page on an external system.

5. Finally, open your command line prompt and leave the operating container by using the ctrl-c key combination. You still have a lot of work ahead of you!

Website Files to a Container Mapping

You may now quickly start an NGINX Docker container using the example webpage. Your unique website must now be uploaded to NGINX. You must give a website source outside of the Docker image since containers are immutable and will remove any modifications when restored.

How to Create (and Manage) Docker Volumes on Windows is a related article.

The /usr/share/nginx/html directory is where the NGINX web server on Linux keeps website files. Rather than uploading files to this directory directly, you should map a storage location to it, and the container will grab those files from the storage location when it boots up.

On your local host operating system, do the following:

1. Create a directory that will be used to map to the NGINX Docker container. C:ArticlesNGINX is used to map to /usr/share/nginx/html in this case.

2. In the newly formed directory, create a file called index.html with the following content. This is the file that NGINX will serve up when you visit the website.

<html> <head></head> <body> <h1>ATA Test!</h1> <p>Welcome to your custom NGINX index file.</p> </body> </html>

3. Invoke docker run on the command line with almost the same arguments as step three in the previous section. Include the volume argument, -v, this time, as indicated below.

The -v argument maps the local C:ArticlesNGINX directory to the image’s /usr/share/nginx/html directory in the example below. Modifying the contents of the C:ArticlesNGINX directory will enable you to edit the contents of the /usr/share/nginx/html container directory.

This is a Windows file location, but the command works in Linux as well. -v /usr/share/myfiles:/usr/share/nginx/html, for example.

nginx:mainline-alpine docker run -i -t -v c:ArticlesNGINX:/usr/share/nginx/html —name nginx-mainline -p 80:80

Parameter mapping in VParameter mapping in V

4. Finally, open a web browser and type http://localhost into the address bar. Because it is now pulling files from the local C:ArticlesNGINX directory, you will see the following displayed based on the newly produced index.html file.

The mapped index.html file is shown.The mapped index.html file is shown.

Using a Docker NGINX Container to Run a PHP-FPM Application

You may now simply edit the contents of a website using an NGINX Docker container. This could work if you just need a very simple setup. However, if you want to run a web application on NGINX, you’ll have to put in a bit more effort.

Let’s use the PHP FastCGI Process Manager to enable the NGINX image we just created to run the popular web scripting language PHP (FPM).

Several new topics must be presented in this section. A Docker Compose file is the easiest way to quickly define a group of containers and their connections. In a YAML (Yet Another Markup Language) file, Docker Compose describes a set of services and linked apps.

Everything You Need to Know About Docker Compose is Related

A special version of the nginx:mainline-alpine image is also required since a custom configuration file for the default site must be copied into the NGINX container. A dockerfile is used to augment an existing image and is created using the build step in the Docker Compose file.

1. Make a directory in which your configuration files will be stored. The directory C:ArticlesNGINXPHP is used in this example.

2. First, create a file called dockerfile with the contents shown below.

FROM nginx:mainline-alpine # The image to grab the basic configuration from # Any extra files will be referenced from this directory. C:ArticlesNGINXPHP # WORKDIR COPY./default.conf /etc/nginx/conf.d/default.conf COPY./default.conf /etc/nginx/conf.d/default.conf COPY./default.conf /etc/nginx/conf.d/default.conf COPY./default.conf /etc/nginx/conf.d/default.conf COPY./default.conf /etc/ngin

3. Next, construct the docker-compose.yml file, which should include the following information.

# The docker-compose specification version is “3.9.” # This service is made up of the following applications: The NGINX custom container web is as follows: # Instead of referring image: nginx:mainline-alpine, use build to # refer to the current directory (. ), which will search for a dockerfile by default build:. # The external directory path that will be mapped to internal volumes: NGINXPHP:/usr/share/nginx/html – C:ArticlesNGINXPHP:/usr/share/nginx/html # Ports that map external ports to internal port mapping ports: “80:80” – image: php:fpm-alpine php:fpm-alpine php:fpm-alpine php:fpm- # Both containers must be able to refer to the same file volumes: NGINXPHP:/usr/share/nginx/html – C:ArticlesNGINXPHP:/usr/share/nginx/html

4. Finally, add the following to the default.conf NGINX configuration file.

server # The listen port, which must precisely match the internal volume share root /usr/share/nginx/html; # The root directory, which must exactly match the internal volume share root /usr/share/nginx/html; # Run the following command for all files with the PHP extension: /.+.php(/|$) # fastcgi pass php:9000; # Send the request to the host “php” and port 9000 (the default PHP-FPM port). # Define one more parameter instructing PHP-FPM where to locate the file fastcgi param SCRIPT FILENAME $document root$fastcgi script name; # Include the default NGINX FastCGI parameters include fastcgi params;

You may be wondering where the hostname “PHP” comes from. By default, the parent directory’s name is used to construct a network that is shared by all apps in the service. A hostname corresponding to the application name is allocated to each container. The network in this example is nginxphp, and the hostnames are web and php.

5. Navigate to your own Docker configuration file directory, which in this case is C:ArticlesNGINXPHP, in a terminal session. To construct and launch your own service, use the command docker-compose up.

Docker Compose starts the given group of containers.Docker Compose starts the given group of containers.

6. You’ll need a PHP file to verify the setup now that the containers are up and running. Create the index.php file in your NGINX Docker shared directory, which in this example is C:ArticlesNGINXPHP.

7. Open a web browser and go to http://localhost/index.php to make that the PHP file is shown correctly.

Checking the PHP fileChecking the PHP file


Docker NGINX containers are very handy for development and testing. Docker containers may also assist to considerably expand the capabilities of a production web server with correct care and attention!

Try adding a database container to the mix now that you’ve successfully deployed an NGINX container and a coupled PHP-FPM container for a complete web application environment.

The “docker nginx example” is a tutorial that explains how to get started with NGINX on Docker.

Frequently Asked Questions

Related Tags

  • nginx docker tutorial
  • dockerfile for nginx on ubuntu
  • nginx dockerfile example github
  • docker-compose nginx
  • nginx docker image

Table of Content