restructure repo, separate sample app from docker configs

This commit is contained in:
sid palas
2023-01-27 09:26:02 -05:00
parent dd5d7bff0b
commit 08510ae883
58 changed files with 645 additions and 12 deletions

View File

@ -0,0 +1,15 @@
.PHONY: compose-build
compose-build:
docker compose build
.PHONY: compose-up
compose-up:
docker compose up
.PHONY: compose-up-build
compose-up-build:
docker compose up --build
.PHONY: compose-down
compose-down:
docker compose down

View File

@ -0,0 +1,9 @@
FROM golang
WORKDIR /app
COPY . .
RUN go mod download
CMD ["go", "run", "./main.go"]

View File

@ -0,0 +1,11 @@
# Pin specific version for stability
# Use alpine for reduced image size
FROM golang:1.19-alpine
WORKDIR /app
COPY . .
RUN go mod download
CMD ["go", "run", "./main.go"]

View File

@ -0,0 +1,14 @@
# Pin specific version for stability
# Use alpine for reduced image size
FROM golang:1.19-alpine
WORKDIR /app
COPY . .
RUN go mod download
# Compile application during build rather than at runtime
RUN go build -o api-golang
CMD ["./api-golang"]

View File

@ -0,0 +1,17 @@
# Pin specific version for stability
# Use alpine for reduced image size
FROM golang:1.19-alpine
WORKDIR /app
# Copy only files required to install dependencies (better layer caching)
COPY go.mod go.sum ./
RUN go mod download
COPY . .
# Compile application during build rather than at runtime
RUN go build -o api-golang
CMD ["./api-golang"]

View File

@ -0,0 +1,29 @@
# Pin specific version for stability
# Use separate stage for building image
# Use debian for easier build utilities
FROM golang:1.19-bullseye AS build
WORKDIR /app
# Copy only files required to install dependencies (better layer caching)
COPY go.mod go.sum ./
RUN go mod download
COPY . .
# Compile application during build rather than at runtime
# Add -w and -s flags to
RUN go build \
-ldflags="-linkmode external -extldflags -static" \
-o api-golang
# Use separate stage for deployable image
FROM scratch
WORKDIR /
# Copy the binary from the build stage
COPY --from=build /app/api-golang api-golang
CMD ["/api-golang"]

View File

@ -0,0 +1,35 @@
# Pin specific version for stability
# Use separate stage for building image
# Use debian for easier build utilities
FROM golang:1.19-bullseye AS build
WORKDIR /app
# Copy only files required to install dependencies (better layer caching)
COPY go.mod go.sum ./
RUN go mod download
COPY . .
# Compile application during build rather than at runtime
# Add flags to statically link binary
RUN go build \
-ldflags="-linkmode external -extldflags -static" \
-o api-golang
# Use separate stage for deployable image
FROM scratch
# Set gin mode
ENV GIN_MODE=release
WORKDIR /
# Copy the binary from the build stage
COPY --from=build /app/api-golang api-golang
# Indicate expected port
EXPOSE 8080
CMD ["/api-golang"]

View File

@ -0,0 +1,47 @@
# Pin specific version for stability
# Use separate stage for building image
# Use debian for easier build utilities
FROM golang:1.19-bullseye AS build
# Add non root user
RUN useradd -u 1001 nonroot
WORKDIR /app
# Copy only files required to install dependencies (better layer caching)
COPY go.mod go.sum ./
# Use cache mount to speed up install of existing dependencies
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go mod download
COPY . .
# Compile application during build rather than at runtime
# Add flags to statically link binary
RUN go build \
-ldflags="-linkmode external -extldflags -static" \
-o api-golang
# Use separate stage for deployable image
FROM scratch
# Set gin mode
ENV GIN_MODE=release
WORKDIR /
# Copy the passwd file
COPY --from=build /etc/passwd /etc/passwd
# Copy the binary from the build stage
COPY --from=build /app/api-golang api-golang
# Use nonroot user
USER nonroot
# Indicate expected port
EXPOSE 8080
CMD ["/api-golang"]

View File

@ -0,0 +1,12 @@
API_GOLANG_PATH:=../../05-example-web-application/api-golang/
N?=0
.PHONY: build-N
build-N:
docker build --file ./Dockerfile.$(N) -t api-golang:$(N) ${API_GOLANG_PATH}
.PHONY: build-all
build-all:
for number in 0 1 2 3 4 5 6 ; do \
N=$$number $(MAKE) build-N; \
done

View File

@ -0,0 +1,7 @@
FROM node
COPY . .
RUN npm install
CMD [ "node", "index.js" ]

View File

@ -0,0 +1,9 @@
# Pin specific version
# Use alpine for reduced image size
FROM node:19.4-alpine
COPY . .
RUN npm install
CMD [ "node", "index.js" ]

View File

@ -0,0 +1,12 @@
# Pin specific version
# Use alpine for reduced image size
FROM node:19.4-alpine
# Specify working directory other than /
WORKDIR /usr/src/app
COPY . .
RUN npm install
CMD [ "node", "index.js" ]

View File

@ -0,0 +1,18 @@
# Pin specific version for stability
# Use alpine for reduced image size
FROM node:19.4-alpine
# Specify working directory other than /
WORKDIR /usr/src/app
# Copy only files required to install
# dependencies (better layer caching)
COPY package*.json ./
RUN npm install
# Copy remaining source code AFTER installing dependencies.
# Again, copy only the necessary files
COPY ./src/ .
CMD [ "node", "index.js" ]

View File

@ -0,0 +1,22 @@
# Pin specific version for stability
# Use alpine for reduced image size
FROM node:19.4-alpine
# Specify working directory other than /
WORKDIR /usr/src/app
# Copy only files required to install
# dependencies (better layer caching)
COPY package*.json ./
RUN npm install
# Use non-root user
# Use --chown on COPY commands to set file permissions
USER node
# Copy remaining source code AFTER installing dependencies.
# Again, copy only the necessary files
COPY --chown=node:node ./src/ .
CMD [ "node", "index.js" ]

View File

@ -0,0 +1,26 @@
# Pin specific version for stability
# Use alpine for reduced image size
FROM node:19.4-alpine
# Set NODE_ENV
ENV NODE_ENV production
# Specify working directory other than /
WORKDIR /usr/src/app
# Copy only files required to install
# dependencies (better layer caching)
COPY package*.json ./
# Install only production dependencies
RUN npm ci --only=production
# Use non-root user
# Use --chown on COPY commands to set file permissions
USER node
# Copy remaining source code AFTER installing dependencies.
# Again, copy only the necessary files
COPY --chown=node:node ./src/ .
CMD [ "node", "index.js" ]

View File

@ -0,0 +1,29 @@
# Pin specific version for stability
# Use alpine for reduced image size
FROM node:19.4-alpine
# Set NODE_ENV
ENV NODE_ENV production
# Specify working directory other than /
WORKDIR /usr/src/app
# Copy only files required to install
# dependencies (better layer caching)
COPY package*.json ./
# Install only production dependencies
RUN npm ci --only=production
# Use non-root user
# Use --chown on COPY commands to set file permissions
USER node
# Copy remaining source code AFTER installing dependencies.
# Again, copy only the necessary files
COPY --chown=node:node ./src/ .
# Indicate expected port
EXPOSE 3000
CMD [ "node", "index.js" ]

View File

@ -0,0 +1,36 @@
# Pin specific version for stability
# Use alpine for reduced image size
FROM node:19.4-alpine
# Set NODE_ENV
ENV NODE_ENV production
# Specify working directory other than /
WORKDIR /usr/src/app
# Copy only files required to install
# dependencies (better layer caching)
COPY package*.json ./
# Install only production dependencies
# Use cache mount to speed up install of existing dependencies
RUN --mount=type=cache,target=/usr/src/app/.npm \
npm set cache /usr/src/app/.npm && \
npm ci --only=production
# Use non-root user
# Use --chown on COPY commands to set file permissions
USER node
# Copy remaining source code AFTER installing dependencies.
# Again, copy only the necessary files
COPY --chown=node:node ./src/ .
# Indicate expected port
EXPOSE 3000
CMD [ "node", "index.js" ]
# TODO: Use multi-stage with distroless image or chainguard image?
# https://github.com/GoogleContainerTools/distroless/blob/main/examples/nodejs/Dockerfile
# https://edu.chainguard.dev/chainguard/chainguard-images/reference/node/overview/

View File

@ -0,0 +1,12 @@
API_NODE_PATH:=../../05-example-web-application/api-node/
N?=0
.PHONY: build-N
build-N:
docker build --file ./Dockerfile.$(N) -t api-node:$(N) ${API_NODE_PATH}
.PHONY: build-all
build-all:
for number in 0 1 2 3 4 5 6 7 ; do \
N=$$number $(MAKE) build-N; \
done

View File

@ -0,0 +1,14 @@
# DEV Dockerfile
FROM node:19.4-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 5173
CMD ["npm", "run", "dev"]

View File

@ -0,0 +1,7 @@
FROM node
COPY . .
RUN npm install
CMD ["npm", "run", "dev"]

View File

@ -0,0 +1,7 @@
FROM node:19.4-bullseye
COPY . .
RUN npm install
CMD ["npm", "run", "dev"]

View File

@ -0,0 +1,14 @@
FROM node:19.4-bullseye AS build
# Specify working directory other than /
WORKDIR /usr/src/app
# Copy only files required to install
# dependencies (better layer caching)
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]

View File

@ -0,0 +1,28 @@
FROM node:19.4-bullseye AS build
# Specify working directory other than /
WORKDIR /usr/src/app
# Copy only files required to install
# dependencies (better layer caching)
COPY package*.json ./
# Use cache mount to speed up install of existing dependencies
RUN --mount=type=cache,target=/usr/src/app/.npm \
npm set cache /usr/src/app/.npm && \
npm install
COPY . .
RUN npm run build
# Use separate stage for deployable image
FROM nginx:1.23-alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build usr/src/app/dist/ /usr/share/nginx/html
EXPOSE 80
# No CMD specified... will uses CMD/ENTRYPOINT from base image (nginx:1.23-alpine)

View File

@ -0,0 +1,12 @@
API_NODE_PATH:=../../05-example-web-application/api-node/
N?=0
.PHONY: build-N
build-N:
docker build --file ./Dockerfile.$(N) -t api-node:$(N) ${API_NODE_PATH}
.PHONY: build-all
build-all:
for number in 0 1 2 3 ; do \
N=$$number $(MAKE) build-N; \
done

View File

@ -0,0 +1,28 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react-swc';
import dns from 'dns';
dns.setDefaultResultOrder('verbatim');
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
server: {
proxy: {
'/api/golang': {
// TODO: Make this inside and outside of docker (e.g. localhost vs api-golang)
target: 'http://api-golang:8080',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api\/golang/, ''),
secure: false,
},
'/api/node': {
// TODO: Make this inside and outside of docker (e.g. localhost vs api-node)
target: 'http://api-node:3000',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api\/node/, ''),
secure: false,
},
},
},
});

View File

@ -0,0 +1,48 @@
services:
client-react:
build:
context: ../05-example-web-application/client-react/
dockerfile: ../../06-building-container-images/client-react/Dockerfile
ports:
- 5173:5173
volumes:
- ${PWD}/client-react/vite.config.js:/usr/src/app/vite.config.js
client-react-nginx:
build:
context: ../05-example-web-application/client-react/
dockerfile: ../../06-building-container-images/client-react/Dockerfile.3
init: true
ports:
- 5174:80
api-node:
build:
context: ../05-example-web-application/api-node/
dockerfile: ../../06-building-container-images/api-node/Dockerfile.7
init: true
depends_on:
- db
environment:
- PGUSER=postgres
- PGHOST=db
- PGPASSWORD=foobarbaz
- PGDATABASE=postgres
- PGPORT=5432
ports:
- 3000:3000
api-golang:
build:
context: ../05-example-web-application/api-golang/
dockerfile: ../../06-building-container-images/api-golang/Dockerfile.6
init: true
depends_on:
- db
environment:
- DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres
ports:
- 8080:8080
db:
image: postgres:15.1-alpine
environment:
- POSTGRES_PASSWORD=foobarbaz
ports:
- 5432:5432

View File

@ -0,0 +1,37 @@
# syntax=docker/dockerfile:1
# escape=\
# ^ OPTIONAL "directives" (must be at top if used)
# THIS IS A COMMENT
# ARG is the only instruction that can come before FROM
ARG BASE_IMAGE_TAG=19.4
# ARGs can be overriden at build time
# > docker build --build-arg BASE_VERSION=19.3 .
FROM node:${BASE_IMAGE_TAG}
RUN echo "Hey Team 👋 (shell form)"
RUN ["echo", "Hey Team 👋 (exec form)"]
# --mount allows for mounting additional files
# into the build context
# RUN --mount=type=bind ...
# RUN --mount=type=cache ...
# RUN --mount=type=secret ...
# RUN --mount=type=ssh ...
# Available only at build time
# (Still in image metadata though...)
ARG BUILD_ARG=foo
# Available at build and run time
ENV ENV_VAR=bar
# Set the default working directory
# Use the convention of your language/framework
WORKDIR path/to/the/working/directory
ENTRYPOINT [ "echo", "Hey Team 👋 (entrypoint)" ]
CMD [ "+ (cmd)" ]