Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
NGINX HTTP Server

You're reading from   NGINX HTTP Server Harness the power of NGINX with a series of detailed tutorials and real-life examples

Arrow left icon
Product type Paperback
Published in May 2024
Publisher Packt
ISBN-13 9781835469873
Length 262 pages
Edition 5th Edition
Languages
Tools
Concepts
Arrow right icon
Authors (3):
Arrow left icon
Martin Bjerretoft Fjordvald Martin Bjerretoft Fjordvald
Author Profile Icon Martin Bjerretoft Fjordvald
Martin Bjerretoft Fjordvald
Gabriel Ouiran Gabriel Ouiran
Author Profile Icon Gabriel Ouiran
Gabriel Ouiran
Mr. Clement Nedelcu Mr. Clement Nedelcu
Author Profile Icon Mr. Clement Nedelcu
Mr. Clement Nedelcu
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Part 1:Begin with NGINX FREE CHAPTER
2. Chapter 1: Downloading and Installing NGINX 3. Chapter 2: Basic NGINX Configuration 4. Part 2: Dive into NGINX
5. Chapter 3: Exploring the HTTP Configuration 6. Chapter 4: Exploring Module Configuration in NGINX 7. Chapter 5: PHP and Python with NGINX 8. Chapter 6: NGINX as a Reverse Proxy 9. Part 3: NGINX in Action
10. Chapter 7: Introduction to Load Balancing and Optimization 11. Chapter 8: NGINX within a Cloud Infrastructure 12. Chapter 9: Fully Deploy, Manage, and Auto-Update NGINX with Ansible 13. Chapter 10: Case Studies 14. Chapter 11: Troubleshooting 15. Index 16. Other Books You May Enjoy

Using Docker

In the previous section, we explored the fundamentals of cloud architectures. We’re now going to move on from theory to practice by installing and configuring Docker step by step to launch our first container.

Docker is not just a tool but a paradigm shift—a new era where software can be packaged and isolated, ensuring consistency across environments. By the end of this section, Docker will be more than a concept; it will be an integral part of your toolbox, starting with the deployment of an NGINX container.

Installing Docker

Fortunately for us, the Docker team has provided a script that simplifies the installation process. This script is compatible with a range of Linux distributions, including Red Hat Enterprise Linux (RHEL), CentOS, Fedora, Debian, Ubuntu, and their derivatives.

To install Docker, you will need to run the script with root privileges. Open your terminal and enter the following command:

# curl -s https://get.docker.com | bash

With this single command, Docker will be installed natively on your system using custom repositories adapted to your Linux distribution’s package manager. This means that updating Docker will be as easy as updating any other package on your system.

Your first Docker container

There are many ways to operate Docker containers. For instance, you can launch a container using a simple command:

root@docker:~# docker run -d nginx
latest: Pulling from library/nginx
e1caac4eb9d2: Pull complete

This command pulls the NGINX image from Docker Hub and starts a new container in detached mode. However, this container runs with default settings and isn’t yet configured for specific use.

To customize the container, you can pass additional parameters. For example, to map the container’s port 80 to the host’s port 80, allowing web traffic to reach the container, you would run the following:

root@docker:~# docker run -d nginx -p 80:80

The first command launches the NGINX container with the default configuration. The second command runs the same container, but now it is accessible via the host machine’s port 80. But what if you require a more advanced and complete configuration? This is where Docker Compose steps in, offering a solution for managing multi-container Docker applications with ease.

In the context of this book, the NGINX image serves as an excellent example of Docker’s capabilities as it showcases how containerization simplifies the deployment of services that traditionally require dedicated servers.

As we explore deeper, we’ll see how to tailor an NGINX container to serve static content or act as a reverse proxy, introducing the concept of Docker volumes and how to use them to serve custom configuration files and content.

Simplifying with Docker Compose

After running our first Docker container, it became clear that managing a container’s parameters directly from the command line can quickly become cumbersome. Docker Compose simplifies this process by allowing us to define and run multi-container Docker applications using a YAML file for configuration.

Let’s create an equivalent setup to the one we ran in the previous section—an NGINX container with port 80 exposed to the host machine:

  1. Begin by creating a /root/nginx directory, and within that directory, save a file named docker-compose.yml with the following content:
    version: '3'
    services:
      nginx:
        image: nginx:latest
        ports:
          - "80:80"

A reminder

Although the file format is called YAML, the extension needs to be .yml.

In this docker-compose.yml file, we’ve defined a service named nginx, using the official nginx image tagged with latest, which means it will be updated every time we pull it from Docker Hub. If you wish, you can specify a fixed version, such as nginx:1.25.4. More details can be found on the Docker Hub for NGINX page (https://hub.docker.com/_/nginx/).

  1. Now, with the Docker Compose file saved, run the following command in the same directory:
    root@nginx:~/nginx# docker compose up
    [+] Running 1/1
      Container nginx-nginx-1  Created
  2. You’ve just launched your first container using Docker Compose. To stop this container, simply use the Ctrl + C shortcut (^C). Alternatively, you can start the container in detached mode with up -d and stop it with down:
    root@nginx:~/nginx# docker compose up -d
    root@nginx:~/nginx# docker compose down

With Docker Compose, running NGINX in Docker becomes a matter of defining the desired state in a file, which is easier to manage and read than standalone commands. In the next section, we’ll explore how to further configure NGINX in Docker, tailoring it to our specific needs.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image