Question

How to expose a Docker container to the internet on DigitalOcean?

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!


Submit an answer
ļ»æ

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!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
ā€¢ October 8, 2024
Accepted Answer

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:

  1. 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:

    docker run -d -p 80:80 your-container-image
    

    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.

  2. 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:

    server {
        listen 80;
        server_name yourdomain.com;
    
        location / {
            proxy_pass http://localhost:8080;
            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;
        }
    }
    

    You can also set up multiple locations in your Nginx config to handle multiple apps or services.

  3. 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!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOceanā€™s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow ā€” whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.