3

I have a three tier application on Docker tat consists of the following: 1. An Adonis app to server as the api for the application 2. A MongoDB database 3. A NuxtJS app for the client application.

When I run the application locally with docker-compose up it seems to start up just fine. However, when I try to access the api with postman (localhost:3333) or try to access the Next app on my browser (localhost:3000/), I get a "Could not get any response" error and a "Cannot open the page error" respectively.

I have tried exposing the ports within each component's Dockerfile, in addition to specifying port mappings on the docker-compose.yml file.

Here is my Dockerfile for the API application

# The API application

FROM node:alpine
WORKDIR home/api

COPY ./server-api/package.json .
RUN npm install
COPY  ./server-api .

EXPOSE 3333

CMD ["npm", "start"]

Here is the Dockerfile for the NuxtJS application

# The Web application

FROM node:latest
WORKDIR home/app

COPY ./web-client/package.json .
RUN npm install
COPY ./web-client .

EXPOSE 3000

CMD ["npm", "start"]

Here is my docker-compose.yml file

version: '3'

services:

  api:
    build: ./server
    restart: always
    ports:
      - "3333:3333"

  mongodb:
    image: 'mongo'
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: NoneOfYourBusiness

  web:
    build: ./web
    restart: always
    ports:
      - "3000:3000"

When I send a GET request to "localhost:3333/" I expect to get a response of "test". However, I am getting a "Could not get any response" error from Postman instead.

When I go to "localhost:3000/" on by browser, I expect to get a page. Instead, it is saying it cannot connect to the server.

2
  • localhost is always "this container". Use the other container's name in the docker-compose.yml file (e.g., api) as a host name instead. Commented May 13, 2019 at 1:16
  • So, your saying in my browser/postman, I should call api:3333 and web:3000/ instead of localhost:3333 and localhost:3000? Commented May 13, 2019 at 1:22

2 Answers 2

2

If you want to access using localhost, you should share your host network interfaces with dockers. Otherwise, lo iface is different. Definitively, try with this docker-compose:

version: '3'

services:

  api:
    build: ./server
    restart: always
    network_mode: "host"
    ports:
      - "3333:3333"

  mongodb:
    image: 'mongo'
    restart: always
    network_mode: "host"
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: NoneOfYourBusiness

  web:
    build: ./web
    restart: always
    network_mode: "host"
    ports:
      - "3000:3000"
Sign up to request clarification or add additional context in comments.

Comments

1

From your machine:

You should be able to use localhost:3333 with your current setup. You can double check if a program is listening on the port 3333 (lsof -i:3333 on linux).

From inside your docker:

Set up networks and use the container names to call them, example

version: '3'

networks:
    back:

services:

  api:
    build: ./server
    restart: always
    ports:
      - "3333:3333"
    networks:
      - back
  mongodb:
    image: 'mongo'
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: NoneOfYourBusiness
    networks:
      - back
  web:
    build: ./web
    restart: always
    ports:
      - "3000:3000"
    networks:
      - back

From the web container, you could call api:3333 for the api.

BTW, you don't need the EXPOSES 3333 in the Dockerfile since it's already done in the docker-compose file.

Comments

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.