1. Overview

In this brief tutorial, we’ll discuss how we can run the Nginx server inside a Docker container.

First, we’ll install and run Nginx by creating a Docker image from scratch using a Dockerfile. Afterward, we’ll create a simple Docker container from the official Nginx Docker image.

2. Creating an Nginx Image From Scratch Using a Dockerfile

Nginx is a powerful, advanced web server we can use to serve a variety of content, such as static web pages and APIs. In this section, we’ll begin by creating a new Dockerfile with the required instructions for building and running the Nginx image.

2.1. Initial Setup

First, let’s create a new directory and touch the Dockerfile:

$ mkdir server
$ touch Dockerfile

Now, we’re ready to populate our Dockerfile. Usually, the first command in the Dockerfile instructs Docker to create an image based on another image:

FROM ubuntu

Our Docker image will now be based on the minimal Ubuntu image, which is available on Dockerhub. The preceding commands in the Dockerfile will be layered on top of the base image.

2.2. Installing Nginx

In the next line, we’re going to tell Docker to install the nginx package from the official package repository using apt:

RUN apt-get -y update && apt-get -y install nginx

The command first tries to update the package repository. Once that’s successful, it installs the nginx package.

2.3. Configuration File

Nginx does come with a default configuration file. However, when we have our own Nginx configuration file, it’s much easier to maintain it outside of a Docker container. Therefore, we are going to create a default config file for Nginx in the same directory as the Dockerfile:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    
    root /usr/share/nginx/html;
    index index.html index.htm;

    server_name _;
    location / {
        try_files $uri $uri/ =404;
    }
}

Let’s add an entry in the Dockerfile for copying our config file:

COPY default /etc/nginx/sites-available/default

Now, every time we build our Docker image, Docker copies the config file to the target directory.

2.4. Exposing Ports

Next, we’ll instruct the system to expose the port on which we’ll access our server. In our case, it’s going to be port 80 using TCP:

EXPOSE 80/tcp

2.5. Running Nginx

Now, we are going to run Nginx whenever our Docker image launches:

CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
  • the -g option specifies a directive to nginx
  • the daemon off directive will disable the self-daemonizing behavior of nginx

We should note the ; at the end of the directive. Not specifying it might lead to unexpected problems.

With the final command, our whole Dockerfile now looks something like this:

# Pull the minimal Ubuntu image
FROM ubuntu

# Install Nginx
RUN apt-get -y update && apt-get -y install nginx

# Copy the Nginx config
COPY default /etc/nginx/sites-available/default

# Expose the port for access
EXPOSE 80/tcp

# Run the Nginx server
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]

2.6. Building the Image

Now, our Dockerfile is ready to be processed. We are going to use the docker command to build our image:

$ docker build . -t haidar/server

Let’s break this down:

  • build creates an image out of the Dockerfile in the current directory
  • -t gives a tag to our image so it can be easily identified

Once we run the command, Docker will run each command in the Dockerfile in sequence.

2.7. Running the Image

Once our image builds successfully, we are going to take it for a test run.

Let’s issue the following command to run the server:

$ docker run -d -p 80:80 haidar/server

Let’s break this down:

  • the run sub-command specifies that we want to run the container
  • the -d flag indicates that we want to run the image in detached mode
  • the -p option signifies the port number in the format local-port:container-port. In this case, we are mapping port 80 in the container to port 80 on the server
  • the final argument nginx specifies the tag of the image we want to run

Let’s fire up a browser and check our newly-built server:

Nginx Running in Docker

3. Running Nginx Using the Official Nginx Image

Most Linux distributions don’t ship with Nginx by default. So, we’ll need to install Nginx using a package manager or build it from its source.

Not only that, but we’ll also need to maintain Nginx when there’s a new release. Thus, it can become an overhead to update Nginx either with a package manager or build it from the source.

For that reason, when we containerize Nginx from the official Nginx image, we reduce the maintenance overhead. The Docker container will be updated or replaced when a new version of Nginx is released.

3.1. Creating a Docker Container for Nginx

The official Nginx image is available on Dockerhub, so we don’t have to worry about building our own image for Nginx.** We can simply pull the image:

$ docker pull nginx

The command installs all the necessary components for us. Besides, the image also comes with preset configurations for running the server.

3.2. Running the Image

Let’s take the Nginx Docker container for a test run:

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

We saw these options in the previous section. However, the –name option is used to give a name to the container—a random name if not specified.

We’re done at this point. Let’s see the status of our Docker image:

$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS                               NAMES
816532f135c8   server    "/docker-entrypoint.…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx

4. Conclusion

In this article, we saw how we could create an Nginx container from scratch using a Dockerfile. Apart from that, we also discussed how to pull and run the official Nginx Docker image.