Changes to Dockerfile to fix error in installing MongoDB
This commit is contained in:
+21
-46
@@ -13,65 +13,40 @@ COPY package*.json ./
|
|||||||
RUN npm install
|
RUN npm install
|
||||||
COPY src/backend/ ./src/backend/
|
COPY src/backend/ ./src/backend/
|
||||||
|
|
||||||
# Stage 3: Install MongoDB on Debian-based image
|
# Stage 3: Final production image
|
||||||
FROM debian:bullseye AS mongodb-builder
|
|
||||||
|
|
||||||
# Install dependencies and MongoDB
|
|
||||||
RUN apt-get update && apt-get install -y \
|
|
||||||
wget \
|
|
||||||
gnupg \
|
|
||||||
ca-certificates \
|
|
||||||
lsb-release \
|
|
||||||
sudo && \
|
|
||||||
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | apt-key add - && \
|
|
||||||
# Dynamically set the architecture and MongoDB version
|
|
||||||
ARCH=$(uname -m) && \
|
|
||||||
if [ "$ARCH" = "x86_64" ]; then \
|
|
||||||
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/debian $(lsb_release -c | awk '{print $2}')/mongodb-org/5.0 main" > /etc/apt/sources.list.d/mongodb-org-5.0.list; \
|
|
||||||
elif [ "$ARCH" = "aarch64" ]; then \
|
|
||||||
echo "deb [ arch=arm64 ] https://repo.mongodb.org/apt/debian $(lsb_release -c | awk '{print $2}')/mongodb-org/5.0 main" > /etc/apt/sources.list.d/mongodb-org-5.0.list; \
|
|
||||||
else \
|
|
||||||
echo "Unsupported architecture $ARCH"; \
|
|
||||||
exit 1; \
|
|
||||||
fi && \
|
|
||||||
apt-get update && apt-get install -y mongodb-org && \
|
|
||||||
rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
# Stage 4: Final production image
|
|
||||||
FROM node:18-alpine
|
FROM node:18-alpine
|
||||||
|
|
||||||
# Install Nginx and required packages
|
# Install dependencies
|
||||||
RUN apk add --no-cache nginx bash
|
RUN apk add --no-cache \
|
||||||
|
wget \
|
||||||
|
bash \
|
||||||
|
curl \
|
||||||
|
ca-certificates \
|
||||||
|
gnupg \
|
||||||
|
bash \
|
||||||
|
libcurl \
|
||||||
|
&& update-ca-certificates
|
||||||
|
|
||||||
# Create mongodb user and group in the final image
|
# Install MongoDB manually
|
||||||
RUN addgroup -g 1001 mongodb && adduser -D -u 1001 -G mongodb mongodb
|
RUN wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-5.0.7.tgz -O /tmp/mongodb.tgz \
|
||||||
|
&& tar -xvzf /tmp/mongodb.tgz -C /usr/local \
|
||||||
# Install MongoDB binaries from mongodb-builder
|
&& rm /tmp/mongodb.tgz \
|
||||||
COPY --from=mongodb-builder /usr/bin/mongod /usr/bin/mongod
|
&& ln -s /usr/local/mongodb-linux-x86_64-5.0.7/bin/* /usr/local/bin/ \
|
||||||
COPY --from=mongodb-builder /usr/bin/mongo /usr/bin/mongo
|
&& mkdir -p /data/db /var/log/mongodb \
|
||||||
|
&& chown -R mongodb:mongodb /data/db /var/log/mongodb
|
||||||
|
|
||||||
# Configure nginx
|
# Configure nginx
|
||||||
COPY docker/nginx.conf /etc/nginx/nginx.conf
|
COPY docker/nginx.conf /etc/nginx/nginx.conf
|
||||||
COPY --from=frontend-builder /app/dist /usr/share/nginx/html
|
COPY --from=frontend-builder /app/dist /usr/share/nginx/html
|
||||||
|
|
||||||
# Copy backend code
|
# Copy backend
|
||||||
COPY --from=backend-builder /app/node_modules ./node_modules
|
COPY --from=backend-builder /app/node_modules ./node_modules
|
||||||
COPY --from=backend-builder /app/src/backend ./src/backend
|
COPY --from=backend-builder /app/src/backend ./src/backend
|
||||||
|
|
||||||
# Set permissions and prepare directories for MongoDB
|
|
||||||
ENV MONGO_DATA_DIR=/data/db
|
|
||||||
RUN mkdir -p $MONGO_DATA_DIR && \
|
|
||||||
chown -R mongodb:mongodb $MONGO_DATA_DIR
|
|
||||||
|
|
||||||
# Expose necessary ports
|
# Expose necessary ports
|
||||||
EXPOSE 8080 8081 8082 27017
|
EXPOSE 8080 8081 27017
|
||||||
|
|
||||||
# Use an entrypoint script to start services (MongoDB, Nginx, and Node backend)
|
# Use an entrypoint script to run services (nginx, MongoDB, Node backend)
|
||||||
COPY docker/entrypoint.sh /entrypoint.sh
|
COPY docker/entrypoint.sh /entrypoint.sh
|
||||||
RUN chmod +x /entrypoint.sh
|
RUN chmod +x /entrypoint.sh
|
||||||
|
|
||||||
# Switch to a non-root user for security
|
|
||||||
USER mongodb
|
|
||||||
|
|
||||||
# Default command to start the services
|
|
||||||
CMD ["/entrypoint.sh"]
|
CMD ["/entrypoint.sh"]
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
# Start MongoDB in the background
|
# Start MongoDB in the background
|
||||||
echo "Starting MongoDB..."
|
echo "Starting MongoDB..."
|
||||||
mongod --fork --logpath /var/log/mongodb.log --bind_ip 0.0.0.0 --dbpath /data/db
|
mongod --fork --logpath /var/log/mongodb/mongod.log --bind_ip 0.0.0.0 --dbpath /data/db
|
||||||
|
|
||||||
# Wait for MongoDB to fully start (you can adjust the sleep if needed)
|
# Wait for MongoDB to fully start (you can adjust the sleep if needed)
|
||||||
sleep 5
|
sleep 5
|
||||||
@@ -13,8 +13,8 @@ nginx -g "daemon off;" &
|
|||||||
|
|
||||||
# Start Node.js backend (adjust as needed for your backend setup)
|
# Start Node.js backend (adjust as needed for your backend setup)
|
||||||
echo "Starting Node.js backend..."
|
echo "Starting Node.js backend..."
|
||||||
node /app/src/backend/ssh.cjs &
|
node /usr/app/src/backend/ssh.cjs &
|
||||||
node /app/src/backend/database.cjs &
|
node /usr/app/src/backend/database.cjs &
|
||||||
|
|
||||||
# Keep the container running
|
# Keep the container running
|
||||||
wait
|
wait
|
||||||
Reference in New Issue
Block a user