0

I am running a SpringBoot application in a docker container and another VueJS application in another docker container using docker-compose.yml as follows:

version: '3'
services:
  backend:
    container_name: backend
    build: ./backend
    ports:
      - "28080:8080"

  frontend:
    container_name: frontend
    build: ./frontend
    ports:
      - "5000:80"
    depends_on:
      - backend

I am trying to invoke SpringBoot REST API from my VueJS application using http://backend:8080/hello and it is failing with GET http://backend:8080/hello net::ERR_NAME_NOT_RESOLVED.

Interestingly if I go into frontend container and ping backend it is able to resolve the hostname backend and I can even get the response using wget http://backend:8080/hello.

Even more interestingly, I added another SpringBoot application in docker-compose and from that application I am able to invoke http://backend:8080/hello using RestTemplate!!

My frontend/Dockerfile:

FROM node:9.3.0-alpine
ADD package.json /tmp/package.json
RUN cd /tmp && yarn install
RUN mkdir -p /usr/src/app && cp -a /tmp/node_modules /usr/src/app
WORKDIR /usr/src/app
ADD . /usr/src/app
RUN npm run build
ENV PORT=80
EXPOSE 80
CMD [ "npm", "start" ]

In my package.json I mapped script "start": "node server.js" and my server.js is:

const express = require('express')
const app = express()
const port = process.env.PORT || 3003
const router = express.Router()

app.use(express.static(`${__dirname}/dist`)) // set the static files location for the static html
app.engine('.html', require('ejs').renderFile)
app.set('views', `${__dirname}/dist`)
router.get('/*', (req, res, next) => {
  res.sendFile(`${__dirname}/dist/index.html`)
})
app.use('/', router)
app.listen(port)
console.log('App running on port', port)

Why is it not able to resolve hostname from the application but can resolve from the terminal? Am I missing any docker or NodeJS configuration?

2 Answers 2

2

Finally figured it out. Actually, there is no issue. When I run my frontend VueJS application in docker container and access it from the browser, the HTML and JS files will be downloaded on my browser machine, which is my host, and the REST API call goes from the host machine. So from my host, the docker container hostname (backend) is not resolved.

The solution is: Instead of using actual docker hostname and port number (backend:8080) I need to use my hostname and mapped port (localhost:28080) while making REST calls.

Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest:

  1. docker ps to get the names/Ids of running containers
  2. docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' BACKEND_CONTAINER_NAME to get backend container's IP address from the host.

Now put this IP in the front app and it should be able to connect to your backend.

3 Comments

But the IP Address won't be same everytime we spin up the container right?
Even using IP address also I am getting the same issue.
What happened when you try telnet http://backend 8080 and telnet CONTAINER_IP 8080 from your host ?

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.