我有两个独立的项目,第一个是使用drupal创建的api,用于暴露json数据,第二个是连接到该api的nuxt项目。 问题是当我尝试获取文章列表时,出现错误:
Error: connect ECONNREFUSED 127.0.0.1:8000
Vuejs代码
try {
const articles = await
axios.get(`http://localhost:8000/jsonapi/node/article`)
return { articles: articles.data.rows }
} catch (e) {
error({ message: "Pas d'articles" + e, statusCode: 404 })
}
docker文件(nuxt)
version: "3"
services:
nuxt:
build: ./app/
container_name: nuxt
restart: always
ports:
- "3000:3000"
command:
"npm run start"
nginx:
image: nginx:1.13
container_name: nginx
ports:
- "8081:80"
volumes:
- ./nginx:/etc/nginx/conf.d
depends_on:
- nuxt
Drupal api的Docker文件:
version: "3"
services:
mariadb:
image: wodby/mariadb:$MARIADB_TAG
container_name: "${PROJECT_NAME}_mariadb"
stop_grace_period: 30s
environment:
MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
MYSQL_DATABASE: $DB_NAME
MYSQL_USER: $DB_USER
MYSQL_PASSWORD: $DB_PASSWORD
php:
image: wodby/drupal-php:$PHP_TAG
container_name: "${PROJECT_NAME}_php"
environment:
PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
DB_HOST: $DB_HOST
DB_USER: $DB_USER
DB_PASSWORD: $DB_PASSWORD
DB_NAME: $DB_NAME
DB_DRIVER: $DB_DRIVER
PHP_FPM_USER: wodby
PHP_FPM_GROUP: wodby
COLUMNS: 80
volumes:
- ./:/var/www/html
nginx:
image: wodby/nginx:$NGINX_TAG
container_name: "${PROJECT_NAME}_nginx"
depends_on:
- php
environment:
NGINX_STATIC_OPEN_FILE_CACHE: "off"
NGINX_ERROR_LOG_LEVEL: debug
NGINX_BACKEND_HOST: php
NGINX_SERVER_ROOT: /var/www/html/web
NGINX_VHOST_PRESET: $NGINX_VHOST_PRESET
volumes:
- ./:/var/www/html
labels:
- 'traefik.backend=${PROJECT_NAME}_nginx'
- 'traefik.port=80'
- 'traefik.frontend.rule=Host:${PROJECT_BASE_URL}'
mailhog:
image: mailhog/mailhog
container_name: "${PROJECT_NAME}_mailhog"
labels:
- 'traefik.backend=${PROJECT_NAME}_mailhog'
- 'traefik.port=8025'
- 'traefik.frontend.rule=Host:mailhog.${PROJECT_BASE_URL}'
portainer:
image: portainer/portainer
container_name: "${PROJECT_NAME}_portainer"
command: --no-auth -H unix:///var/run/docker.sock
volumes:
- /var/run/docker.sock:/var/run/docker.sock
labels:
- 'traefik.backend=${PROJECT_NAME}_portainer'
- 'traefik.port=9000'
- 'traefik.frontend.rule=Host:portainer.${PROJECT_BASE_URL}'
traefik:
image: traefik
container_name: "${PROJECT_NAME}_traefik"
command: -c /dev/null --web --docker --logLevel=INFO
ports:
- '8000:80'
volumes:
- /var/run/docker.sock:/var/run/docker.sock
我有两个独立的环境,一个用于drupal api(后端),第二个用于nuxt项目(前端)
答案 0 :(得分:0)
好像您的端口映射是错误的。在docker compose中,您正在将nginx端口8081映射到计算机的端口80。也许您需要映射8081:8000,前提是您的nginx配置实际上在端口8081上启动了一项服务,并将适当的代理重定向到nuxt主机(nuxt:3000)。
或者也许您的vuejs代码需要将端口更新为http://localhost/,因为您正在映射到撰写文件中的端口80。
答案 1 :(得分:0)
在容器内部,localhost或127.0.0.1映射到容器,而不是外部主机,除非您关闭网络名称空间(不建议)。我们在您的问题中看不到该应用程序在8000上监听,因此一般建议如下:
在容器之间,您需要将两个容器放置在同一用户创建的docker网络上,以主机名连接到容器或服务名称,并连接到应用程序端口,而不是主机上的已发布端口。默认情况下,Compose会为您设置网络。从“ nuxt”中可以curl http://nginx:80/
开始。
要从容器与不在容器上运行的主机上的应用进行对话,请将该应用移到容器中并按照上述步骤操作,或者连接到主机的DNS或IP而不是本地主机。
根据您的修改,以上内容适用。您可以将两个项目分开,然后将主机名/ IP更改为主机的名称/ IP。或者,您可以通过共享网络连接两个项目,例如:
docker network create backend
然后将共享网络添加到每个撰写文件:
version: "3"
services:
nuxt:
build: ./app/
container_name: nuxt
restart: always
ports:
- "3000:3000"
networks:
- default
- backend
command:
"npm run start"
nginx:
image: nginx:1.13
container_name: nginx
ports:
- "8081:80"
volumes:
- ./nginx:/etc/nginx/conf.d
depends_on:
- nuxt
networks:
backend:
external: true
对于其他项目:
version: "3"
services:
mariadb:
image: wodby/mariadb:$MARIADB_TAG
container_name: "${PROJECT_NAME}_mariadb"
stop_grace_period: 30s
environment:
MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
MYSQL_DATABASE: $DB_NAME
MYSQL_USER: $DB_USER
MYSQL_PASSWORD: $DB_PASSWORD
php:
image: wodby/drupal-php:$PHP_TAG
container_name: "${PROJECT_NAME}_php"
environment:
PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
DB_HOST: $DB_HOST
DB_USER: $DB_USER
DB_PASSWORD: $DB_PASSWORD
DB_NAME: $DB_NAME
DB_DRIVER: $DB_DRIVER
PHP_FPM_USER: wodby
PHP_FPM_GROUP: wodby
COLUMNS: 80
volumes:
- ./:/var/www/html
nginx:
image: wodby/nginx:$NGINX_TAG
container_name: "${PROJECT_NAME}_nginx"
depends_on:
- php
environment:
NGINX_STATIC_OPEN_FILE_CACHE: "off"
NGINX_ERROR_LOG_LEVEL: debug
NGINX_BACKEND_HOST: php
NGINX_SERVER_ROOT: /var/www/html/web
NGINX_VHOST_PRESET: $NGINX_VHOST_PRESET
volumes:
- ./:/var/www/html
labels:
- 'traefik.backend=${PROJECT_NAME}_nginx'
- 'traefik.port=80'
- 'traefik.frontend.rule=Host:${PROJECT_BASE_URL}'
mailhog:
image: mailhog/mailhog
container_name: "${PROJECT_NAME}_mailhog"
labels:
- 'traefik.backend=${PROJECT_NAME}_mailhog'
- 'traefik.port=8025'
- 'traefik.frontend.rule=Host:mailhog.${PROJECT_BASE_URL}'
portainer:
image: portainer/portainer
container_name: "${PROJECT_NAME}_portainer"
command: --no-auth -H unix:///var/run/docker.sock
volumes:
- /var/run/docker.sock:/var/run/docker.sock
labels:
- 'traefik.backend=${PROJECT_NAME}_portainer'
- 'traefik.port=9000'
- 'traefik.frontend.rule=Host:portainer.${PROJECT_BASE_URL}'
traefik:
image: traefik
container_name: "${PROJECT_NAME}_traefik"
command: -c /dev/null --web --docker --logLevel=INFO
ports:
- '8000:80'
networks:
- default
- backend
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
backend:
external: true
然后将您的连接从localhost:8000
更改为traefik:80
。