无法在Docker上运行Strapi [英] Cannot run Strapi with Docker

查看:79
本文介绍了无法在Docker上运行Strapi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对我的 Strapi 应用程序进行Docker化,因此首先我在项目的根目录中创建了一个 .env 文件,该文件包含以下内容:

I'm trying to Dockerize my Strapi application, so first all in the root directory of the project I have created an .env file which contains the following:

HOST=0.0.0.0
PORT=3002

然后,在 backend/config/server.js 内部,我有:

module.exports = ({ env }) => ({
  host: env("HOST", "0.0.0.0"),
  port: env.int("PORT"),
  admin: {
    auth: {
      secret: env("ADMIN_JWT_SECRET", "3b8efb990e54568fc0d91ff31390cda7"),
    },
  },
});

该代码应将应用程序绑定到 0.0.0.0 .实际上,当我运行容器时,我可以看到它已绑定到 0.0.0.0 .

this code is supposed to bind the application to 0.0.0.0. Infact, when I run the container I can see that is binded to 0.0.0.0.

此后,我创建了一个 Dockerfile ,其中包含以下说明:

After this, I have created a Dockerfile which contains the following instructions:

FROM node:12

EXPOSE 3002

WORKDIR /backend

COPY ./package.json .
RUN npm install
COPY . .

RUN ls -l

CMD ["npm", "run", "develop"]

然后我有一个 docker-compose.yml 具有:

version: '3'
services:
  backend:
    container_name: foo_backend
    build: ./backend/
    ports:
      - '3002:3002'
    volumes:
      - ./backend:/usr/src/foo/backend
      - /usr/src/foo/backend/node_modules
    environment:
      - APP_NAME=foo_backend
      - DATABASE_CLIENT=mysql
      - DATABASE_HOST=foo_mysql
      - DATABASE_PORT=3306
      - DATABASE_NAME=foo_db
      - DATABASE_USERNAME=foo
      - DATABASE_PASSWORD=foofoo
      - DATABASE_SSL=false
      - DATABASE_AUTHENTICATION_DATABASE=foo_db
      - HOST=localhost
    depends_on:
      - db
    restart: always

(我没有在此处添加 db 服务).

(I didn't added here the db service).

当我使用 docker-compose up --build 运行容器时,一切在日志中运行良好:

When I run the container using docker-compose up --build everything works well in the log:

但是当我访问 http://localhost:3002 时,我得到了:

but when I visit http://localhost:3002 I get:

ERR_EMPTY_RESPONSE

ERR_EMPTY_RESPONSE

这仅在Windows上发生.有什么主意吗?

This only happen on windows. Any idea?

NGINX配置

server {
        server_name mysite.backend.domain.com www.mysite.backend.domain.com;

        location / {
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header Host $http_host;
                proxy_pass http://localhost:3002/;
        }

}

这是我得到的错误:

[错误] 6783#6783:* 82 recv()失败(104:对等连接重置),同时从上游读取响应标头,客户端:93.148.94.171,服务器:mysite.backend.domain.com请求:GET/favicon.ico HTTP/1.1",上游:"http://127.0.0.1:3002/favicon.ico",主机:"mysite.backend.domain.com",引荐来源网址:"https://mysite.backend.domain.com/"

[error] 6783#6783: *82 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 93.148.94.171, server: mysite.backend.domain.com request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:3002/favicon.ico", host: "mysite.backend.domain.com", referrer: "https://mysite.backend.domain.com/"

推荐答案

您在这里犯了一个小错误,在后端服务的环境变量中,将- HOST = localhost 替换为- HOST =0.0.0.0 会起作用的.

You are doing a little mistake here, in environment variables of backend service replace - HOST=localhost to - HOST=0.0.0.0 It will work.

这篇关于无法在Docker上运行Strapi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆