Configuring a Docker application to a domain name can seem daunting, especially when integrating services from multiple providers. In this guide on how to configure a Docker app to a domain name with Cloudflare DNS, we’ll walk you through the process of deploying your Docker app on an AWS EC2 instance and configuring it to use a custom domain name managed by Cloudflare DNS. We will see how to leverage the power of Nginx as a reverse proxy.
Prerequisites
For this demonstration, we will use the subdomain below. For the rest of the configurations, replace the subdomain used with your subdomain or domain name. Ensure the following are on check.
- Subdomains: api.dtechnologys.com
- The subdomain should have its DNS settings configured on Cloudflare, with SSL certificates enabled and forced to HTTPS. Update the A record to the IP of your EC2 instance/server
- An EC2 instance or any server to run your Docker containers and Nginx. Install docker. See the guide on how to install docker on Linux.
Step 1: Running Docker Containers
First, we’ll run our Docker applications on the specified ports. The docker image used is a Django application. When setting up your container, update the parameters as required. Note: By running the below, you will be able to access the application on http://HOSTIP:8001
docker run --name dtechweb -d -p 8001:8000 dannywangari/dtechnologies:2.0.0
Step 2: Install Nginx
Next, install Nginx on your server to act as a reverse proxy for your Docker applications.
- Update your package list and install nginx:
sudo apt update
sudo apt install nginx -y
Step 3: Configure Nginx
Create an Nginx configuration file for each subdomain/domain to route traffic to the appropriate Docker container.
Configure Nginx for the subdomain api.dtechnologys.com:
sudo nano /etc/nginx/sites-available/api.dtechnologys.com
#add the following
server {
listen 80;
server_name api.dtechnologys.com;
location / {
proxy_pass http://127.0.0.1:8001; #the port 8001 is the port which you exposed your docker application to on the container
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;
}
}
Step 4: Enable Nginx Sites
Enable these configurations by creating symbolic links to the sites-enabled
directory.
sudo ln -s /etc/nginx/sites-available/api.dtechnologys.com /etc/nginx/sites-enabled/
Step 5: Test Nginx Configuration
Check the Nginx configuration for syntax errors:
sudo nginx -t
If the test is successful, restart Nginx to apply the changes:
sudo systemctl restart nginx
If you encounter any errors, review the Nginx error logs and troubleshoot accordingly.
Conclusion
By following these above steps, you have successfully set up Docker applications on a domain/subdomain whose DNS records are being managed on Cloudflare, with Nginx acting as a reverse proxy. This setup ensures that your applications are securely accessible over HTTPS, leveraging Cloudflare’s robust DNS and SSL features.
For further assistance or advanced configurations, feel free to explore the Nginx documentation and Docker documentation.
Make a donation to support us
Web Hosting and email hosting Packages
- A Practical Tutorial for Dockerizing Software Applications
- How to Configure a Docker App to a Domain Name
- Getting Started with Docker | Docker commands
- How To Run Scripts in Linux
- Deploy a Django Application on EC2 Instance with Nginx
- How to configure a domain to a docker container and install an SSL certificate on AWS