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

View File

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

View File

@ -2,9 +2,6 @@
# Use alpine for reduced image size # Use alpine for reduced image size
FROM node:19.6-alpine AS base FROM node:19.6-alpine AS base
# Set NODE_ENV
ENV NODE_ENV production
# Specify working directory other than / # Specify working directory other than /
WORKDIR /usr/src/app WORKDIR /usr/src/app
@ -12,21 +9,20 @@ WORKDIR /usr/src/app
# dependencies (better layer caching) # dependencies (better layer caching)
COPY package*.json ./ 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 \ RUN --mount=type=cache,target=/usr/src/app/.npm \
npm set cache /usr/src/app/.npm && \ npm set cache /usr/src/app/.npm && \
npm install npm install
COPY . . 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 # Install only production dependencies
# Use cache mount to speed up install of existing dependencies # Use cache mount to speed up install of existing dependencies

View File

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

View File

@ -1,12 +1,11 @@
version: "3.9"
services: services:
client-react-vite: client-react-vite:
image: client-react-vite
build: build:
context: ../05-example-web-application/client-react/ context: ../05-example-web-application/client-react/
dockerfile: ../../06-building-container-images/client-react/Dockerfile.3 dockerfile: ../../06-building-container-images/client-react/Dockerfile.3
networks: init: true
- frontend
ports:
- 5173:5173
volumes: volumes:
- type: bind - type: bind
source: ../05-example-web-application/client-react/ source: ../05-example-web-application/client-react/
@ -16,19 +15,34 @@ services:
- type: bind - type: bind
source: ../08-running-containers/client-react/vite.config.js source: ../08-running-containers/client-react/vite.config.js
target: /usr/src/app/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 restart: unless-stopped
api-node: api-node:
image: api-node
build: build:
context: ../05-example-web-application/api-node/ context: ../05-example-web-application/api-node/
dockerfile: ../../06-building-container-images/api-node/Dockerfile.9 dockerfile: ../../06-building-container-images/api-node/Dockerfile.9
target: dev target: dev
init: true
volumes: volumes:
- type: bind - type: bind
source: ../05-example-web-application/api-node/ source: ../05-example-web-application/api-node/
target: /usr/src/app/ target: /usr/src/app/
- type: volume - type: volume
target: /usr/src/app/node_modules target: /usr/src/app/node_modules
init: true
depends_on: depends_on:
- db - db
environment: environment:
@ -37,18 +51,19 @@ services:
- frontend - frontend
- backend - backend
ports: ports:
- "3000:3000" - 3000:3000
restart: unless-stopped restart: unless-stopped
api-golang: api-golang:
image: api-golang
build: build:
context: ../05-example-web-application/api-golang/ context: ../05-example-web-application/api-golang/
dockerfile: ../../06-building-container-images/api-golang/Dockerfile.8 dockerfile: ../../06-building-container-images/api-golang/Dockerfile.8
target: dev target: dev
init: true
volumes: volumes:
- type: bind - type: bind
source: ../05-example-web-application/api-golang/ source: ../05-example-web-application/api-golang/
target: /app/ target: /app/
init: true
depends_on: depends_on:
- db - db
environment: environment:
@ -57,7 +72,7 @@ services:
- frontend - frontend
- backend - backend
ports: ports:
- "8080:8080" - 8080:8080
restart: unless-stopped restart: unless-stopped
db: db:
image: postgres:15.1-alpine image: postgres:15.1-alpine

View File

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