1

I have three containers :

  • front (vuejs)
  • server (nodejs)
  • mongo (mongodb)

Communication between server <-> mongo is OK but communication between front -> server is KO (a front http call to server cannot reach).

Here my docker-compose :

version: "3.3"

services:
  server:
    build:
      context: ../server
    command: nodemon ../server/bin/www
    volumes:
      - ../server:/server
    ports:
      - "3000:3000"
    networks:
      - frontend
      - backend
    depends_on:
      - mongo
  front:
    build:
      context: ../front
    command: npm run dev
    volumes:
      - ../front:/app
    ports:
      - "8081:8081"
    networks:
      - frontend
    depends_on:
      - server
  mongo:
    image: mongo:3.6.5
    ports:
      - "27017:27017"
    networks:
      - backend
    environment:
      MONGO_INITDB_DATABASE: mongo-dev
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: password
    command: mongod

networks:
  frontend:
  backend:

Running docker-compose up is OK : Mongo is created + server is running + front is running.

I can access them from my browser (http://localhost:3000 for server + http://localhost:8081 for front) but impossible for my front to get a simple http GET to my server with

http://server:3000/myGetRoute

Note : this route is reachable from my browser with

http://localhost:3000/myGetRoute

and ping server from my front container is OK.

Communication from front -> server is impossible. Any idea ?

EDIT : my front service running on : http://0.0.0.0:8081)

Chris
  • 234
  • 3
  • 16
  • 1
    of course, you are outside the docker network. you have to use "localhost" not "server". "server" is just a name for docker but your localhost has no idea about what "server" is. – ius May 31 '18 at 14:02
  • http://server:3000/myGetRoute is the route used by my front app. My front app should know 'server' because it's the name of the service defined in my docker-compose and server is in the same network as front. No ? – Chris May 31 '18 at 14:18
  • 2
    actually no, your node process is running in docker but the vue client is in your localhost (in your browser) not in docker. Did you tried to access to the server from your browser with server:3000/myGetRoute? i don't think it will work. This is exactly what your your vue client is trying to do. You should use in your client localhost:3000/myGetRoute. I think that way should work. – ius May 31 '18 at 14:31
  • your are right ! with my front which call 'localhost' it's working. Thanks ! But my front is running into a container, not in my local machine : '**front_1** | I Your application is running here: http://0.0.0.0:8081' => I don't really understand. I want access from my browser to front in container, to simulate real production condition. – Chris May 31 '18 at 14:55
  • 1
    If you want to access it you have to use localhost:3000. One option you could do is to create a nginx container that works as proxy. He should decide when the requests has to be handled by your client or the server. This way you would have both containers encapsulated in one port. Then tell nginx that all under 'api' its your server and all other stuff is your client. Vue should be able then to call your api just like that "/api/myGetRoute" without need to use the full URL. This has the advantage that you wont have to worry about the CORS (important if you want to make POST/PUT/DELETE calls). – ius May 31 '18 at 15:33

1 Answers1

-1

You might want to try links instead of depends_on . Also see Difference between links and depends_on in docker_compose.yml for further elaboration.