0

I'm trying to dockerize an existing app using Angular for the frontend, node.js as a backend and Postgres as DB. I've created a network and tried to run the containers one by one without the DB for now but I get an error with the node.js backend.

I've built the backend image with this Dockerfile:

FROM node:10.17.0-alpine

WORKDIR /app

COPY package*.json ./

RUN npm i

COPY . .

EXPOSE 3000

CMD node --experimental-worker backend.js

and the frontend one with this:

FROM node:10.17

RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get install -yq google-chrome-stable

WORKDIR /app

ENV PATH /app/node_modules/.bin:$PATH

COPY package.json /app/package.json
RUN npm install
RUN npm install -g @angular/[email protected]

COPY . /app

EXPOSE 4200
CMD ng serve --host 0.0.0.0

I've built the images and I've started the containers with:

docker container run --network my-network -it -p 4200:4200 frontend
docker container run --network my-network -it -p 3000:3000 backend
docker container run --network my-network -it --name my-redis -p 6379:6379 redis

but the backend relies on redis to start so it fails with the error:

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)

I've tried:

docker container run --network my-network --link my-redis:redis -it -p 3000:3000

but it doesn't help at all. I'm sorry but I am very new to Docker so any clarification would be useful.

1
  • localhost(127.0.0.1) means the container itself, not the host on which redis is running. Commented Nov 5, 2019 at 11:57

1 Answer 1

1

Your backend service is trying to connect to 127.0.0.1:6379, however it should be like my-redis:6379.

Basically, you need to inject the redis host to your backend service. There is multiple ways to do so, but the most common way is to read it from ENV variable (e.g. REDIS_HOST=my-redis)

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

3 Comments

Thanks, I managed to change the host and the port but the error doesn't change much. it is now failing with "ECONNREFUSED my-redis:6379" instead of 127.0.0.1:6379.
I think your redis is blocking connection, so you might need to twist redis configuration again.
How do you twist redis configuration?

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.