Nginx is a popular web server known for its high performance, stability, and rich feature set. Running Nginx inside a Docker container can provide isolation, ease of deployment, and scalability. This tutorial will guide you through the process of running Nginx in a Docker container on an Ubuntu 22.04 server.

Step 1: Deploying a Cloud Instance on Shape.host

  • Log in to Shape.host Dashboard:
  • Navigate to the Shape.host website and log in to your account.
  • Create a New Instance:
  • Click on the “Create” button located at the top right corner of the dashboard.
  • From the dropdown menu, select “Instances”.
Shape.host Resources dashboard showing the Create menu and instance rebuild controls.
  • Select Instance Location:
  • Choose the desired location for your server. For this tutorial, we’ll select “New York, USA”.
Shape.host Create New Instance form with the available data center locations.
  • Choose a Plan:
  • Select a plan that fits your requirements. For example, you might choose a plan with 2 cores CPU, 2 GB Memory, and 50 GB SSD disk space.
  • Select an Operating System:
  • Scroll down to the “Choose an image” section and select “Ubuntu 22.04”.
Shape.host instance form with a server plan and Ubuntu image selected.
  • Configure Additional Options:
  • (Optional) You can configure additional options like User Data Configuration and IPv6 Networking.
  • Enter a hostname for your instance, e.g., “Tutorial Ubuntu”.
  • Click on the “Create instance” button to deploy the instance.
Shape.host instance form showing hostname, instance count and the Create Instance button.

Step 2: Connecting to Your Instance

  • Retrieve SSH Credentials:
  • Note the IP address of your newly created instance from the Shape.host dashboard.
  • Connect via SSH:
  • Open a terminal on your local machine.
  • Use the following command to connect to your instance:
    sh ssh root@your_instance_ip
  • Replace your_instance_ip with the actual IP address of your instance.

To complete this tutorial, you will need:

  • An Ubuntu 22.04 server with root access.

Step 3: Installing Docker

First, update your existing list of packages and upgrade them:

apt update
apt upgrade -y
Terminal showing APT package management commands and output.

Next, install Docker using the apt command:

apt install docker.io -y
Terminal showing APT package management commands and output.

After the installation is complete, start the Docker service and enable it to start at boot:

systemctl start docker
systemctl enable docker
Terminal showing systemd service management commands or output.

Verify that Docker is running:

systemctl status docker
Terminal showing systemd service management commands or output.
Terminal showing systemd service management commands or output.

Step 4: Running an Nginx Container

Now that Docker is installed and running, you can run an Nginx container using the official Nginx image. Pull the Nginx image from Docker Hub and run it in a container with the following command:

docker run --name mynginx -d -p 80:80 nginx
Terminal showing docker commands or output during Running an Nginx Container.

In this command:

  • --name mynginx names the container “mynginx”.
  • -d runs the container in detached mode, meaning it runs in the background.
  • -p 80:80 maps port 80 on the host to port 80 in the container.

Verify that the Nginx container is running:

docker ps
Terminal showing the Docker container listing.
Terminal showing the Docker container listing.

You should see the “mynginx” container listed.

Step 5: Adjusting the Firewall

If your server is running a firewall, ensure that traffic on port 80 is allowed:

ufw allow 80
ufw enable
Terminal showing the Docker container listing.

Check the status of the firewall:

ufw status

Step 5: Testing Nginx

To verify that the Nginx container is working, open your web browser and navigate to your server’s IP address:

http://your_server_ip
Nginx default welcome page.

You should see the default Nginx welcome page.

Step 6: Managing the Nginx Container

Stopping the Container

To stop the Nginx container, use the following command:

docker stop mynginx
Terminal showing firewall configuration commands and rules.

Starting the Container

To start the Nginx container again, use:

docker start mynginx
Terminal showing docker commands or output during Starting the Container.

Removing the Container

If you want to remove the Nginx container, stop it first and then remove it:

docker stop mynginx
docker rm mynginx
Terminal showing docker commands or output during Removing the Container.

Step 7: Customizing Nginx Configuration

To customize the Nginx configuration, you need to create a custom configuration file and mount it into the container. First, create a directory to store the Nginx configuration file:

mkdir -p ~/mynginx/conf.d
Terminal showing file and directory setup commands.

Create a new configuration file in this directory:

nano ~/mynginx/conf.d/my_custom_config.conf
Terminal showing file and directory setup commands.

Add your custom Nginx configuration to this file and save it. For example:

server {
    listen 80;
    server_name your_domain;

    location / {
        proxy_pass http://your_backend_service;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
Terminal showing the GNU nano editor and configuration text.

Run a new Nginx container with the custom configuration:

docker run --name mynginx -d -p 80:80 -v ~/mynginx/conf.d:/etc/nginx/conf.d nginx
Terminal showing nano commands or output during Customizing Nginx Configuration.

In this command, -v ~/mynginx/conf.d:/etc/nginx/conf.d mounts the local configuration directory into the container.

Now, the Nginx container will use your custom configuration.

For reliable hosting solutions, consider using Shape.host services. They offer Cloud VPS, providing high performance and stability for running Docker containers with applications like Nginx.