To deploy a Node.js Express Git repo to an Nginx server using a pre-built Dockerfile, you can follow these steps:
- Clone your Node.js Express Git repo to your local machine.
git clone https://github.com/your-username/your-repo.git
- Navigate to the directory containing your Node.js Express Git repo.
cd your-repo
- Build the Docker image for your Node.js Express application.
docker build -t node-express-app .
- Push the Docker image to a Docker registry.
For example, to push the Docker image to Docker Hub, you would run the following command:
docker push your-username/node-express-app
- Log in to your Nginx server and create a new directory for your Node.js Express application.
ssh root@nginx-server-ip-address
mkdir -p /var/www/node-express-app
- Copy the Nginx configuration file from your local machine to the Nginx server.
scp ./nginx.conf root@nginx-server-ip-address:/etc/nginx/conf.d/default.conf
- Create a new systemd service file for your Node.js Express application.
cat << EOF > /etc/systemd/system/node-express-app.service
[Unit]
Description=Node.js Express App
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/docker run -d -p 80:80 node-express-app
[Install]
WantedBy=multi-user.target
EOF
- Reload the systemd daemon and start the Node.js Express application service.
systemctl daemon-reload
systemctl start node-express-app.service
- Enable the Node.js Express application service to start on boot.
systemctl enable node-express-app.service
Once these steps are complete, your Node.js Express application will be deployed on your Nginx server and accessible at http://nginx-server-ip-address
.
Here is an example of a pre-built Dockerfile for a Node.js Express application:
FROM node:alpine WORKDIR /usr/src/app COPY package.json . RUN npm install COPY . . EXPOSE 80 CMD ["npm", "start"]
This Dockerfile will build a Docker image that contains your Node.js Express application and all of its dependencies. The image will also be configured to expose port 80, which is the port that Nginx uses to listen for incoming requests.
Here is an example of an Nginx configuration file for a Node.js Express application:
upstream node-express-app { server localhost:80; } server { listen 80; server_name nginx-server-ip-address; location / { proxy_pass http://node-express-app; } }
This Nginx configuration file will proxy all requests to your Node.js Express application running on port 80.