skip to Main Content

https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/installation/docker.html#creating-a-strapi-project
dockerize strapi with docker and dockercompose
Resolve different error

strapi failed to load resource: the server responded with a status of 404 ()

2

Answers


  1. Chosen as BEST ANSWER

    Dockerize Strapi with Docker-compose

    FROM node:16.14.2
    
    # Set up the working directory that will be used to copy files/directories below :
    WORKDIR /app
    
    # Copy package.json to root directory inside Docker container of Strapi app
    COPY package.json .
    
    RUN npm install
    
    COPY . .
    RUN npm run build
    
    EXPOSE 1337
    
    CMD ["npm", "start"]
    

    #docker-compose file

    version: '3.7'
    services:
      strapi:
    container_name: strapi
    restart: unless-stopped
    build:
      context: ./strapi
      dockerfile: Dockerfile
    volumes:
      - strapi:/app
      - /app/node_modules
    ports:
      - '1337:1337'
    
    volumes:
      strapi:
        driver: local
    

  2. you can use my dockerized project.

    Dockerfile:

    FROM node:16.15-alpine3.14
    RUN mkdir -p /opt/app
    WORKDIR /opt/app
    RUN adduser -S app
    COPY app/ .
    RUN npm install
    RUN npm install --save @strapi/strapi
    RUN chown -R app /opt/app
    USER app
    RUN npm run build
    EXPOSE 1337
    CMD [ "npm", "run", "start" ]
    

    if you don’t use RUN npm run build your project on port 80 or http://localhost work but strapi admin templates call http://localhost:1337 on your system that you are running on http://localhost and there is no http://localhost:1337 stabile url and strapi throw exceptions like:

    Refused to connect to 'http://localhost:1337/admin/init' because it violates the document's Content Security Policy.
    
    Refused to connect to 'http://localhost:1337/admin/init' because it violates the following Content Security Policy directive: "connect-src 'self' https:".
    

    docker-compose.yml:

    version: "3.9"
    services:
      #Strapi Service (APP Service)
      strapi_app:
        build:
          context: .
        depends_on:
          - strapi_db
        ports:
          - "80:1337"
        environment:
          - DATABASE_CLIENT=postgres
          - DATABASE_HOST=strapi_db
          - DATABASE_PORT=5432
          - DATABASE_NAME=strapi_db
          - DATABASE_USERNAME=strapi_db
          - DATABASE_PASSWORD=strapi_db
          - DATABASE_SSL=false
        volumes:
        - /var/scrapi/public/uploads:/opt/app/public/uploads
        - /var/scrapi/public:/opt/app/public
        networks:
          - app-network
    
      #PostgreSQL Service
      strapi_db:
        image: postgres
        container_name: strapi_db
        environment:
          POSTGRES_USER: strapi_db
          POSTGRES_PASSWORD: strapi_db
          POSTGRES_DB: strapi_db
        ports:
          - '5432:5432'
        volumes:
          - dbdata:/var/lib/postgresql/data
        networks:
          - app-network
    
    #Docker Networks
    networks:
      app-network:
        driver: bridge
    #Volumes
    volumes:
      dbdata:
        driver: local
    

    in docker compose file I used postgres as database, you can use any other databases and set its config in app service environment variables like:

    environment:
          - DATABASE_CLIENT=postgres
          - DATABASE_HOST=strapi_db
          - DATABASE_PORT=5432
          - DATABASE_NAME=strapi_db
          - DATABASE_USERNAME=strapi_db
          - DATABASE_PASSWORD=strapi_db
          - DATABASE_SSL=false
    

    for using environment variables in project you must use process.env for getting operating system environment variables.

    change app/config/database.js file to:

    module.exports = ({ env }) => ({
      connection: {
        client: process.env.DATABASE_CLIENT,
        connection: {
          host: process.env.DATABASE_HOST,
          port: parseInt(process.env.DATABASE_PORT),
          database: process.env.DATABASE_NAME,
          user: process.env.DATABASE_USERNAME,
          password: process.env.DATABASE_PASSWORD,
          // ssl: Boolean(process.env.DATABASE_SSL),
          ssl: false,
        },
      },
    });
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search