Skip to content

Commit 2ed1d2d

Browse files
committed
Update to debian 12. Install dependencies via venv to avoi useless copy files in next stage and reduce image size. Better apt-get install and no upgrade. Removed wheel install because already specified in requirements.txt. Use groupadd and useradd instead of addgroup and adduser. Copy entire folder, useless files are ignored via dockerignore. Set PATH to venv. Added TZ specification.
1 parent 0dc2be7 commit 2ed1d2d

1 file changed

Lines changed: 22 additions & 28 deletions

File tree

Dockerfile

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
1-
FROM python:3.9-slim-bullseye AS PREBUILD
1+
FROM python:3.9-slim-bookworm AS builder
22

3-
# Install dependencies
4-
RUN apt-get update && apt-get -y upgrade && apt-get install -y git-core
5-
6-
COPY requirements.txt /tmp/requirements.txt
7-
8-
RUN pip3 install --upgrade pip && \
9-
pip3 install wheel && \
10-
pip3 install -r /tmp/requirements.txt && \
11-
pip3 install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git
12-
13-
14-
FROM python:3.9-slim-bullseye
3+
COPY requirements.txt .
154

165
# Install dependencies
17-
RUN apt-get update && apt-get -y upgrade
6+
RUN apt-get update && apt-get install -y --no-install-recommends git && \
7+
rm -rf /var/lib/apt/lists/* && \
8+
python3 -m venv /opt/netbox-sync/venv && \
9+
/opt/netbox-sync/venv/bin/python3 -m pip install --upgrade pip && \
10+
/opt/netbox-sync/venv/bin/pip install -r requirements.txt && \
11+
/opt/netbox-sync/venv/bin/pip install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git
1812

19-
# Run the application
20-
WORKDIR /app
13+
FROM python:3.9-slim-bookworm AS netbox-sync
2114

22-
RUN set -eux; \
23-
addgroup --gid 1000 netbox-sync; \
24-
adduser --uid 1000 --ingroup netbox-sync --shell /bin/sh --home /home/netbox-sync --disabled-password \
25-
--gecos "netbox-sync,0815,2342,9001" netbox-sync
15+
# Copy installed packages
16+
COPY --from=builder /opt/netbox-sync/venv /opt/netbox-sync/venv
2617

27-
# Prepare the application
28-
COPY Dockerfile LICENSE.txt netbox-sync.py README.md requirements.txt settings-example.ini /app/
29-
COPY module /app/module
18+
# Add netbox-sync user
19+
RUN groupadd --gid 1000 netbox-sync && \
20+
useradd --uid 1000 --gid netbox-sync --shell /bin/sh \
21+
--no-create-home --system netbox-sync
3022

31-
RUN chown -R netbox-sync:netbox-sync /app
23+
USER netbox-sync
3224

33-
# disable upgrading setup tools due to bug in setuptools and automation sdk
34-
# once this is fixed, switch back to: pip3 install --upgrade pip setuptools
35-
COPY --from=PREBUILD /usr/local /usr/local
25+
# Prepare the application
26+
WORKDIR /app
27+
COPY --chown=netbox-sync:netbox-sync . .
3628

37-
USER netbox-sync
29+
# Use virtual env packages and allow timezone setup
30+
ENV PATH=/opt/netbox-sync/venv/bin:$PATH
31+
ENV TZ=Europe/Berlin
3832

3933
ENTRYPOINT ["python3", "netbox-sync.py"]

0 commit comments

Comments
 (0)