最近我正在使用一个MERN堆栈应用程序。然后我有一个docker-compose.yml,看起来像这样:
version: "3"
services:
web:
build: ./concept.io-frontend
ports:
- "80:80"
depends_on:
- api
networks:
- network.concept.io
container_name: concept.io
api:
build: ./concept.io-backend
restart: always
ports:
- "5328:5328"
depends_on:
- db
networks:
- network.concept.io
container_name: api.concept.io
db:
image: mongo
restart: always
networks:
- network.concept.io
container_name: db.concept.io
networks:
network.concept.io:
driver: bridge
因此,在后端(Express)代码中,我写道:
const URL = "mongodb://db.concept.io:27017/conceptio";
module.exports = {
url: URL,
};
当我声明API容器依赖于Mongo容器时,API容器与Mongo one完美连接。
但当涉及到前端容器时,我复制了相同的设置,该前端容器依赖于API容器,并将"localhost“替换为API和Mongo容器的容器名称。
// const URL = "http://localhost:5328/";
const URL = "http://api.concept.io:5328/";
export const API_URL = URL + "api/";
export const AUTH_URL = API_URL + "auth/";
但我不能向该端点发出请求。
向Docker容器的同一网络中的另一个容器发出HTTP请求有什么错误或想法吗?
以下是Dockerfiles:
接口:
FROM node:lts-alpine3.12 as builder
RUN apk add yarn
WORKDIR /opt/app
COPY package.json yarn.lock ./
RUN yarn install
COPY . ./
EXPOSE 5328
CMD ["yarn", "start"]
前端:
FROM node:lts-alpine3.12 AS builder
RUN apk add yarn
WORKDIR /opt/web
COPY package.json yarn.lock ./
RUN yarn install
ENV PATH="./node_modules/.bin:$PATH"
COPY . ./
RUN yarn run build
FROM nginx:1.19.1-alpine
RUN apk add curl
RUN curl -L https://github.com/a8m/envsubst/releases/download/v1.2.0/envsubst-`uname -s`-`uname -m` -o envsubst && \
chmod +x envsubst && \
mv envsubst /usr/local/bin
COPY ./nginx.config /etc/nginx/nginx.template
EXPOSE 80
CMD ["/bin/sh", "-c", "envsubst < /etc/nginx/nginx.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"]
COPY --from=builder /opt/web/dist /usr/share/nginx/html
发布于 2020-08-18 07:26:18
您必须在docker-compose.yml文件中添加虚拟主机
环境: VIRTUAL_HOST: concept.io
https://stackoverflow.com/questions/63463677
复制