Hosting Multiple Docker Containers on a Single DigitalOcean Droplet

Coding Posted on Apr 07, 2023
In the previous blog post, I walked you through deploying a Dockerized Ruby on Rails application on a DigitalOcean Droplet. In this follow-up post, we'll demonstrate how to host multiple Docker containers on the same Droplet. This can be useful for running multiple applications, services, or microservices on a single virtual server to save costs and simplify management.

Prerequisites


Before you proceed, ensure that you've successfully deployed your Dockerized Rails application on a DigitalOcean Droplet using the previous blog post. Also, make sure your Droplet has enough resources (CPU, memory, and storage) to accommodate the additional containers.

Step 1: Prepare Your Applications


  1. Ensure that each application has its own Dockerfile and docker-compose.yml file with the appropriate configurations.
  2. Make sure your applications use different ports to avoid conflicts. For example, if your first application uses port 3000, your second application should use a different port, like 3001.

Step 2: Clone and Deploy Your Applications on the Droplet


  1. Connect to your Droplet using SSH:

$ ssh -i ~/.ssh/id_rsa root@your_droplet_ip_address

2. Clone the repositories for each application on the Droplet:

$ git clone <your_first_repository_url> first_app
$ git clone <your_second_repository_url> second_app

3. Navigate to the first application directory and build the Docker containers:

$ cd first_app
$ docker-compose build

4. Start the first application's containers:

$ docker-compose up -d

5. Navigate to the second application directory and build the Docker containers:

$ cd ../second_app
$ docker-compose build

6. Start the second application's containers:

$ docker-compose up -d

Both applications should now be running on the same Droplet, each with its own set of containers.

Step 3: Set Up a Reverse Proxy (Optional)


If you want to host multiple applications with their domain names on the same Droplet, you can set up a reverse proxy using Nginx. This will route traffic to the appropriate containers based on domain names or URI paths.

  1. Install Nginx on your Droplet:

$ sudo apt-get update
$ sudo apt-get install nginx

2. Create an Nginx configuration file for each application:

$ sudo nano /etc/nginx/sites-available/first_app.conf
$ sudo nano /etc/nginx/sites-available/second_app.conf

3. Configure the Nginx files with the appropriate server blocks. For example:

server {
    listen 80;
    server_name your_first_app_domain.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

server {
    listen 80;
    server_name your_second_app_domain.com;

    location / {
        proxy_pass http://localhost:3001;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

4. Create symbolic links for the configuration files:

$ sudo ln -s /etc/nginx/sites-available/first_app.conf /etc/nginx/sites-enabled/
$ sudo ln -s /etc/nginx/sites-available/second_app.conf /etc/nginx/sites-enabled/

5. Restart Nginx to apply the changes:

$ sudo systemctl restart nginx

Your applications should now be accessible using their respective domain names.

Conclusion


In this blog post, I've shown you how to host multiple Docker containers on a single DigitalOcean Droplet. By following these steps, you can deploy multiple applications, services, or microservices on a single virtual server, saving costs and simplifying management. Additionally, we've demonstrated how to set up a reverse proxy using Nginx to route traffic to the appropriate containers based on domain names or URI paths, allowing you to host multiple applications with their domain names on the same Droplet.

Remember to monitor your Droplet's resource usage and upgrade to a larger Droplet if necessary. Running multiple containers on a single Droplet may increase the risk of resource contention and impact your applications' performance. Always ensure that your Droplet has enough resources (CPU, memory, and storage) to accommodate the combined requirements of all the containers.

Leave a comment:

Comments (0)