Run Nginx Docker Container on Debian 12 – Easy Setup

This guide intends to teach you to Run Nginx Docker Container on Debian 12. A Docker container is an executable package with everything needed to run software. Containers are created from Docker images, which are read-only templates that define the contents and configuration of a container. This guide on the Orcacore website will show you how to deploy and Run Nginx Docker Container on Debian 12.

Steps To Run Nginx Docker Container on Debian 12

Before you start your Nginx setup in the Docker container, you need to log in to your server as a non-root user with sudo privileges. To do this, you can check the Initial Server Setup with Debian 12.

Also, you need to have Docker installed on your server. For this purpose, you can check the Docker installation on Debian 12.

Then, you can use one of the following methods to complete this guide:

  • Run Nginx with DockerHub
  • Run Nginx with Dockerfile

Let’s see how you can Run Nginx Docker Container on Debian 12.

Method 1 – Run Nginx Docker Container on Debian 12 with DockerHub

In this method, you can simply search and download Nginx images from DockerHub. To do this, you can search for the Nginx image with the command below:

docker search nginx
Run Nginx Docker Container on Debian 12 with Docker Hub

As you can see, the official image is called Nginx. To download the image, you can run:

docker pull nginx
Pull Nginx Docker Image

At this point, you can create and run the Nginx container by using the following command:

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

You will get something similar to this in your output:

Exmaple Output
58b5de943ab88b688dbff8003cd5e3346017b8ced6c153906e914f713341cab8

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
Run Nginx with DockerHub on Debian 12

Method 2 – Run Nginx Docker Container on Debian 12 with Dockerfile

Another way that you can run your Nginx container is to use the Dockerfile. 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 like the Nano 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>

When you are done, save and close the file.

Now you need to create the Dockerfile for your Nginx image on Debian 12. To do this, you can run the command below:

vi Dockerfile

Then, you must write the Nginx image configuration inside the file on Debian 12:

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

When you are done, save and close the file.

Now you can build your Dockerfile image name with the command below:

docker build -t nginx:v1 .
Build Nginx Dockerfile

At this point, you can run Nginx as a Dockerfile with the following command:

docker run -d --name nginx -p 80:80 nginx:v1
Exmaple Output
e97b807e2246bed6d65985c69f87d462e5709e02fb9fb0956f83bf6cb2ce0d49

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:

Run Nginx with Dockerfile on Debian 12

Manage Nginx Container From CLI

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

Also, you can check your Nginx Docker container status with the command below:

docker ps
Check Nginx Docker container status

Conclusion

At this point, you have learned to use Docker Hub and Dockerfile to Run Nginx Docker Container on Debian 12. Also, you can easily manage your docker container from your terminal. Hope you enjoy it.

Also, you may like to read the following articles:

Run Docker Containers with Portainer

LEMP Stack Setup with Docker Compose on Debian / Ubuntu

Start a Docker Container with Exited Status

Install MySQL in Docker Container on Debian 12

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!