Skip to content

Commit

Permalink
Improved Dockerfile syntax to prevent some unwanted behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLeChat committed Dec 2, 2024
1 parent 5a84f82 commit 71515df
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
14 changes: 7 additions & 7 deletions docker/Dockerfile.development
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ RUN --mount=type=cache,target=.npm \
npm ci && chown -R node:node ./node_modules

# Add some cronjobs for automated tasks
RUN npx tsc --skipLibCheck scripts/create-fake-accounts.ts scripts/expired-files.ts scripts/outdated-notifications.ts
RUN echo "*/5 * * * * node /usr/src/app/scripts/expired-files.js > /dev/null 2>&1" >> /var/spool/cron/crontabs/node
RUN echo "*/5 * * * * node /usr/src/app/scripts/outdated-notifications > /dev/null 2>&1" >> /var/spool/cron/crontabs/node
RUN npx tsc --skipLibCheck scripts/create-fake-accounts.ts scripts/expired-files.ts scripts/outdated-notifications.ts && \
echo "*/5 * * * * node /usr/src/app/scripts/expired-files.js > /dev/null 2>&1" >> /var/spool/cron/crontabs/node && \
echo "*/5 * * * * node /usr/src/app/scripts/outdated-notifications > /dev/null 2>&1" >> /var/spool/cron/crontabs/node

# Add wait script to wait for other services to be ready
ADD --chmod=0755 https://github.com/ufoscout/docker-compose-wait/releases/latest/download/wait /wait
Expand All @@ -37,9 +37,9 @@ ADD --chmod=0755 https://github.com/ufoscout/docker-compose-wait/releases/latest
USER node

# Find and replace some default environment variables
RUN sed -i "s#AUTH_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx#AUTH_SECRET=$(openssl rand -base64 32)#g" .env

# Create a custom entrypoint script
RUN echo "/wait && npm run migrate && node scripts/create-fake-accounts.js && supercronic /etc/crontabs/node & npm run dev" > docker/entrypoint.sh && chmod +x docker/entrypoint.sh
# before creating the custom entrypoint script
RUN sed -i "s#AUTH_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx#AUTH_SECRET=$(openssl rand -base64 32)#g" .env && \
echo "/wait && npm run migrate && node scripts/create-fake-accounts.js && supercronic /etc/crontabs/node & npm run dev" > docker/entrypoint.sh && \
chmod +x docker/entrypoint.sh

CMD ["docker/entrypoint.sh"]
19 changes: 8 additions & 11 deletions docker/Dockerfile.production
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,20 @@ RUN --mount=type=cache,target=.npm \
npm ci && chown -R node:node ./node_modules

# Add some cronjobs for automated tasks
RUN npx tsc --skipLibCheck scripts/create-fake-accounts.ts scripts/expired-files.ts scripts/outdated-notifications.ts
RUN echo "*/5 * * * * node /usr/src/app/scripts/expired-files.js > /dev/null 2>&1" >> /var/spool/cron/crontabs/node
RUN echo "*/5 * * * * node /usr/src/app/scripts/outdated-notifications > /dev/null 2>&1" >> /var/spool/cron/crontabs/node
RUN npx tsc --skipLibCheck scripts/create-fake-accounts.ts scripts/expired-files.ts scripts/outdated-notifications.ts && \
echo "*/5 * * * * node /usr/src/app/scripts/expired-files.js > /dev/null 2>&1" >> /var/spool/cron/crontabs/node && \
echo "*/5 * * * * node /usr/src/app/scripts/outdated-notifications > /dev/null 2>&1" >> /var/spool/cron/crontabs/node

# Add wait script to wait for other services to be ready
ADD --chmod=0755 https://github.com/ufoscout/docker-compose-wait/releases/latest/download/wait /wait

# Use non-root user
USER node

# Build the entire project
RUN npm run build

# Remove all development dependencies
RUN npm prune --production

# Create a custom entrypoint script
RUN echo "/wait && npm run migrate && supercronic /etc/crontabs/node & npm run start" > docker/entrypoint.sh && chmod +x docker/entrypoint.sh
# Build the entire project and remove all development dependencies
# before creating the custom entrypoint script
RUN npm run build && npm prune --production && \
echo "/wait && npm run migrate && supercronic /etc/crontabs/node & npm run start" > docker/entrypoint.sh && \
chmod +x docker/entrypoint.sh

CMD ["docker/entrypoint.sh"]

0 comments on commit 71515df

Please sign in to comment.