ghvint
ghvint

Reputation: 21

How to set up an exposed docker frontend and a non-exposed backend?

I have a simple docker network set up with two containers. The frontend has its port mapped 80:80 so that I can access it via localhost in my browser. My backend does not have its port (3000) mapped. I want my frontend to be accessible on my network but do not want my backend to be.

The problem I have run into is that I cannot make requests from my frontend to my backend from the browser. Both containers are on the proper network and can communicate internally.

ex: using curl inside the frontend container provides the expected output from the backend:

curl 10.5.0.4:3000/test

But calling axios.get(10.5.0.4:3000/test) from the webpage will result in a 404 error.

Is the problem that the axios request is coming from the browser and therefore cannot connect to the docker network? If so is there a solution that does not involve making the backend to be accessible outside the docker network?

Here is my docker-compose file for clarity

services:
  backend:
    build: ./testbe
    image: testbe:dev
    networks:
      test_net:
        ipv4_address: 10.5.0.4

  frontend:
    build: ./testfe
    image: testfe:dev
    networks:
      test_net:
        ipv4_address: 10.5.0.5
    ports:
      - "80:80"


networks:
  test_net:
    driver: bridge
    driver_opts:
      com.docker.network.endable_ipv6: "false"
    ipam:
      driver: default
      config:
        - subnet: 10.5.0.0/5
          gateway: 10.5.0.1

Upvotes: 2

Views: 654

Answers (1)

Faeeria
Faeeria

Reputation: 826

You shouldn't use the private IP of the container to call it, but call the name it has on the network.

Upvotes: 1

Related Questions