Hey everyone! š
Iām new to Docker and DigitalOcean, and I have a simple web application running inside a Docker container on my Droplet. I can access it locally on the Droplet, but Iām not sure how to make it accessible to the public over the internet using the Dropletās public IP.
Do I need to expose specific ports, and how do I configure this in Docker? Any help would be appreciated!
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Hey there š,
Great question! To make your Docker container accessible to the internet on your Droplet, youāll need to expose the correct ports when you run the container. Hereās a bit more detail to get you going:
If your app is running on, letās say, port 80 inside the container (for HTTP traffic), youāll need to expose it to the outside world by mapping it to a port on your Dropletās public IP. You can do this by adding the
-p
option to your Docker run command. For example:This will map port 80 on your Droplet (the public IP) to port 80 inside your container, making your app accessible through the Dropletās IP. If youāre running on a different port inside the container, just adjust the port mapping accordingly, e.g.,
-p 8080:80
.Note that if youāre running your container on a different port (e.g., 8080), users will need to access your app via
http://your-droplet-ip:8080
. Also if your app is listening on a different port inside the container, youāll need to adjust the-p
option accordingly, eg.-p 3000:3000
.If youāre running multiple containers or want more control over how traffic is managed, you could set up Nginx as a reverse proxy. Nginx will listen for requests and forward them to the appropriate container. This is especially useful when you have multiple apps running on the same Droplet and want them all served via different paths or subdomains.
Hereās an example of an Nginx configuration that forwards requests to your Docker container:
You can also set up multiple locations in your Nginx config to handle multiple apps or services.
Itās super important to secure your web app with SSL (HTTPS), especially if youāre dealing with sensitive data. With Nginx, you can easily add SSL by using Letās Encrypt, which provides free SSL certificates. You can follow this tutorial to install Letās Encrypt and configure SSL for your Nginx setup.
On another note, if you havenāt already, be sure to check your firewall settings! Youāll want to make sure your Dropletās firewall allows incoming traffic on ports 80 (HTTP) and 443 (HTTPS). You can manage this through DigitalOceanās Cloud Firewalls, which make it easy to control inbound and outbound traffic.
Let me know how it goes or if you need any help with the Nginx setup, SSL configuration, or anything else!
- Bobby
And hey, if youāre new to Docker or want to dive deeper into using it, check out this free Docker eBook I wrote: Introduction to Docker eBook. Itās a great way to level up your container game!