Docker mis-forwarding ports












0















I have several domains sharing one public IP (EC2 instance). My setup is like this:



/home/ubuntu contains docker-compose.yml:



version: '3'
services:
nginx-proxy:
image: "jwilder/nginx-proxy"
container_name: nginx-proxy
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
ports:
- "80:80"
restart: "always"


This creates a network named ubuntu_default which will allow other compose instances to join. The nginx-proxy image creates reverse proxies for these other compose instances so that you can visit example.com and be routed to the appropriate UI within the appropriate compose instance.



/home/ubuntu/example.com/project-1 contains a docker-compose.yml like:



version: '3'
services:
db:
build: "./db" # mongo
volumes:
- "./data:/data/db"
restart: "always"
api:
build: "./api" # a node backend
ports:
- "9005:9005"
restart: "always"
depends_on:
- db
ui:
build: "./ui" # a react front end
ports:
- "8005:8005"
restart: "always"
environment:
- VIRTUAL_HOST=project-1.example.com # this tells nginx-proxy which domain to proxy
- VIRTUAL_PORT=8005 # this tells nginx-proxy which port to proxy
networks:
default:
external:
name: ubuntu_default


/home/ubuntu/testing.com/project-2 contains a docker-compose.yml like:



version: '3'
services:
db:
build: "./db" # postgres
volumes:
- "./data:/var/lib/postgresql/data"
restart: "always"
api:
build: "./api" # a python backend
ports:
- "9000:9000"
restart: "always"
depends_on:
- db
ui:
build: "./ui" # a react front end
ports:
- "8000:8000"
restart: "always"
environment:
- VIRTUAL_HOST=testing.com,www.testing.com # tells nginx-proxy which domains to proxy
- VIRTUAL_PORT=8000 # tells nginx-proxy which port to proxy
networks:
default:
external:
name: ubuntu_default


So basically:




  • project-1.example.com:80 forwards to the UI running on :8005

  • project-1.example.com:80/api forwards to the API running on :9005

  • testing.com forwards to the UI running on :8000

  • testing.com/api forwards to the API running on :9000


...and that all works perfectly as long as I only run one at a time. The moment I start both Compose instances, the /api urls start clashing. I can sit on one of them and refresh repeatedly and sometimes I'll see the one for example.com/api and sometimes I'll see the one for testing.com/api.



I have no idea whats going on at this point. Maybe the premise I'm working against is fundamentally flawed but it seems like an intended use of Docker/Compose. I'm open to suggestions to accomplish the same otherwise.










share|improve this question



























    0















    I have several domains sharing one public IP (EC2 instance). My setup is like this:



    /home/ubuntu contains docker-compose.yml:



    version: '3'
    services:
    nginx-proxy:
    image: "jwilder/nginx-proxy"
    container_name: nginx-proxy
    volumes:
    - /var/run/docker.sock:/tmp/docker.sock:ro
    ports:
    - "80:80"
    restart: "always"


    This creates a network named ubuntu_default which will allow other compose instances to join. The nginx-proxy image creates reverse proxies for these other compose instances so that you can visit example.com and be routed to the appropriate UI within the appropriate compose instance.



    /home/ubuntu/example.com/project-1 contains a docker-compose.yml like:



    version: '3'
    services:
    db:
    build: "./db" # mongo
    volumes:
    - "./data:/data/db"
    restart: "always"
    api:
    build: "./api" # a node backend
    ports:
    - "9005:9005"
    restart: "always"
    depends_on:
    - db
    ui:
    build: "./ui" # a react front end
    ports:
    - "8005:8005"
    restart: "always"
    environment:
    - VIRTUAL_HOST=project-1.example.com # this tells nginx-proxy which domain to proxy
    - VIRTUAL_PORT=8005 # this tells nginx-proxy which port to proxy
    networks:
    default:
    external:
    name: ubuntu_default


    /home/ubuntu/testing.com/project-2 contains a docker-compose.yml like:



    version: '3'
    services:
    db:
    build: "./db" # postgres
    volumes:
    - "./data:/var/lib/postgresql/data"
    restart: "always"
    api:
    build: "./api" # a python backend
    ports:
    - "9000:9000"
    restart: "always"
    depends_on:
    - db
    ui:
    build: "./ui" # a react front end
    ports:
    - "8000:8000"
    restart: "always"
    environment:
    - VIRTUAL_HOST=testing.com,www.testing.com # tells nginx-proxy which domains to proxy
    - VIRTUAL_PORT=8000 # tells nginx-proxy which port to proxy
    networks:
    default:
    external:
    name: ubuntu_default


    So basically:




    • project-1.example.com:80 forwards to the UI running on :8005

    • project-1.example.com:80/api forwards to the API running on :9005

    • testing.com forwards to the UI running on :8000

    • testing.com/api forwards to the API running on :9000


    ...and that all works perfectly as long as I only run one at a time. The moment I start both Compose instances, the /api urls start clashing. I can sit on one of them and refresh repeatedly and sometimes I'll see the one for example.com/api and sometimes I'll see the one for testing.com/api.



    I have no idea whats going on at this point. Maybe the premise I'm working against is fundamentally flawed but it seems like an intended use of Docker/Compose. I'm open to suggestions to accomplish the same otherwise.










    share|improve this question

























      0












      0








      0








      I have several domains sharing one public IP (EC2 instance). My setup is like this:



      /home/ubuntu contains docker-compose.yml:



      version: '3'
      services:
      nginx-proxy:
      image: "jwilder/nginx-proxy"
      container_name: nginx-proxy
      volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      ports:
      - "80:80"
      restart: "always"


      This creates a network named ubuntu_default which will allow other compose instances to join. The nginx-proxy image creates reverse proxies for these other compose instances so that you can visit example.com and be routed to the appropriate UI within the appropriate compose instance.



      /home/ubuntu/example.com/project-1 contains a docker-compose.yml like:



      version: '3'
      services:
      db:
      build: "./db" # mongo
      volumes:
      - "./data:/data/db"
      restart: "always"
      api:
      build: "./api" # a node backend
      ports:
      - "9005:9005"
      restart: "always"
      depends_on:
      - db
      ui:
      build: "./ui" # a react front end
      ports:
      - "8005:8005"
      restart: "always"
      environment:
      - VIRTUAL_HOST=project-1.example.com # this tells nginx-proxy which domain to proxy
      - VIRTUAL_PORT=8005 # this tells nginx-proxy which port to proxy
      networks:
      default:
      external:
      name: ubuntu_default


      /home/ubuntu/testing.com/project-2 contains a docker-compose.yml like:



      version: '3'
      services:
      db:
      build: "./db" # postgres
      volumes:
      - "./data:/var/lib/postgresql/data"
      restart: "always"
      api:
      build: "./api" # a python backend
      ports:
      - "9000:9000"
      restart: "always"
      depends_on:
      - db
      ui:
      build: "./ui" # a react front end
      ports:
      - "8000:8000"
      restart: "always"
      environment:
      - VIRTUAL_HOST=testing.com,www.testing.com # tells nginx-proxy which domains to proxy
      - VIRTUAL_PORT=8000 # tells nginx-proxy which port to proxy
      networks:
      default:
      external:
      name: ubuntu_default


      So basically:




      • project-1.example.com:80 forwards to the UI running on :8005

      • project-1.example.com:80/api forwards to the API running on :9005

      • testing.com forwards to the UI running on :8000

      • testing.com/api forwards to the API running on :9000


      ...and that all works perfectly as long as I only run one at a time. The moment I start both Compose instances, the /api urls start clashing. I can sit on one of them and refresh repeatedly and sometimes I'll see the one for example.com/api and sometimes I'll see the one for testing.com/api.



      I have no idea whats going on at this point. Maybe the premise I'm working against is fundamentally flawed but it seems like an intended use of Docker/Compose. I'm open to suggestions to accomplish the same otherwise.










      share|improve this question














      I have several domains sharing one public IP (EC2 instance). My setup is like this:



      /home/ubuntu contains docker-compose.yml:



      version: '3'
      services:
      nginx-proxy:
      image: "jwilder/nginx-proxy"
      container_name: nginx-proxy
      volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      ports:
      - "80:80"
      restart: "always"


      This creates a network named ubuntu_default which will allow other compose instances to join. The nginx-proxy image creates reverse proxies for these other compose instances so that you can visit example.com and be routed to the appropriate UI within the appropriate compose instance.



      /home/ubuntu/example.com/project-1 contains a docker-compose.yml like:



      version: '3'
      services:
      db:
      build: "./db" # mongo
      volumes:
      - "./data:/data/db"
      restart: "always"
      api:
      build: "./api" # a node backend
      ports:
      - "9005:9005"
      restart: "always"
      depends_on:
      - db
      ui:
      build: "./ui" # a react front end
      ports:
      - "8005:8005"
      restart: "always"
      environment:
      - VIRTUAL_HOST=project-1.example.com # this tells nginx-proxy which domain to proxy
      - VIRTUAL_PORT=8005 # this tells nginx-proxy which port to proxy
      networks:
      default:
      external:
      name: ubuntu_default


      /home/ubuntu/testing.com/project-2 contains a docker-compose.yml like:



      version: '3'
      services:
      db:
      build: "./db" # postgres
      volumes:
      - "./data:/var/lib/postgresql/data"
      restart: "always"
      api:
      build: "./api" # a python backend
      ports:
      - "9000:9000"
      restart: "always"
      depends_on:
      - db
      ui:
      build: "./ui" # a react front end
      ports:
      - "8000:8000"
      restart: "always"
      environment:
      - VIRTUAL_HOST=testing.com,www.testing.com # tells nginx-proxy which domains to proxy
      - VIRTUAL_PORT=8000 # tells nginx-proxy which port to proxy
      networks:
      default:
      external:
      name: ubuntu_default


      So basically:




      • project-1.example.com:80 forwards to the UI running on :8005

      • project-1.example.com:80/api forwards to the API running on :9005

      • testing.com forwards to the UI running on :8000

      • testing.com/api forwards to the API running on :9000


      ...and that all works perfectly as long as I only run one at a time. The moment I start both Compose instances, the /api urls start clashing. I can sit on one of them and refresh repeatedly and sometimes I'll see the one for example.com/api and sometimes I'll see the one for testing.com/api.



      I have no idea whats going on at this point. Maybe the premise I'm working against is fundamentally flawed but it seems like an intended use of Docker/Compose. I'm open to suggestions to accomplish the same otherwise.







      docker routing docker-compose compose jwilder-nginx-proxy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 22:50









      oooyayaoooyaya

      959926




      959926
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Docker containers communicate using DNS lookups on their network. If multiple containers have the same alias on the same network, it will round robin load balance between the containers with each network connection. If you don't want containers to talk to each other, then you don't want them on the same docker network. The good news is you solve this by using more than one network, and not putting the api and db server on the frontend proxy network:



          version: '3'
          services:
          db:
          build: "./db" # postgres
          volumes:
          - "./data:/var/lib/postgresql/data"
          restart: "always"
          api:
          build: "./api" # a python backend
          ports:
          - "9000:9000"
          restart: "always"
          depends_on:
          - db
          ui:
          build: "./ui" # a react front end
          ports:
          - "8000:8000"
          restart: "always"
          networks:
          - default
          - proxy
          environment:
          - VIRTUAL_HOST=testing.com,www.testing.com # tells nginx-proxy which domains to proxy
          - VIRTUAL_PORT=8000 # tells nginx-proxy which port to proxy
          networks:
          proxy:
          external:
          name: ubuntu_default


          If you do not override the default network, docker will create one for your compose project and use it for any containers not assigned to another network.






          share|improve this answer
























          • wow. i didnt know that about the load balancing. thats super useful now that i know it exists and its definitely what was happening. thank you for the knowledge.

            – oooyaya
            Jan 4 at 13:50













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014196%2fdocker-mis-forwarding-ports%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Docker containers communicate using DNS lookups on their network. If multiple containers have the same alias on the same network, it will round robin load balance between the containers with each network connection. If you don't want containers to talk to each other, then you don't want them on the same docker network. The good news is you solve this by using more than one network, and not putting the api and db server on the frontend proxy network:



          version: '3'
          services:
          db:
          build: "./db" # postgres
          volumes:
          - "./data:/var/lib/postgresql/data"
          restart: "always"
          api:
          build: "./api" # a python backend
          ports:
          - "9000:9000"
          restart: "always"
          depends_on:
          - db
          ui:
          build: "./ui" # a react front end
          ports:
          - "8000:8000"
          restart: "always"
          networks:
          - default
          - proxy
          environment:
          - VIRTUAL_HOST=testing.com,www.testing.com # tells nginx-proxy which domains to proxy
          - VIRTUAL_PORT=8000 # tells nginx-proxy which port to proxy
          networks:
          proxy:
          external:
          name: ubuntu_default


          If you do not override the default network, docker will create one for your compose project and use it for any containers not assigned to another network.






          share|improve this answer
























          • wow. i didnt know that about the load balancing. thats super useful now that i know it exists and its definitely what was happening. thank you for the knowledge.

            – oooyaya
            Jan 4 at 13:50


















          1














          Docker containers communicate using DNS lookups on their network. If multiple containers have the same alias on the same network, it will round robin load balance between the containers with each network connection. If you don't want containers to talk to each other, then you don't want them on the same docker network. The good news is you solve this by using more than one network, and not putting the api and db server on the frontend proxy network:



          version: '3'
          services:
          db:
          build: "./db" # postgres
          volumes:
          - "./data:/var/lib/postgresql/data"
          restart: "always"
          api:
          build: "./api" # a python backend
          ports:
          - "9000:9000"
          restart: "always"
          depends_on:
          - db
          ui:
          build: "./ui" # a react front end
          ports:
          - "8000:8000"
          restart: "always"
          networks:
          - default
          - proxy
          environment:
          - VIRTUAL_HOST=testing.com,www.testing.com # tells nginx-proxy which domains to proxy
          - VIRTUAL_PORT=8000 # tells nginx-proxy which port to proxy
          networks:
          proxy:
          external:
          name: ubuntu_default


          If you do not override the default network, docker will create one for your compose project and use it for any containers not assigned to another network.






          share|improve this answer
























          • wow. i didnt know that about the load balancing. thats super useful now that i know it exists and its definitely what was happening. thank you for the knowledge.

            – oooyaya
            Jan 4 at 13:50
















          1












          1








          1







          Docker containers communicate using DNS lookups on their network. If multiple containers have the same alias on the same network, it will round robin load balance between the containers with each network connection. If you don't want containers to talk to each other, then you don't want them on the same docker network. The good news is you solve this by using more than one network, and not putting the api and db server on the frontend proxy network:



          version: '3'
          services:
          db:
          build: "./db" # postgres
          volumes:
          - "./data:/var/lib/postgresql/data"
          restart: "always"
          api:
          build: "./api" # a python backend
          ports:
          - "9000:9000"
          restart: "always"
          depends_on:
          - db
          ui:
          build: "./ui" # a react front end
          ports:
          - "8000:8000"
          restart: "always"
          networks:
          - default
          - proxy
          environment:
          - VIRTUAL_HOST=testing.com,www.testing.com # tells nginx-proxy which domains to proxy
          - VIRTUAL_PORT=8000 # tells nginx-proxy which port to proxy
          networks:
          proxy:
          external:
          name: ubuntu_default


          If you do not override the default network, docker will create one for your compose project and use it for any containers not assigned to another network.






          share|improve this answer













          Docker containers communicate using DNS lookups on their network. If multiple containers have the same alias on the same network, it will round robin load balance between the containers with each network connection. If you don't want containers to talk to each other, then you don't want them on the same docker network. The good news is you solve this by using more than one network, and not putting the api and db server on the frontend proxy network:



          version: '3'
          services:
          db:
          build: "./db" # postgres
          volumes:
          - "./data:/var/lib/postgresql/data"
          restart: "always"
          api:
          build: "./api" # a python backend
          ports:
          - "9000:9000"
          restart: "always"
          depends_on:
          - db
          ui:
          build: "./ui" # a react front end
          ports:
          - "8000:8000"
          restart: "always"
          networks:
          - default
          - proxy
          environment:
          - VIRTUAL_HOST=testing.com,www.testing.com # tells nginx-proxy which domains to proxy
          - VIRTUAL_PORT=8000 # tells nginx-proxy which port to proxy
          networks:
          proxy:
          external:
          name: ubuntu_default


          If you do not override the default network, docker will create one for your compose project and use it for any containers not assigned to another network.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 1:53









          BMitchBMitch

          65.6k10142161




          65.6k10142161













          • wow. i didnt know that about the load balancing. thats super useful now that i know it exists and its definitely what was happening. thank you for the knowledge.

            – oooyaya
            Jan 4 at 13:50





















          • wow. i didnt know that about the load balancing. thats super useful now that i know it exists and its definitely what was happening. thank you for the knowledge.

            – oooyaya
            Jan 4 at 13:50



















          wow. i didnt know that about the load balancing. thats super useful now that i know it exists and its definitely what was happening. thank you for the knowledge.

          – oooyaya
          Jan 4 at 13:50







          wow. i didnt know that about the load balancing. thats super useful now that i know it exists and its definitely what was happening. thank you for the knowledge.

          – oooyaya
          Jan 4 at 13:50






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014196%2fdocker-mis-forwarding-ports%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas