how to access node api which is running as docker container

Viewed 1187

I have built a docker-compose file for my node js application that has been dockerized, But I don't know how to make the API call to that node js app which is running as a docker container, Please help me with this concern.

My DockerFile:

FROM node:10.15-slim

ENV NODE_ENV=production

WORKDIR /app

COPY package.json package-lock*.json ./

RUN npm install && npm cache clean --force

COPY . .

CMD ["node", "./bin/www"]

My Docker-compose file:

version: '2.4'

services:
  express:
    build: 
      context: .
      dockerfile: Dockerfile
    command: /app/node_modules/.bin/nodemon ./bin/www
    ports:
      - 3000:3000
    volumes:
      - .:/app
    environment:
      - DEBUG=sample-express:*
      - NODE_ENV=development
1 Answers

You'll need to expose the port from docker on which your application is running. Let's say your application is running on port 8080 inside docker, here's how you can expose that specific port:

EXPOSE 8080

Then you'll need to map the port exposed by docker tthato your local port. Here's how you can do it in docker:

docker run -p 49160:8080 -d docker_image

And if you're working with docker-compose, you'll do it like this:

version: '3'

services:
  nodejs:
    build:
      context: .
      dockerfile: Dockerfile
    image: nodejs
    container_name: nodejs
    ports:
      - "8080:8080"

UPDATE

Let's say you want to send /api requests to back-end server. This is how you'll do it in nginx conf:

server {
  listen 80

  location /api {
      proxy_pass http://backend:8080/;
   }
} 

I hope it helps.

Related