Update Dockerfile
Browse files- Dockerfile +16 -14
Dockerfile
CHANGED
|
@@ -1,29 +1,31 @@
|
|
|
|
|
| 1 |
FROM node:20-slim
|
| 2 |
|
| 3 |
-
# Create a non-root user 'appuser'
|
| 4 |
RUN useradd -m -s /bin/bash appuser
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Set the application directory inside the user's home
|
| 7 |
-
ENV APP_HOME
|
|
|
|
| 8 |
WORKDIR ${APP_HOME}
|
| 9 |
|
| 10 |
-
# Copy package files
|
| 11 |
COPY package*.json ./
|
| 12 |
|
| 13 |
-
#
|
| 14 |
RUN npm install
|
| 15 |
|
| 16 |
-
# Copy the rest of the application
|
| 17 |
COPY . .
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
# This is the crucial step. We create the directory as root, then give
|
| 21 |
-
# the non-root user ownership of everything.
|
| 22 |
-
RUN mkdir -p ${APP_HOME}/images && \
|
| 23 |
-
chown -R appuser:appuser ${APP_HOME}
|
| 24 |
-
|
| 25 |
-
# Switch to the non-root user
|
| 26 |
-
USER appuser
|
| 27 |
-
|
| 28 |
EXPOSE 7860
|
|
|
|
|
|
|
| 29 |
CMD ["node", "server.js"]
|
|
|
|
| 1 |
+
# Use a specific version for reproducibility
|
| 2 |
FROM node:20-slim
|
| 3 |
|
| 4 |
+
# Create a non-root user 'appuser' and its home directory
|
| 5 |
RUN useradd -m -s /bin/bash appuser
|
| 6 |
|
| 7 |
+
# Switch to the non-root user *early*.
|
| 8 |
+
# All subsequent commands will be run as 'appuser'.
|
| 9 |
+
USER appuser
|
| 10 |
+
|
| 11 |
+
# Set the user's home directory as a base
|
| 12 |
+
ENV HOME=/home/appuser
|
| 13 |
# Set the application directory inside the user's home
|
| 14 |
+
ENV APP_HOME=${HOME}/app
|
| 15 |
+
# Set the WORKDIR. Since we are 'appuser', this directory will be created and owned by 'appuser'.
|
| 16 |
WORKDIR ${APP_HOME}
|
| 17 |
|
| 18 |
+
# Copy package files. They will be owned by 'appuser' because of the USER command above.
|
| 19 |
COPY package*.json ./
|
| 20 |
|
| 21 |
+
# Run npm install as 'appuser'. It has permission to write to its own WORKDIR.
|
| 22 |
RUN npm install
|
| 23 |
|
| 24 |
+
# Copy the rest of the application code. It will also be owned by 'appuser'.
|
| 25 |
COPY . .
|
| 26 |
|
| 27 |
+
# Expose the port
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
EXPOSE 7860
|
| 29 |
+
|
| 30 |
+
# Run the server. The process starts as 'appuser' and has full control over the APP_HOME directory.
|
| 31 |
CMD ["node", "server.js"]
|