I'm building a project in Node Mongo and using Docker plus Nginx to run all on my local machine MacBook pro-Catalina. I have several issues I'm unable to fix on my own and I don't know how to configure Nginx and Docker to make all work correctly. At the end of the post will be shown my Dockerfile, Docker compose and Nginx.
failed to connect to server [127.0.0.1:27017] on first connect
but via a GUI as compass I'm able to connect to the same URL.failed to connect to server [wetaxitask_mongodb:27017] on first connect [Error: connect ECONNREFUSED 172.18.0.3:27017
Dockerfile:
# Developpment stage
FROM node:12.18-alpine AS base-builder
RUN apk update
RUN apk add --no-cache python make g++
RUN apk add --no-cache libc6-compat
WORKDIR /usr/src/app
COPY ["tsconfig.json", "package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
ADD . /usr/src/app
COPY ./.env /usr/src/app/.env
RUN npm cache clean --force
RUN npm install
# ------------------------------------------------------
# Production Build
# ------------------------------------------------------
FROM nginx:1.19.0-alpine
RUN apk add --no-cache --repository http://nl.alpinelinux.org/alpine/edge/main libuv \
&& apk add --update nodejs npm
WORKDIR /usr/src/app
RUN ls -l /usr/src/app/
COPY --from=0 /usr/src/app/ .
RUN rm /etc/nginx/conf.d/default.conf
RUN rm -rf /docker-entrypoint.d
COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 3000
EXPOSE 80
#CMD ["nginx", "-g", "daemon off;","npm", "start"]
CMD nginx ; exec npm start
Docker compose:
version: '3.8'
services:
wetaxitask:
container_name: wetaxitask_api_dev
image: wetaxitask
restart: always
build: .
depends_on:
- mongodb
- redis
env_file: .env
ports:
- 8080:80
links:
- redis
- mongodb
mongodb:
container_name: wetaxitask_mongodb
image: mongo:latest
restart: always
ports:
- 27017:27017
volumes:
- ./data:/data/db
environment:
- MONGO_INITDB_DATABASE=wetaxitask
redis:
container_name: wetaxitask_redis
image: redis:latest
ports:
- 6379:6379
Nginx:
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /welcome {
proxy_pass http://127.0.0.1:3000;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Try to change the hostname from localhost(127.0.0.1) to mongodb (the name of your service defined in docker-compose). Docker dns magic will do the rest.
edit: add them to a common network in your docker compose and the containers should be able to communicate with their name as hostname in the requests
version: '3.8'
services:
wetaxitask:
container_name: wetaxitask_api_dev
image: wetaxitask
restart: always
build: .
depends_on:
- mongodb
- redis
env_file: .env
ports:
- 8080:80
links:
- redis
- mongodb
networks:
- my-network
mongodb:
container_name: wetaxitask_mongodb
image: mongo:latest
restart: always
ports:
- 27017:27017
volumes:
- ./data:/data/db
environment:
- MONGO_INITDB_DATABASE=wetaxitask
networks:
- my-network
redis:
container_name: wetaxitask_redis
image: redis:latest
ports:
- 6379:6379
networks:
- my-network
networks:
my-network:
#this should define a network called my-network with default settings
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.