Loopback-next: How to expose graphql oasgraph in Docker image?

Created on 12 Jul 2019  路  5Comments  路  Source: strongloop/loopback-next

I am creating an application in loopback 4, using graphql.

The problem I have is that I can not expose graphql oasgraph in Docker image

I have two options:
1 - Create a new dockerfile to expose graphql.
2 - Use the same loopback dockerfile to expose port 3000 (Explorer API at http://127.0.0.1:3000/explorer), and 3001 for graphql (http://127.0.0.1:3001/graphql).

How can I configure my dockerfile, in order to expose the graphql server?

https://loopback.io/doc/en/lb4/exposing-graphql-apis.html

This is my dockerfile

FIRST OPTION

# Check out https://hub.docker.com/_/node to select a new base image
FROM hasura/graphql-engine

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app

WORKDIR /home/node/app

RUN npm i -g openapi-to-graphql-cli

RUN npx oasgraph http://localhost:3000/openapi.json/

# Bind to all network interfaces so that it can be mapped to the host OS
ENV HOST=0.0.0.0 PORT=3001

EXPOSE ${PORT}

CMD [ "npx", "oasgraph", "http://localhost:3000/openapi.json/" ]

SECOND OPTION:

# Check out https://hub.docker.com/_/node to select a new base image
##FROM node:10-slim
## node con oracle instant client
FROM collinestes/docker-node-oracle

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app

WORKDIR /home/node/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY --chown=node package*.json ./

RUN npm install
# Install openapi-to-graphql-cli 
RUN npm i openapi-to-graphql-cli


# Bundle app source code
COPY --chown=node . .

RUN npm run build

# RUN oasgraph 
RUN npx oasgraph http://localhost:3000/openapi.json/

# Bind to all network interfaces so that it can be mapped to the host OS
ENV HOST=0.0.0.0 PORT=3000

EXPOSE ${PORT}

CMD [ "node", "." ]
question

Most helpful comment

@fga8, I might have a working Dockerfile for you:

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:10-slim

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app

WORKDIR /home/node/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY --chown=node package*.json ./

RUN npm install
RUN npm install openapi-to-graphql-cli

# Bundle app source code
COPY --chown=node . .

RUN npm run build

# Bind to all network interfaces so that it can be mapped to the host OS
ENV HOST=0.0.0.0 PORT=3000 PORT2=3001 .  <--- Change this line to add PORT2 here

EXPOSE ${PORT}
EXPOSE ${PORT2} <----- Add this line


CMD (npm start &) ; (sleep 5) ; (npx openapi-to-graphql http://0.0.0.0:3000/openapi.json -p 3001) <--- Change this line

I've indicated 3 places that you need to make changes if you're using the Dockerfile generated by the LoopBack cli. Please note that I'm not a docker expert, so what I have above is something that works, you might be able to find a better solution.

For the sake of posterity, let me mention a few challenges:

  1. oasgraph has been deprecated and the new module is openapi-to-graphql
  2. In order to make sure openapi-to-graphql is not using the same port as LB, I've used -p option to specify the port number. (Thanks @ErikWittern)
  3. I find the sleep in the CMD is needed, otherwise openapi-to-graphql will get started before the openapi is ready from LB.

Hope it helps!

All 5 comments

@fga8, are you running into problems for either options? By looking at it, it seems like option2 is promising. If you could let us know what's the issue you're running, we can try to help. Thanks.

@fga8, I might have a working Dockerfile for you:

# Check out https://hub.docker.com/_/node to select a new base image
FROM node:10-slim

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app

WORKDIR /home/node/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY --chown=node package*.json ./

RUN npm install
RUN npm install openapi-to-graphql-cli

# Bundle app source code
COPY --chown=node . .

RUN npm run build

# Bind to all network interfaces so that it can be mapped to the host OS
ENV HOST=0.0.0.0 PORT=3000 PORT2=3001 .  <--- Change this line to add PORT2 here

EXPOSE ${PORT}
EXPOSE ${PORT2} <----- Add this line


CMD (npm start &) ; (sleep 5) ; (npx openapi-to-graphql http://0.0.0.0:3000/openapi.json -p 3001) <--- Change this line

I've indicated 3 places that you need to make changes if you're using the Dockerfile generated by the LoopBack cli. Please note that I'm not a docker expert, so what I have above is something that works, you might be able to find a better solution.

For the sake of posterity, let me mention a few challenges:

  1. oasgraph has been deprecated and the new module is openapi-to-graphql
  2. In order to make sure openapi-to-graphql is not using the same port as LB, I've used -p option to specify the port number. (Thanks @ErikWittern)
  3. I find the sleep in the CMD is needed, otherwise openapi-to-graphql will get started before the openapi is ready from LB.

Hope it helps!

@federicogomeza, we believe we've solved your issue. Please feel free to ping me if you're still running this issue. Thanks.

@federicogomeza , creemos que hemos resuelto su problema. Por favor, si茅ntase libre de enviarme un ping si a煤n tiene este problema. Gracias.

Thanks Diana for the help, at the moment I am using loopback 4 without graphql, since I need it for a productive application, and for the moment this implementation is lacking community. In the future I hope to be able to use graphql again :)

Was this page helpful?
0 / 5 - 0 ratings