Deploy Nginx Web Server Container with Docker Compose

Introduction

Nginx (pronounced “engine-x”) is a popular open-source web server, reverse proxy server, and load balancer. It’s known for its high performance, scalability, and efficient resource utilization. Originally developed to address the C10k problem (the challenge of handling 10,000 simultaneous connections), Nginx has become widely used in both small-scale and large-scale web applications.

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define an entire application stack, including services, networks, and volumes, in a single file called docker-compose.yml. With a single command, you can then spin up your entire application stack, simplifying the process of managing and deploying multi-container Docker applications.

Prerequisites

  • Up and running ubuntu 22.04 LTS machine.
  • Basic knowledge in linux commands.
  • Internet connectivity.
  • Docker and docker compose should installed on your machine.

To install Docker and docker compose on ubuntu 22.04 LTS, We can use the given link.

In this post, We will deploy Nginx web server container using Docker Compose on Ubuntu 22.04 LTS machine.

Step 1: Creating docker-compose.yml

To deploy a nginx container using Docker Compose, you’ll need to create a docker-compose.yml file that defines the service. Here’s a simple example:

To create a docker-compose.yml

sudo nano docker-compose.yml

Copy and paste the followings configuration.

version: '3'

services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"

This docker-compose.yml file defines a service named “nginx” using the official Nginx Docker image from Docker Hub. It exposes port 80 on the host, which is the default port for HTTP traffic.

To deploy the Nginx container, follow these steps.

  • Make sure you have Docker and Docker Compose installed on your system.
  • Create the docker-compose.yml file with the content mentioned above.
  • Open a terminal in the same directory as your docker-compose.yml file.
  • Run the following command to start the Nginx container.

Step 2: Deploy Nginx Container

To deploy this nginx service, follow these steps, Run the following command to start the nginx container.

sudo docker-compose up -d

The -d flag runs the containers in the background, It will take few sec or mins to pull the image and deploy the nginx.

Step 3: Validate the Container

To check the running containers launched by Docker Compose, you can use the following command:

sudo docker-compose ps

This command provides a summary of the status of each service defined in your docker-compose.yml file. It shows information such as the service name, container ID, status, ports, and names.

Here’s an example of what the output might look like:

   Name                 Command            State               Ports
-------------------------------------------------------------------------
nginx   docker-entrypoint.sh nginx   Up      0.0.0.0:80->80/tcp

Step 4: Logging

Some time we need to also check the real time logs, So we can use the given commands.

sudo docker-compose logs -f nginx

Step 5: Destroy Nginx Container

To destroy (stop and remove) the nginx container launched with Docker Compose, you can use the following command in the directory where your docker-compose.yml file is located:

sudo docker-compose down

This command stops and removes all the containers, networks, and volumes defined in your docker-compose.yml file.

The down command stops and removes the containers but retains the data volumes by default. If you want to remove the volumes as well, you can use the -v option:

sudo docker-compose down -v

Make sure you are in the correct directory containing your docker-compose.yml file when running these commands. This ensures that Docker Compose identifies the correct configuration file.

After running the docker-compose down command, you can use the docker-compose ps command to verify that the containers are no longer running. The output should be empty, indicating that no containers are currently running.

Remember that this command will stop and remove all the services defined in your docker-compose.yml file, not just the nginx service. If you only want to remove a specific service, you can specify the service name:

docker-compose down -v <service_name>

Replace <service_name> with the actual name of the service you want to remove.

Conclusion

We have successfully deployed nginx web server on docker’s container using docker compose on ubuntu 22.04 LTS machine, If you still have questions, please post them in the comments section below.

Author

Deploy Nginx Web Server Container with Docker Compose

Leave a Reply

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

Scroll to top