From 9ea12ee458643df18a75de19c53f91008dd0542f Mon Sep 17 00:00:00 2001 From: Dennis Postma Date: Fri, 7 Feb 2025 00:59:59 +0100 Subject: [PATCH] Moved bash code into .sh file --- Dockerfile | 16 +++------------- docker-start.sh | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 docker-start.sh diff --git a/Dockerfile b/Dockerfile index 796e788..6e7d011 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,9 +16,6 @@ COPY package*.json ./ # Install application dependencies RUN npm install -# Import migrations -RUN npx mikro-orm migration:up - # Copy the rest of your application code to the container COPY . . @@ -28,16 +25,9 @@ RUN npm run build # Expose the ports your Node.js application, Redis, and MySQL will listen on EXPOSE 80 6379 3306 -# Create a shell script to run Redis, MySQL, run migrations, and start the application in a tmux session -RUN echo '#!/bin/sh' > /usr/src/start.sh && \ - echo 'redis-server --daemonize yes' >> /usr/src/start.sh && \ - echo 'mysqld --user=mysql --datadir=/var/lib/mysql &' >> /usr/src/start.sh && \ - echo 'sleep 10' >> /usr/src/start.sh && \ - echo 'npx prisma migrate deploy' >> /usr/src/start.sh && \ - echo 'tmux new-session -d -s nodeapp "node dist/server.js"' >> /usr/src/start.sh && \ - echo 'echo "App is running in tmux session. Attach with: tmux attach-session -t nodeapp"' >> /usr/src/start.sh && \ - echo 'tail -f /dev/null' >> /usr/src/start.sh && \ - chmod +x /usr/src/start.sh +# Copy and make the startup script executable +COPY docker-start.sh /usr/src/start.sh +RUN chmod +x /usr/src/start.sh # Use the shell script as the entry point CMD ["/usr/src/start.sh"] \ No newline at end of file diff --git a/docker-start.sh b/docker-start.sh new file mode 100644 index 0000000..b6a2329 --- /dev/null +++ b/docker-start.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +# Start Redis in the background +redis-server --daemonize yes + +# Start MySQL +mysqld --user=mysql --datadir=/var/lib/mysql & + +# Wait for MySQL to be ready +while ! mysqladmin ping -h localhost --silent; do + echo "Waiting for MySQL to be ready..." + sleep 2 +done +echo "MySQL is ready!" + +# Run database migrations +npx mikro-orm migration:up + +# Start the Node.js application in a tmux session +tmux new-session -d -s nodeapp "node dist/server.js" +echo "App is running in tmux session. Attach with: tmux attach-session -t nodeapp" + +# Keep container running +tail -f /dev/null \ No newline at end of file