Test docker

This commit is contained in:
Sam Liu 2024-09-09 09:42:20 +00:00
parent 9b2f9f6da1
commit 16c0c24fae
6 changed files with 204 additions and 2 deletions

View File

@ -31,3 +31,8 @@ tasks:
dir: frontend dir: frontend
cmds: cmds:
- pnpm dev - pnpm dev
docker:
desc: builds and runs the production docker image locally
dir: docker
cmds:
- docker compose -f docker-compose.yml -p fuware up -d --build

View File

@ -39,7 +39,7 @@
"class": "logging.handlers.RotatingFileHandler", "class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG", "level": "DEBUG",
"formatter": "detailed", "formatter": "detailed",
"filename": "${DATA_DIR}/mealie.log", "filename": "${DATA_DIR}/fuware.log",
"maxBytes": 10000, "maxBytes": 10000,
"backupCount": 3 "backupCount": 3
} }

124
docker/Dockerfile Normal file
View File

@ -0,0 +1,124 @@
FROM node:18.20.4-alpine3.20 as builder
WORKDIR /app
COPY ./frontend .
RUN npm install -g pnpm
RUN pnpm install --prefer-offline=true --frozen-lockfile=true
RUN pnpm build
###############################################
# Base Image - Python
###############################################
FROM python:3.10-slim as python-base
ENV FUWARE_HOME="/app"
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_HOME="/opt/poetry" \
POETRY_VIRTUALENVS_IN_PROJECT=true \
POETRY_NO_INTERACTION=1 \
PYSETUP_PATH="/opt/pysetup" \
VENV_PATH="/opt/pysetup/.venv"
# prepend poetry and venv to path
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"
# create user account
RUN useradd -u 911 -U -d $FUWARE_HOME -s /bin/bash abc \
&& usermod -G users abc \
&& mkdir $FUWARE_HOME
###############################################
# Builder Image
###############################################
FROM python-base as builder-base
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
curl \
build-essential \
libpq-dev \
libwebp-dev \
# LDAP Dependencies
libsasl2-dev libldap2-dev libssl-dev \
gnupg gnupg2 gnupg1 \
&& rm -rf /var/lib/apt/lists/* \
&& pip install -U --no-cache-dir pip
# install poetry - respects $POETRY_VERSION & $POETRY_HOME
ENV POETRY_VERSION=1.3.1
RUN curl -sSL https://install.python-poetry.org | python3 -
# copy project requirement files here to ensure they will be cached.
WORKDIR $PYSETUP_PATH
COPY ./poetry.lock ./pyproject.toml ./
# install runtime deps - uses $POETRY_VIRTUALENVS_IN_PROJECT internally
RUN poetry install --only main
###############################################
# Production Image
###############################################
FROM python-base as production
ENV PRODUCTION=true
ENV TESTING=false
ARG COMMIT
ENV GIT_COMMIT_HASH=$COMMIT
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
gosu \
iproute2 \
libldap-common \
libldap-2.5 \
&& rm -rf /var/lib/apt/lists/*
# create directory used for Docker Secrets
RUN mkdir -p /run/secrets
# copying poetry and venv into image
COPY --from=builder-base $POETRY_HOME $POETRY_HOME
COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH
# copy backend
COPY ./backend $FUWARE_HOME/backend
COPY ./poetry.lock ./pyproject.toml $FUWARE_HOME/
# Alembic
COPY ./alembic $FUWARE_HOME/alembic
COPY ./alembic.ini $FUWARE_HOME/
# venv already has runtime deps installed we get a quicker install
WORKDIR $FUWARE_HOME
RUN . $VENV_PATH/bin/activate && poetry install --only main
WORKDIR /
VOLUME [ "$FUWARE_HOME/data/" ]
ENV APP_PORT=9000
EXPOSE ${APP_PORT}
HEALTHCHECK CMD python $FUWARE_HOME/backend/scripts/healthcheck.py || exit 1
# ----------------------------------
# Copy Frontend
# copying caddy into image
ENV STATIC_FILES=/spa/static
COPY --from=builder /app/dist ${STATIC_FILES}
ENV HOST 0.0.0.0
EXPOSE ${APP_PORT}
COPY ./docker/entry.sh $FUWARE_HOME/run.sh
RUN chmod +x $FUWARE_HOME/run.sh
ENTRYPOINT ["/app/run.sh"]

30
docker/docker-compose.yml Normal file
View File

@ -0,0 +1,30 @@
services:
mealie:
container_name: mealie
image: mealie:dev
build:
context: ../
target: production
dockerfile: ./docker/Dockerfile
restart: always
volumes:
- mealie-data:/app/data/
ports:
- 9091:9000
environment:
ALLOW_SIGNUP: "false"
LOG_LEVEL: "DEBUG"
# =====================================
# Email Configuration
# SMTP_HOST=
# SMTP_PORT=587
# SMTP_FROM_NAME=Mealie
# SMTP_AUTH_STRATEGY=TLS # Options: 'TLS', 'SSL', 'NONE'
# SMTP_FROM_EMAIL=
# SMTP_USER=
# SMTP_PASSWORD=
volumes:
mealie-data:
driver: local

44
docker/entry.sh Normal file
View File

@ -0,0 +1,44 @@
#!/bin/bash
# Start Backend API
# Get PUID/PGID
PUID=${PUID:-911}
PGID=${PGID:-911}
BASH_SOURCE=${BASH_SOURCE:-$0}
add_user() {
groupmod -o -g "$PGID" abc
usermod -o -u "$PUID" abc
}
change_user() {
if [ "$(id -u)" = $PUID ]; then
echo "
User uid: $PUID
User gid: $PGID
"
elif [ "$(id -u)" = "0" ]; then
# If container is started as root then create a new user and switch to it
add_user
chown -R $PUID:$PGID /app
echo "Switching to dedicated user"
exec gosu $PUID "$BASH_SOURCE" "$@"
fi
}
init() {
# $FUWARE_HOME directory
cd /app
# Activate our virtual environment here
. /opt/pysetup/.venv/bin/activate
}
change_user
init
# Start API
HOST_IP=`/sbin/ip route|awk '/default/ { print $3 }'`
exec python /app/fuware/main.py

View File

@ -3,7 +3,6 @@ name = "backend"
version = "0.1.0" version = "0.1.0"
description = "project for manage item with exp date" description = "project for manage item with exp date"
authors = ["Sam Liu <luu.dat.tham@gmail.com>"] authors = ["Sam Liu <luu.dat.tham@gmail.com>"]
readme = "README.md"
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.10" python = "^3.10"