Cannot run Strapi with Docker

混江龙づ霸主 提交于 2021-02-07 19:51:21

问题


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

then, inside backend/config/server.js I have:

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

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.

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"]

then I have a docker-compose.yml that has:

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

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

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

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

ERR_EMPTY_RESPONSE

This only happen on windows. Any idea?

NGINX CONFIGURATION

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/;
        }

}

this is the error that I get:

[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/"


回答1:


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.



来源:https://stackoverflow.com/questions/65645205/cannot-run-strapi-with-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!