Easy Steps To Run Nginx in Docker Container on AlmaLinux 9

Posted on

Easy Steps To Run Nginx in Docker Container on AlmaLinux 9

Easy Steps To Run Nginx in Docker Container on AlmaLinux 9

This guide will demonstrate how to Install and Run Nginx in Docker Container on AlmaLinux 9. We will explore different methods to achieve this, leveraging the power of Docker for efficient and isolated Nginx deployments. The goal is to provide a comprehensive understanding of how to containerize Nginx on AlmaLinux 9.

Follow the steps outlined below to successfully Run Nginx in Docker Container on AlmaLinux 9.

Prerequisites

Before you begin to Run Nginx in Docker Container on AlmaLinux 9, ensure you meet the following requirements:

Requirements for Running Nginx in a Docker Container

First, you must have access to your server as a non-root user with sudo privileges. Refer to our Initial Server Setup Guide on AlmaLinux 9 for assistance.

Also, Docker must be installed and running on your server. Consult this guide on Install and Use Docker on AlmaLinux 9 to get Docker set up.

Once these prerequisites are satisfied, you’re ready to proceed with the steps to Run Nginx in Docker Container on AlmaLinux 9.

Method 1 – Install Nginx with Docker Hub on AlmaLinux 9

Docker Hub provides a convenient repository of pre-built images, including Nginx. This method utilizes the official Nginx image from Docker Hub for quick deployment.

Download the Official Nginx Docker Image

To search for the official Nginx image on AlmaLinux 9, use the following command:

docker search nginx
**<mark>Output</mark>**
NAME                                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                                             Official build of Nginx.                        18931     [OK]
unit                                              Official build of NGINX Unit: Universal Web      9         [OK]
nginxinc/nginx-unprivileged                       Unprivileged NGINX Dockerfiles                  112
nginx/nginx-ingress                               NGINX and  NGINX Plus Ingress Controllers fo     76
nginx/nginx-prometheus-exporter                   NGINX Prometheus Exporter for NGINX and NGIN     33
nginx/unit                                        NGINX Unit is a dynamic web and application      64
nginxinc/nginx-s3-gateway                         Authenticating and caching gateway based on      1
...

The output confirms that the official Nginx image is simply named "nginx". To download it, execute:

docker pull nginx
**<mark>Output</mark>**
Using default tag: latest
latest: Pulling from library/nginx
52d2b7f179e3: Pull complete
fd9f026c6310: Pull complete
055fa98b4363: Pull complete
96576293dd29: Pull complete
a7c4092be904: Pull complete
e3b6889c8954: Pull complete
da761d9a302b: Pull complete
Digest: sha256:104c7c5c54f2685f0f46f3be607ce60da7085da3eaa5ad22d3d9f01594295e9c
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

Run Nginx Docker Container

With the Nginx image downloaded, create and start a container. This command maps port 80 on the host to port 80 inside the container:

docker run -d --name nginx -p 80:80 nginx
**<mark>Output</mark>**
5d0370d1c3426d49c2905fa794b7f822867e0a8ad6b5fc7f6360df6690b6aef7

Verify whether your Nginx Container is Running or Not

Open a web browser and navigate to your server’s IP address. If Nginx is running correctly, you should see the "Welcome to Nginx" page.

http://<mark>your-server-ip-address</mark>
Run Nginx Docker Container with Docker Hub
Run Nginx in Docker Container on AlmaLinux 9

Manage Nginx Container

To stop the container:

docker stop nginx

To start the container:

docker start nginx

To remove the container (stop it first):

docker stop nginx
docker rm nginx

Method 2 – Install Nginx with Dockerfile on AlmaLinux 9

Using a Dockerfile allows for greater customization and control over the Nginx image. This method involves creating a Dockerfile that specifies the base image, copies necessary files, and exposes the required port.

Create an Nginx Image Directory

Create a directory for your Nginx project and navigate into it:

mkdir nginx
cd nginx

Create a custom landing page (e.g., index.html) using a text editor:

vi index.html

Add the following sample code to the file:

<h1>Test</h1>
<p>This is a orcacore.com test page for the Nginx deployment in Docker</p>

Save and close the file.

Build a Dockerfile for Nginx Image

Create a Dockerfile:

vi Dockerfile

Add the following configuration to the Dockerfile:

FROM nginx:latest
COPY index.html /usr/share/nginx/html
EXPOSE 80

Save and close the file.

Run Nginx Dockerfile as a Container

Build the Docker image:

docker build -t nginx:v1 .

Run the container from the newly built image:

docker run -d --name nginx -p 80:80 nginx:v1
**<mark>Output</mark>**
0f8843fe9d883534f21105f5db8880f08e36c66e4aa44eb2fc3e29dafb1066c8

Verify Nginx Dockerfile Container is Running

Access your server’s IP address in a web browser:

http://<mark>your-server-ip</mark>

You should see your custom landing page.

Run Nginx Dockerfile Container
Run Nginx in Docker Container on AlmaLinux 9

Alternative Solutions for Running Nginx in Docker

While the previous methods are common, here are two alternative approaches to Run Nginx in Docker Container on AlmaLinux 9:

1. Using Docker Compose

Docker Compose simplifies the management of multi-container applications. It allows you to define your Nginx container and any related services in a single docker-compose.yml file. This is beneficial when you have other components that need to interact with Nginx, such as a database or a backend application.

Explanation:

Docker Compose reads the docker-compose.yml file and creates and manages the defined containers. It handles networking, volume mounting, and dependency management between services. This approach improves maintainability and reproducibility of your Nginx deployment.

Code Example (docker-compose.yml):

version: "3.9"
services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./html:/usr/share/nginx/html
    restart: always

In this example:

  • version: Specifies the Docker Compose file version.
  • services: Defines the services that make up the application.
  • nginx: The name of the Nginx service.
  • image: The Nginx image to use.
  • ports: Maps port 80 on the host to port 80 in the container.
  • volumes: Mounts a local directory (./html) to the Nginx web root.
  • restart: always: Ensures that the container restarts automatically if it fails.

To use this, create a directory named html in the same location as the docker-compose.yml file and place your index.html inside it. Then, run docker-compose up -d to start the container in detached mode.

2. Using a Custom Nginx Configuration

The default Nginx configuration might not always suit your needs. You can create a custom Nginx configuration file and mount it into the container. This allows you to customize the server’s behavior, such as defining virtual hosts, setting up SSL, or configuring caching.

Explanation:

This method involves creating a nginx.conf file with your desired configuration and then mounting it into the container’s /etc/nginx/conf.d directory. Docker will then use your custom configuration instead of the default one.

Code Example (nginx.conf):

server {
    listen 80;
    server_name example.com;

    root /usr/share/nginx/html;
    index index.html index.htm;

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

Dockerfile Example:

FROM nginx:latest

COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY index.html /usr/share/nginx/html/

EXPOSE 80

In this example:

  • The nginx.conf file defines a simple virtual host that listens on port 80 and serves files from the /usr/share/nginx/html directory.
  • The Dockerfile copies the nginx.conf file to the correct location within the container, overwriting the default configuration.
  • The index.html file is copied to the web root.

After building the Docker image using the Dockerfile, run the container using:

docker run -d --name mynginx -p 80:80 mynginx

This will start an Nginx container with your custom configuration.

Conclusion

This guide provided comprehensive instructions on how to Install and Run Nginx in Docker Container on AlmaLinux 9, including utilizing Docker Hub and Dockerfiles. Furthermore, it explored two alternative methods: Docker Compose for managing multi-container applications and using custom Nginx configurations for tailored server behavior. These techniques empower you to deploy Nginx efficiently and effectively within Docker containers on AlmaLinux 9.

Leave a Reply

Your email address will not be published. Required fields are marked *