0

I have two containers: one for Cypress and another for my web app. I have them both set up in a docker-compose.yml file like so:

version: '3.2'
services:
  pa-portal:
    image: web_app_image
    container_name: pa_portal
    volumes:
      - productDB:/web_app/db
    ports:
      - "8080:8080"
  cypress:
    image: "cypress/included:4.4.0"
    depends_on:
      - pa-portal
    environment:
      - CYPRESS_baseUrl=http://pa-portal:8080
    working_dir: /cypress-testing

    volumes:
      - ./:/cypress-testing

volumes:
  productDB:

From the Cypress (testing framework) container I am able to access the web app using http://pa-portal:8080 but from a browser on my host the only way I can access the web app that has been launched by the pa_portal container is using localhost:8080.

Why are there different urls depending on where I am accessing from?

Is there some fundamental knowledge i need to do some research on

4
  • If you were able to access the container in that way, why would you need those port mappings? Those DNS names are resolvable only within a docker environment. Commented Aug 5, 2020 at 18:15
  • @MatusDubrava I have edited the question - it's not that I want to, I am just wondering why Commented Aug 5, 2020 at 18:37
  • 1
    Answering that fundamental knowledge question - you need to know how DNS works in general (DNS servers and local config, ex /etc/hosts and /etc/resolv.conf). Then you need to understand how Docker service discovery works. Commented Aug 5, 2020 at 19:08
  • And the question about different urls - because those (your localhost and docker environment) are different networks with their own rules. Commented Aug 5, 2020 at 19:10

1 Answer 1

1

Everything is working as designed.

The service name is just a redirect WITHIN the docker-infrastructure. It doesn't work like a hosts-entry for outside of this scope.

To get what you want look into Traefik .
You can set it up with a docker container, add labels to your docker compose and with that traeffik will route your localhost to the given domainname you want.

I googled a simple howto for that, but the traefik docs are fine too: https://www.digitalocean.com/community/tutorials/how-to-use-traefik-as-a-reverse-proxy-for-docker-containers-on-ubuntu-16-04

Sign up to request clarification or add additional context in comments.

3 Comments

I have edited the question - I am wondering more on the why not the how
Well, because it is what it is: Docker is a software on your computer, it doesnt change your HOSTS file, so basicly your Browser doesnt know your domain "pa_portal". That is not what docker do and is there by design. With traefik you have a software exactly for that :)
Traefik also doesn't edit hosts entries. It's just acting as a proxy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.