Share your love
Run Nginx in Docker Container on AlmaLinux 9
In this guide, we want to show you how to Install and Run an Nginx Web Server in a Docker Container on AlmaLinux 9. We try to Run Nginx by using the following methods in a Docker Container:
- Docker Hub: It is a service that is used to find and share container images.
- Dockerfile: It is a text-based document that can be used to create an image.
Follow the steps below to see how you can do it.
How To Run Nginx in Docker Container on AlmaLinux 9?
Before you start your Nginx installation in a Docker container on your server, you need some requirements. Let’s see what we need.
Requirements
First, you must have access to your server as a non-root user with sudo privileges. For this purpose, you can visit our Initial Server Setup Guide on AlmaLinux 9.
Also, you must have Docker running on your server. To do this, you can visit this guide on Install and Use Docker on AlmaLinux 9.
When you are done with these requirements, follow the steps below to complete this guide.
Step 1 – Install Nginx with Docker Hub on AlmaLinux 9
You can easily search and download Nginx which is available in Docker Hub.
Download the Official Nginx Docker Image
To search for an official image for Nginx on AlmaLinux 9, run the following command:
docker search nginx
Output
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
...
As you can see from the output, the official build image for Nginx is nginx. To download the Nginx image, run the following command:
docker pull nginx
Output
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
When your Nginx image is downloaded, you need to create a container for your Nginx image and start your container on AlmaLinux 9. To do this, you can use the following command to create your container and run it:
docker run -d --name nginx -p 80:80 nginx
You will get something similar to this in your output:
Output
5d0370d1c3426d49c2905fa794b7f822867e0a8ad6b5fc7f6360df6690b6aef7
Verify whether your Nginx Container is Running or Not
At this point, from your web browser type your server’s IP address, if your Nginx is running, you must see the Welcome to Nginx page.
http://your-server-ip-address
Manage Nginx Container
If you plan to stop your container, you can use the command below:
docker stop nginx
You can start your container by using the following command:
docker run nginx
To remove the container, you must stop the container first, then, you can use the command below:
docker remove nginx
Step 2 – Install Nginx with Dockerfile on AlmaLinux 9
Another way that you can run your Nginx container is to use the Dockerfile. To do this, follow the steps below.
Create an Nginx Image Directory
First, you need to create a directory for your Nginx and switch to it with the commands below:
# mkdir nginx
# cd nginx
At this point, you need to create the files you want to include in the image. For example, we create a default landing page with the vi editor, you can choose your desired one.
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>
When you are done, save and close the file.
Build a Dockerfile for Nginx Image
Now you need to create the Dockerfile for your Nginx image on AlmaLinux 9. To do this, you can run the command below:
vi Dockerfile
Then, you must write the Nginx image configuration inside the file on AlmaLinux 9:
FROM nginx:latest
COPY index.html /usr/share/nginx/html
EXPOSE 8080
When you are done, save and close the file.
Run Nginx Dockerfile as a Container
Now you can build your Dockerfile image name with the command below:
docker build -t nginx:v1 .
At this point, you can run Nginx as a Dockerfile with the following command:
docker run -d --name nginx -p 80:80 nginx:v1
Output
0f8843fe9d883534f21105f5db8880f08e36c66e4aa44eb2fc3e29dafb1066c8
Verify Nginx Dockerfile Container is Running
At this point, you can verify your Nginx is running as a Dockerfile by typing your server’s IP address in your web browser:
http://your-server-ip
In your web browser, you should see the text file message:
Conclusion
At this point, you have learned to Install and Run the Nginx Web Server in a Docker Container on AlmaLinux 9. You can use Docker Hub or a Dockerfile to run your Nginx. Both methods are described in the article, you can choose your desired one to complete your setup.
Hope you enjoy it. You may be interested in these articles too: