Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

Cannot run Strapi with Docker

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:

enter image description here

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

question from:https://stackoverflow.com/questions/65645205/cannot-run-strapi-with-docker

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...