Skip to content

Commit

Permalink
Build separate images for dev and prod
Browse files Browse the repository at this point in the history
  • Loading branch information
jsphpl committed May 11, 2024
1 parent a6b15d1 commit e53235d
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 281 deletions.
2 changes: 0 additions & 2 deletions .dockerignore

This file was deleted.

42 changes: 33 additions & 9 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,48 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
- name: Extract metadata for dev image
id: meta-dev
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/dev
tags: |
type=semver,pattern=v{{ major }}
type=semver,pattern=v{{ major }}.{{ minor }}
type=semver,pattern=v{{ version }}
type=semver,pattern={{ major }}
type=semver,pattern={{ major }}.{{ minor }}
type=semver,pattern={{ version }}
type=raw,value=latest,enable={{ is_default_branch }}
- name: Build and push Docker image
- name: Build dev image
uses: docker/build-push-action@v5
with:
context: .
push: true
target: dev
platforms: linux/arm64, linux/amd64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
tags: ${{ steps.meta-dev.outputs.tags }}
labels: ${{ steps.meta-dev.outputs.labels }}
cache-from: type=gha
cache-to: type=gha

- name: Extract metadata for prod image
id: meta-prod
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/prod
tags: |
type=semver,pattern={{ major }}
type=semver,pattern={{ major }}.{{ minor }}
type=semver,pattern={{ version }}
type=raw,value=latest,enable={{ is_default_branch }}
- name: Build prod image
uses: docker/build-push-action@v5
with:
context: .
push: true
target: prod
platforms: linux/arm64, linux/amd64
tags: ${{ steps.meta-prod.outputs.tags }}
labels: ${{ steps.meta-prod.outputs.labels }}
cache-from: type=gha
cache-to: type=gha
32 changes: 20 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
# -|----------------------------------------------------------------------------
# | Intermediate image for building the application.
# | Development image
# -|----------------------------------------------------------------------------
FROM node:20-alpine AS builder
FROM node:20-alpine AS dev
WORKDIR /app
ENV NODE_ENV=development

COPY package.json package-lock.json ./
RUN npm ci

COPY tsconfig.json ./
COPY src ./src
RUN npm run build
COPY public ./public

ENV APP_KEYS="development - don't care"
ENV JWT_SECRET="development - don't care"
ENV ADMIN_JWT_SECRET="development - don't care"
ENV API_TOKEN_SALT="development - don't care"

ENTRYPOINT [ "npm", "run", "develop" ]

# -|----------------------------------------------------------------------------
# | Final image containing only the files required to run in production.
# | Production base image
# -|----------------------------------------------------------------------------
FROM node:20-alpine AS production
FROM node:20-alpine AS prod
WORKDIR /app
ENV NODE_ENV=production

RUN apk add --no-cache vips-dev

WORKDIR /app
RUN chown node:node /app
USER node
ENV NODE_ENV=production

COPY --from=builder --chown=node:node /app/package.json .
COPY --from=builder --chown=node:node /app/dist dist
COPY --from=builder --chown=node:node /app/node_modules node_modules
COPY --chown=node:node public ./public
COPY --from=dev --chown=node:node /app .
COPY --chown=node:node server.js ./

EXPOSE 1337
ENTRYPOINT [ "npm", "run", "start" ]
ENTRYPOINT [ "node", "server.js" ]
80 changes: 37 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,55 @@
# SehrGuteSoftware - Strapi Dockerized

### Configuration

The configuration is done via environment variables. The following variables are available:

```bash
# Server
HOST=0.0.0.0
PORT=1337
APP_KEYS="toBeModified1,toBeModified2"
API_TOKEN_SALT=tobemodified
ADMIN_JWT_SECRET=tobemodified
TRANSFER_TOKEN_SALT=tobemodified
JWT_SECRET=tobemodified

# Database
DATABASE_CLIENT=postgres
DATABASE_HOST=127.0.0.1
DATABASE_PORT=5432
DATABASE_NAME=strapi
DATABASE_USERNAME=strapi
DATABASE_PASSWORD=strapi
DATABASE_SSL=false
```
# Strapi Dockerized

This repository provides development and production images for Strapi.

## Development

### Use in Docker Compose
The development image can be used as-is, for example in a `compose.yaml`. The `src`, `config` and `public` directories from the source folder of your Strapi project must be mounted into the container.

```yaml
version: "3"
services:
postgres:
image: postgres:16-alpine
env_file: .env
volumes:
- ./postgres/data:/var/lib/postgresql/data
- postgres-data:/var/lib/postgresql/data
ports:
- "5432:5432"
environment:
POSTGRES_DB: ${DATABASE_NAME}
POSTGRES_USER: ${DATABASE_USERNAME}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_DB: strapi
POSTGRES_USER: strapi
POSTGRES_PASSWORD: strapi
PGDATA: /var/lib/postgresql/data/pgdata
strapi:
image: strapi-test:latest
env_file: .env
# Uncomment the following line to use the development environment
# environment:
# NODE_ENV: development
# command: ["npm", "run", "develop"]
volumes:
# - ./strapi/config:/opt/app/config # Before you use this, you need to copy the default `config` folder from github.com/sehrgutesoftware/strapi-dockerized
- ./strapi/api:/opt/app/src/api
- ./strapi/components:/opt/app/src/components
- ./.env:/opt/app/.env
image: ghcr.io/sehrgutesoftware/strapi-dockerized/dev:latest
ports:
- "1337:1337"
environment:
DATABASE_CLIENT: postgres
DATABASE_HOST: postgres
DATABASE_PORT: 5432
DATABASE_NAME: strapi
DATABASE_USERNAME: strapi
DATABASE_PASSWORD: strapi
# See https://docs.strapi.io/dev-docs/configurations/environment for all environment variables
volumes:
- ${PWD}/src:/app/src
- ${PWD}/config:/app/config
- ${PWD}/public:/app/public
depends_on:
- postgres
volumes:
postgres-data:
```
## Production
The production image serves as a base image, which can be used in a Dockerfile to build an application-specific production docker image:
```Dockerfile
FROM ghcr.io/sehrgutesoftware/strapi-dockerized/prod:4.22.0
COPY --chown=node:node . .
RUN npm run build
```

Note that by default the `NODE_ENV` is set to `production`. If you want to use the development environment, you can uncomment the `environment` and `command` lines.
Configuration values must be injected at run time via environment variables or a .env file.
17 changes: 0 additions & 17 deletions config/admin.ts

This file was deleted.

7 changes: 0 additions & 7 deletions config/api.ts

This file was deleted.

93 changes: 0 additions & 93 deletions config/database.ts

This file was deleted.

12 changes: 0 additions & 12 deletions config/middlewares.ts

This file was deleted.

1 change: 0 additions & 1 deletion config/plugins.ts

This file was deleted.

10 changes: 0 additions & 10 deletions config/server.ts

This file was deleted.

3 changes: 3 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const strapi = require("@strapi/strapi");
const app = strapi({ distDir: "./dist" });
app.start();
35 changes: 0 additions & 35 deletions src/admin/app.example.tsx

This file was deleted.

Loading

0 comments on commit e53235d

Please sign in to comment.