Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run by separating the environments #132

Open
hahamini opened this issue Jan 2, 2025 · 2 comments
Open

How to run by separating the environments #132

hahamini opened this issue Jan 2, 2025 · 2 comments
Assignees

Comments

@hahamini
Copy link

hahamini commented Jan 2, 2025

I want to run both dev mode and prod mode.
Since I can select both in the env option when setting up the project, it seems that dockerize supports this.

However, docker compose yaml does not distinguish the env mode separately.
How can I run the server in prod mode? Can I just change build: . in the compose file to build: prod.Dockerfile ?

I would really appreciate it if you could guide me on this.

@Eventyret
Copy link
Member

You will need to create two seperate services in Docker compose and set the env to prod or dev hardcoded

@hahamini
Copy link
Author

hahamini commented Jan 3, 2025

@Eventyret So I split .env and hardcoded it.
As in the manual, I put .env as a common file and wrote secret information in .env.dev.

However, when docker compose build is running, a warning appears that variable is unreachable.

So I checked in various ways, but there was nothing visible, and when I did docker compose --env-file .env.dev, I could not see this warning.

.env

HOST=0.0.0.0
PORT=1337

.env.dev

PORT=13371
ADMINER_PORT=13372

# Secrets
APP_KEYS=***,***,***,***
API_TOKEN_SALT=***
ADMIN_JWT_SECRET=***
TRANSFER_TOKEN_SALT=***

# Database
DATABASE_CLIENT=postgres
DATABASE_HOST=127.0.0.1
DATABASE_PORT=5432
DATABASE_NAME=strapi
DATABASE_USERNAME=strapi
DATABASE_PASSWORD=***
DATABASE_SSL=false
DATABASE_FILENAME=

# @strapi-community/dockerize variables 

NODE_ENV=development

JWT_SECRET=***

docker-compose-dev.yaml

services:
  web-api-dev:
    container_name: web-api-dev
    restart: unless-stopped
    build:
      context: .
      dockerfile: dev.Dockerfile
    env_file:
      - path: .env.dev
    environment:
      DATABASE_CLIENT: ${DATABASE_CLIENT}
      DATABASE_HOST: web-api-dev-DB
      DATABASE_NAME: ${DATABASE_NAME}
      DATABASE_USERNAME: ${DATABASE_USERNAME}
      DATABASE_PORT: ${DATABASE_PORT}
      JWT_SECRET: ${JWT_SECRET}
      ADMIN_JWT_SECRET: ${ADMIN_JWT_SECRET}
      DATABASE_PASSWORD: ${DATABASE_PASSWORD}
      NODE_ENV: ${NODE_ENV}
    volumes:
      - ./config:/opt/app/config
      - ./src:/opt/app/src
      - ./package.json:/opt/package.json
      - ./yarn.lock:/opt/yarn.lock

      - ./.env:/opt/app/.env
      - ./.env.dev:/opt/app/.env.dev
      - ./public/uploads:/opt/app/public/uploads
    ports:
      - '1337:${PORT}'
    networks:
      - web-api-dev
    depends_on:
      - web-api-dev-DB
      
  web-api-dev-DB:
    container_name: web-api-dev-DB
    platform: linux/amd64 #for platform error on Apple M1 chips
    restart: unless-stopped
    image: postgres:14.5-alpine
    env_file:
      - .env.dev
    environment:
      POSTGRES_USER: ${DATABASE_USERNAME}
      POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
      POSTGRES_DB: ${DATABASE_NAME}
    volumes:
      - data:/var/lib/postgresql/data/ #using a volume
      #- ./data:/var/lib/postgresql/data/ # if you want to use a bind folder

    # ports:
    #   - '5432:5432'
    networks:
      - web-api-dev
      
  web-api-dev-Adminer:
    container_name: web-api-dev-Adminer
    image: adminer
    restart: unless-stopped
    env_file:
      - .env.dev
    ports:
      - '${ADMINER_PORT}:${ADMINER_PORT}'
    environment:
      - ADMINER_DEFAULT_SERVER=web-api-dev-DB
    networks:
      - web-api-dev
    depends_on:
      - web-api-dev-DB

volumes:
  data:

networks:
  web-api-dev:
    name: web-api-dev
    driver: bridge

dev.Dockerfile

FROM node:18-alpine
# Installing libvips-dev for sharp Compatibility
RUN apk update && apk add --no-cache build-base gcc autoconf automake zlib-dev libpng-dev nasm bash vips-dev
RUN apk add --no-cache chromium
ARG NODE_ENV=development
ENV NODE_ENV=${NODE_ENV}

RUN apk add tzdata && ln -snf /usr/share/zoneinfo/Asia/Seoul /etc/localtime

WORKDIR /opt/
COPY package.json yarn.lock ./
RUN yarn global add node-gyp
RUN yarn config set network-timeout 600000 -g && yarn install

WORKDIR /opt/app
COPY . .
ENV PATH /opt/node_modules/.bin:$PATH
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
ENV PUPPETEER_EXECUTABLE_PATH /usr/bin/chromium
RUN chown -R node:node /opt/app
USER node
RUN ["yarn", "build"]
EXPOSE ${PORT}
CMD ["yarn", "develop"]

warning message when building
docker compose -f docker-compose-dev.yaml

WARN[0000] The "DATABASE_PASSWORD" variable is not set. Defaulting to a blank string. 
WARN[0000] The "DATABASE_NAME" variable is not set. Defaulting to a blank string. 
WARN[0000] The "ADMIN_JWT_SECRET" variable is not set. Defaulting to a blank string. 
WARN[0000] The "JWT_SECRET" variable is not set. Defaulting to a blank string. 
WARN[0000] The "NODE_ENV" variable is not set. Defaulting to a blank string. 
WARN[0000] The "DATABASE_CLIENT" variable is not set. Defaulting to a blank string. 
WARN[0000] The "DATABASE_USERNAME" variable is not set. Defaulting to a blank string. 
WARN[0000] The "DATABASE_PORT" variable is not set. Defaulting to a blank string. 
WARN[0000] The "DATABASE_USERNAME" variable is not set. Defaulting to a blank string. 
WARN[0000] The "DATABASE_PASSWORD" variable is not set. Defaulting to a blank string. 
WARN[0000] The "DATABASE_NAME" variable is not set. Defaulting to a blank string. 
WARN[0000] The "ADMINER_PORT" variable is not set. Defaulting to a blank string. 
WARN[0000] The "ADMINER_PORT" variable is not set. Defaulting to a blank string. 
no port specified: :<empty>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants