【发布时间】:2021-01-26 16:32:20
【问题描述】:
我需要在 docker 容器之间设置代理。在我的变体中,容器可以从主机访问,但它们不能互相看到。
通过 curl 检查容器的可用性。
我做错了什么?如何正确?
docker-compose.yml
version: "2.4"
services:
nginx:
image: nginx:1.17.2-alpine
container_name: nginx
volumes:
- ./default.nginx:/etc/nginx/conf.d/default.conf
ports:
- 8989:8989
frontend:
container_name: frontend
environment:
- VIRTUAL_HOST=localhost:3000
build:
context: ./frontend
dockerfile: Dockerfile
ports:
- 3000:3000
backend:
container_name: backend
environment:
- VIRTUAL_HOST=localhost:1337
build:
context: ./backend
dockerfile: Dockerfile
ports:
- 1337:1337
正面
FROM keymetrics/pm2:latest-alpine
ENV NPM_CONFIG_LOGLEVEL error
WORKDIR /usr/src/app
RUN apk update && apk upgrade && apk add --no-cache bash git openssh
RUN apk --no-cache add curl
COPY . .
RUN yarn install
RUN yarn build:ssr
EXPOSE 3000 1337
CMD [ "pm2-runtime", "start", "pm2.json" ]</blockquote>
back
<blockquote>FROM strapi/base:latest
RUN yarn global add strapi
RUN mkdir /srv/app && chown 1000:1000 -R /srv/app
RUN apk --no-cache add curl
WORKDIR /srv/app
EXPOSE 1337 3000
COPY . .
RUN yarn
# CMD ["strapi", "develop"]
CMD ["strapi", "start"]
default.nginx
server {
listen 8989;
server_name localhost;
location / {
proxy_pass http://frontend:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location localhost:3000 {
proxy_pass http://frontend:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location localhost:3000/graphql {
proxy_pass http://backend:1337/graphql;
# proxy_pass http://backend:1337;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location ^~/graphql {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Frame-Options SAMEORIGIN;
# proxy_pass http://backend:1337/graphql;
proxy_pass http://localhost:1337/graphql;
}
location localhost:1337 {
proxy_pass http://backend:1337;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# location ~ /\.ht {
# deny all;
# }
}
我在这件事上寻求帮助,因为我是初学者。如果是我任务的现成配置,请提供。
【问题讨论】:
标签: docker nginx docker-compose dockerfile devops