Skip to main content

Docker - Setting NGINX

In the world of web development today, Docker - Setting NGINX gives us a strong way to set up and handle web servers. NGINX is famous for being fast and able to grow. We often use it as a reverse proxy and load balancer. This makes it a key tool in web setups. When we learn how to set up NGINX with Docker, we make our deployment better. This way, our applications can run well in separate environments.

In this chapter, we will look at the details of Docker - Setting NGINX. We will cover installation, network setup, and SSL setup. By the end, we will know how to create and manage NGINX containers. This will help us deploy web services better. For more information, you can look at our guides on Docker - Setting MongoDB and Docker - Hosting.

Introduction to NGINX

We know that NGINX is a fast web server. It also works as a reverse proxy, load balancer, and HTTP cache. It is made for speed and reliability. This makes it a great choice for serving static content and managing many connections at the same time. Unlike older web servers, NGINX uses an event-driven design. This helps it handle many requests well.

Here are some key features of NGINX:

  • Reverse Proxy: NGINX can send client requests to other servers. This helps with scaling and performance.
  • Load Balancing: It spreads incoming traffic across several servers. This way, no single server gets too busy.
  • SSL/TLS Support: NGINX can secure communications with HTTPS. This is important for keeping data safe while it travels.
  • Static File Serving: It is very good at serving static files fast. This is important for how quickly a website loads.

If we want to learn more about using NGINX with Docker, we can check out our guide on Docker - Setting NGINX. Setting up NGINX in a Docker environment makes it easier to deploy and scale web applications. If we are interested in improving our web server skills, we can look at our resources on Docker - Setting Node.js and Docker - Hosting.

Understanding Docker

Docker is a free platform. It helps developers automate how they deploy, scale, and manage applications. It uses lightweight and portable containers. Containers hold an application and what it needs to run. This makes sure everything works the same way in different places. This is very important for today’s application development. It lets us move smoothly from development to production.

Here are some key ideas about Docker:

  • Images: These are like read-only templates for making containers. An image has all we need to run an application. This includes code, libraries, and environment variables.
  • Containers: These are running instances of images. Containers do not interfere with each other or the host system. This helps with security and managing resources.
  • Dockerfile: This is a script that gives instructions for building Docker images. It sets up the environment our application needs. We can version-control it like any other code.
  • Docker Hub: This is a cloud place where we share Docker images. It makes it easy to find and use ready-made images for different applications.

We need to understand Docker for efficient Docker - Setting NGINX. It makes it easier to deploy and scale web servers like NGINX. If we want to learn more about Docker’s structure and its parts, we can visit What is Docker.

Installing Docker

To set up NGINX using Docker, we need to install Docker on our system. Docker works on many operating systems like Windows, macOS, and Linux. Let us see how to install Docker on these platforms.

For Windows:

  1. We download the Docker Desktop installer from the Docker Hub.
  2. We run the installer and follow the setup steps.
  3. After we install it, we open Docker Desktop and check if it is running.

For macOS:

  1. We download Docker Desktop from the Docker Hub.
  2. We drag the Docker icon to the Applications folder.
  3. Then we launch Docker from the Applications folder and make sure it is running.

For Linux (Ubuntu):

sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce

After we finish the installation, we verify if Docker is installed correctly by running:

docker --version

Now that we install Docker, we can go to Creating a Docker Network for NGINX or look at other Docker features.

Creating a Docker Network for NGINX

When we want our NGINX container to talk to other containers well, we need to make a special Docker network. This helps with easy communication and keeps our containers separate from others on the host.

  1. Creating the Network: We can create a Docker network called nginx-network by using this command:

    docker network create nginx-network
  2. Network Types: Docker has many network drivers. For NGINX, we usually use the bridge driver. This is the one that comes by default. We can check if the network is made by listing all networks:

    docker network ls
  3. Connecting Containers: When we start our NGINX container, we need to tell it to use the network with the --network option:

    docker run --name my-nginx --network nginx-network -p 80:80 nginx
  4. Benefits of Using a Custom Network:

    • Isolation: Our NGINX container stays separate from other Docker containers.
    • Service Discovery: We can find containers by their names in the same network.
    • Enhanced Security: It helps keep out unwanted traffic.

For more advanced networking ideas, we can look at Docker Networking. This setup is important for our Docker system when we host applications like NGINX.

Writing the NGINX Configuration File

We need to create an NGINX configuration file. This file is important for defining how our web server works and serves content. Usually, we find the configuration file in /etc/nginx/nginx.conf inside the container. But we can change it if we want.

Here is a simple example of an NGINX configuration file:

server {
    listen 80;  # Listen on port 80
    server_name localhost;  # Server name

    location / {
        root /usr/share/nginx/html;  # Document root
        index index.html index.htm;  # Default files
    }

    error_page 404 /404.html;  # Custom error page
    location = /404.html {
        internal;
    }
}

Key Directives Explained:

  • listen: This tells the port the server listens on.
  • server_name: This defines the domain name for the server.
  • location: This sets the URI location block and its settings.
  • root: This sets the document root for serving files.
  • error_page: This configures custom error pages.

We can make our NGINX configuration better by adding SSL settings or reverse proxy settings. If we want more advanced setups, we can use Docker Compose to manage multi-container applications easily. For more details, we can check our guide on Docker - Setting NGINX.

This configuration file is very important for our Dockerized NGINX setup to work well.

Dockerfile for NGINX

We need to create a Dockerfile for NGINX. This helps us build a custom NGINX image for our app. Here is a simple example of a Dockerfile for NGINX:

# Use the official NGINX image from the Docker Hub
FROM nginx:latest

# Copy the NGINX configuration file
COPY nginx.conf /etc/nginx/nginx.conf

# Copy your website content to the NGINX HTML directory
COPY html /usr/share/nginx/html

# Expose the port on which NGINX will run
EXPOSE 80

Explanation of the Dockerfile:

  • FROM nginx:latest: This line tells which base image to use. We use the official NGINX image.
  • COPY nginx.conf /etc/nginx/nginx.conf: This command puts your custom NGINX configuration file into the container.
  • COPY html /usr/share/nginx/html: This line moves your website files into the NGINX default HTML directory.
  • EXPOSE 80: This opens port 80, which is the normal port for HTTP traffic.

With this Dockerfile, we can build our NGINX Docker image by using this command:

docker build -t my-nginx-image .

This setup makes it easy to deploy and manage our NGINX server in a Docker container. For more information about Docker and NGINX, we can check out Docker - Setting a Web Server and Docker - Working with Containers.

Building the NGINX Docker Image

Building the NGINX Docker image is easy. We start by creating a Dockerfile. This file tells the Docker engine how to make the image. Below is a simple Dockerfile for NGINX:

# Use the official NGINX image from Docker Hub
FROM nginx:latest

# Copy custom configuration file
COPY nginx.conf /etc/nginx/nginx.conf

# Copy website files to the NGINX HTML directory
COPY html /usr/share/nginx/html

# Expose port 80 for web traffic
EXPOSE 80

In this Dockerfile:

  • FROM nginx:latest means we use the base image.
  • COPY commands put the configuration and website files into the container.
  • EXPOSE 80 tells that the container listens on port 80.

To build the image, we run this command in the terminal. Make sure we are in the folder with the Dockerfile:

docker build -t my-nginx-image .

This command builds the NGINX Docker image and calls it my-nginx-image. For more help on managing Docker images, we can check our Docker images tutorial.

After we build the image, we can run the NGINX container. This makes it easy to deploy and scale our web application with Docker.

Running the NGINX Container

To run the NGINX container, we need to have the NGINX Docker image built from the Dockerfile. The command to start the container is simple. We can use this command:

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

In this command,

  • -d makes the container run in the background.
  • --name nginx-server gives a name to the container. This helps us manage it easily.
  • -p 80:80 connects port 80 of our computer to port 80 of the container. This way, we can see NGINX in our browser.
  • nginx-image is the name of the image we built before.

Once the NGINX container is running, we can check if it is working by running this command:

docker ps

This command shows all running containers, including our NGINX one. We can go to http://localhost in our web browser to see the NGINX server.

If we want to learn more about advanced NGINX settings or other topics, we can check out Docker - Setting NGINX - Full Example or Docker - Hosting.

Exposing Ports for NGINX

We need to allow external access to the NGINX server that runs inside a Docker container. To do this, we must expose the right ports. NGINX listens on port 80 for HTTP and port 443 for HTTPS by default. When we run the NGINX container, we can map these ports to the host machine.

Here is how we expose ports when we run our NGINX Docker container:

docker run -d -p 80:80 -p 443:443 --name my-nginx nginx

In this command:

  • -d means we run the container in detached mode.
  • -p 80:80 maps port 80 of the host to port 80 of the container.
  • -p 443:443 maps port 443 of the host to port 443 of the container.
  • --name my-nginx gives a name to the container.

When we expose these ports, we can access NGINX from our browser. We just need to use http://localhost for normal access or https://localhost for secure connections.

If we want more details about Docker’s port management, we can check our guide on Docker - Managing Ports. It is very important to set up port exposure correctly. This helps us to deploy web applications with Docker and make sure our NGINX server can be reached from outside the container.

Accessing NGINX from the Host

To access our NGINX server running in a Docker container from our host machine, we need to make sure that the container’s ports are mapped to the host’s ports. This helps us to interact with our NGINX easily.

  1. Run NGINX Container: When we start our NGINX container, we use the -p flag to publish the container’s port to the host. For example, to show port 80 (HTTP):

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

    This command starts the NGINX container in detached mode. It maps port 80 of the container to port 80 of the host.

  2. Accessing NGINX: After we start the container, we can go to http://localhost in our web browser to access NGINX. If we run Docker on a remote server, we should replace localhost with the server’s IP address.

  3. Testing the Connection: We can check if NGINX is working by using curl:

    curl http://localhost

This command should show us the default NGINX welcome page HTML. This means that the NGINX server is running fine. For more advanced setups, we can check Docker - Managing Ports to learn more about managing port mappings.

Configuring NGINX with SSL

We need to configure NGINX with SSL to make our web applications safe. To use SSL, we must have an SSL certificate. We can get this from a Certificate Authority (CA) or make one using tools like Let’s Encrypt.

  1. Obtain an SSL Certificate: We can use Let’s Encrypt for free SSL certificates. It is good to follow their guide to get a certificate.

  2. NGINX Configuration: Next, we should update our NGINX configuration file to use SSL. Here is a simple example:

    server {
        listen 80;
        server_name example.com www.example.com;
        return 301 https://$host$request_uri;  # Redirect to HTTPS
    }
    
    server {
        listen 443 ssl;
        server_name example.com www.example.com;
    
        ssl_certificate /etc/ssl/certs/fullchain.pem;  # Path to our certificate
        ssl_certificate_key /etc/ssl/private/privkey.pem;  # Path to our private key
    
        location / {
            proxy_pass http://your_backend_service;  # Change as needed
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
  3. Testing Configuration: After we update the configuration, we need to check it with this command:

    nginx -t
  4. Reload NGINX: Finally, we should reload NGINX to make the changes take effect:

    systemctl reload nginx

For more details on Docker web server settings, we can look at Docker - Setting NGINX. When we configure SSL correctly, it helps with security and can make our SEO better.

Docker - Setting NGINX - Full Example

In this part, we will show a full example of how to set up NGINX using Docker. This example will cover the main steps. We will start from making a Docker network to running our NGINX container.

  1. Create a Docker Network:
    To let containers talk to each other, we first make a Docker network:

    docker network create nginx-network
  2. NGINX Configuration File:
    Next, we create an nginx.conf file with this simple setup:

    server {
        listen 80;
        server_name localhost;
    
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    }
  3. Dockerfile for NGINX:
    Now, we need to create a Dockerfile to build our NGINX image:

    FROM nginx:latest
    COPY nginx.conf /etc/nginx/nginx.conf
  4. Build the Docker Image:
    We run this command to build the image:

    docker build -t my-nginx-image .
  5. Run the NGINX Container:
    We start the NGINX container using this command:

    docker run -d --name my-nginx --network nginx-network -p 80:80 my-nginx-image

By following these steps, we have made a Dockerized NGINX server. For more information on how to manage NGINX with Docker, we can look at our guide on Docker - Setting NGINX and Docker Networking.

Conclusion

In this article, we talked about how to set up NGINX with Docker. We looked at important steps like making a Docker network, writing config files, and handling SSL. This guide on Docker - Setting NGINX helps us to make web server deployment easier. It also works well with other Docker tools.

If you want to learn more, we can check out our guides on Docker - Setting NodeJS and Docker - Hosting. Let’s use Docker to make our web applications better!

Comments