update dev dockerfiles

This commit is contained in:
sid palas
2023-02-21 20:35:58 -05:00
parent d2a8e3a599
commit e796267218
6 changed files with 59 additions and 36 deletions

View File

@ -1,10 +1,7 @@
# Pin specific version for stability
# Use separate stage for building image
# Use debian for easier build utilities
FROM golang:1.19-bullseye AS base-builder
# Add non root user
RUN useradd -u 1001 nonroot
FROM golang:1.19-bullseye AS build-base
WORKDIR /app
@ -16,8 +13,7 @@ RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go mod download
# Dev stage with additional dev dependencies installed
FROM base-builder AS dev
FROM build-base AS dev
# Install air for hot reload & delve for debugging
RUN go install github.com/cosmtrek/air@latest && \
@ -27,8 +23,10 @@ COPY . .
CMD ["air", "-c", ".air.toml"]
# Production builder stage to produce the static binaries
FROM base-builder AS production-builder
FROM build-base AS build-production
# Add non root user
RUN useradd -u 1001 nonroot
COPY . .
@ -47,7 +45,7 @@ RUN go build \
-o api-golang
# Use separate stage for deployable image
FROM scratch AS production
FROM scratch
# Set gin mode
ENV GIN_MODE=release
@ -55,13 +53,13 @@ ENV GIN_MODE=release
WORKDIR /
# Copy the passwd file
COPY --from=production-builder /etc/passwd /etc/passwd
COPY --from=build-production /etc/passwd /etc/passwd
# Copy the healthcheck binary from the build stage
COPY --from=production-builder /app/healthcheck/healthcheck healthcheck
COPY --from=build-production /app/healthcheck/healthcheck healthcheck
# Copy the app binary from the build stage
COPY --from=production-builder /app/api-golang api-golang
COPY --from=build-production /app/api-golang api-golang
# Use nonroot user
USER nonroot

View File

@ -1,9 +1,6 @@
# Pin specific version for stability
# Use alpine for reduced image size
FROM node:19.6-alpine
# Set NODE_ENV
ENV NODE_ENV production
FROM node:19.6-alpine AS base
# Specify working directory other than /
WORKDIR /usr/src/app
@ -12,6 +9,21 @@ WORKDIR /usr/src/app
# dependencies (better layer caching)
COPY package*.json ./
FROM base as dev
RUN --mount=type=cache,target=/usr/src/app/.npm \
npm set cache /usr/src/app/.npm && \
npm install
COPY . .
CMD ["npm", "run", "dev"]
FROM base as production
# Set NODE_ENV
ENV NODE_ENV production
# Install only production dependencies
# Use cache mount to speed up install of existing dependencies
RUN --mount=type=cache,target=/usr/src/app/.npm \

View File

@ -2,9 +2,6 @@
# Use alpine for reduced image size
FROM node:19.6-alpine AS base
# Set NODE_ENV
ENV NODE_ENV production
# Specify working directory other than /
WORKDIR /usr/src/app
@ -12,21 +9,20 @@ WORKDIR /usr/src/app
# dependencies (better layer caching)
COPY package*.json ./
# Development stage to include dev + production dependencies
FROM base AS dev
FROM base as dev
# Install all dependencies (including development ones)
# For the dev image
RUN --mount=type=cache,target=/usr/src/app/.npm \
npm set cache /usr/src/app/.npm && \
npm install
COPY . .
CMD [ "npm", "run", "dev" ]
CMD ["npm", "run", "dev"]
# Production stage optimzed for deployment
FROM base AS production
FROM base as production
# Set NODE_ENV
ENV NODE_ENV production
# Install only production dependencies
# Use cache mount to speed up install of existing dependencies

View File

@ -1,4 +1,5 @@
# Overlay configuration to enable debuggers
version: "3.9"
services:
api-node:
command:

View File

@ -1,12 +1,11 @@
version: "3.9"
services:
client-react-vite:
image: client-react-vite
build:
context: ../05-example-web-application/client-react/
dockerfile: ../../06-building-container-images/client-react/Dockerfile.3
networks:
- frontend
ports:
- 5173:5173
init: true
volumes:
- type: bind
source: ../05-example-web-application/client-react/
@ -16,39 +15,55 @@ services:
- type: bind
source: ../08-running-containers/client-react/vite.config.js
target: /usr/src/app/vite.config.js
networks:
- frontend
ports:
- 5173:5173
client-react-nginx:
image: client-react-nginx
build:
context: ../05-example-web-application/client-react/
dockerfile: ../../06-building-container-images/client-react/Dockerfile.5
init: true
networks:
- frontend
ports:
- 80:8080
restart: unless-stopped
api-node:
image: api-node
build:
context: ../05-example-web-application/api-node/
dockerfile: ../../06-building-container-images/api-node/Dockerfile.9
target: dev
init: true
volumes:
- type: bind
source: ../05-example-web-application/api-node/
target: /usr/src/app/
- type: volume
target: /usr/src/app/node_modules
init: true
depends_on:
- db
environment:
- DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres
networks:
- frontend
- backend
- backend
ports:
- "3000:3000"
- 3000:3000
restart: unless-stopped
api-golang:
image: api-golang
build:
context: ../05-example-web-application/api-golang/
dockerfile: ../../06-building-container-images/api-golang/Dockerfile.8
target: dev
init: true
volumes:
- type: bind
source: ../05-example-web-application/api-golang/
target: /app/
init: true
depends_on:
- db
environment:
@ -57,7 +72,7 @@ services:
- frontend
- backend
ports:
- "8080:8080"
- 8080:8080
restart: unless-stopped
db:
image: postgres:15.1-alpine

View File

@ -1,4 +1,5 @@
# Overlay configuration to run tests
version: "3.9"
services:
api-node:
command:
@ -10,4 +11,4 @@ services:
- "go"
- "test"
- "-v"
- "./..."
- "./..."