Question

How to run multiple containers of the same image and expose different ports?

Dear community,

I have built a droplet with Ubuntu 18.04 and Docker CE edition. For teaching purposes, I want to run 18 times the same container based on a Docker image hosted on DockerHub: https://hub.docker.com/repository/docker/scienceparkstudygroup/master-gls

When I run one container and expose it on the 8787 port, everything runs smoothly and I can access the RStudio instance at my droplet IP address + :8787.

docker run --rm --name rstudio -e PASSWORD=mypwd -p 8787:8787 scienceparkstudygroup/master-gls:openr-latest

My problem is to create multiple instances of the same container but expose it to different ports. I have tried Docker-compose but no success so far.

Any idea? In advance thank you Marc


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
April 15, 2020
Accepted Answer

Hi there @mgalland,

You would just need to adjust the -p argument and change the first part from 8787 to the new port that you would like to use.

For example:

  • Starting the first container on port 8787:
  1. docker run --rm --name rstudio-1 -e PASSWORD=mypwd -p 8787:8787 scienceparkstudygroup/master-gls:openr-latest
  • Starting the second container on port 8788:
  1. docker run --rm --name rstudio-2 -e PASSWORD=mypwd -p 8788:8787 scienceparkstudygroup/master-gls:openr-latest

Note: Make sure to use different names as well otherwise it would not work.

You could also add Nginx as a reverse proxy to the set up:

https://www.digitalocean.com/community/questions/how-to-host-multiple-docker-containers-on-a-single-droplet-with-nginx-reverse-proxy

Here’s a quick video demo on how to do the above:

Hope that this helps! Regards, Bobby

Hello bobby Thank you, it worked like a charm. I am now running my containers with the --detach option. Still struggling a bit to mount a volume within my machine for each container but I’ll probably figure this out myself. Thanks again marc

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

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.