Dockerize nodejs application
Deploying a Node.js project using Docker from GitHub with a deploy key involves a few main steps:
Overview
Generate and configure a deploy key on GitHub.
Write a Dockerfile for your Node.js project.
Create a Docker container or server environment (e.g., a VPS or cloud instance).
Configure SSH to use the deploy key to clone the private repository
Run Docker build and start the app.
1. Create a Deploy Key on GitHub
Deploy keys are SSH keys associated with a single repository. This means they can only be used for cloning one repository.
1.1 Generate SSH Key (no passphrase)
ssh-keygen -t ED25519 -C "<yourcomment>" -f ./deploy_key_name
You’ll get:
deploy_key_name(private)deploy_key_name.pub(public)
1.2. Add Public Key to GitHub Repo
Go to
→ Settings → Deploy keys .Click "Add deploy key":
Title: anything (e.g.,
Docker Deploy)Paste the contents of
deploy_key.pub. this file can be opend using an text editor✅ Check "Allow write access" if your deployment needs the ability to push. Otherwise, leave it unchecked (Besic need for this totorial).
Save.
2. Create a Dockerfile
In your Node.js project (in GitHub), make sure you have a Dockerfile like this (basic configuration):
# Use Node base image FROM node:lts-alpine # Create app directory WORKDIR /app # Install dependencies COPY backend/package*.json ./ RUN npm install # Bundle app source COPY backend/ . # Expose port and start app #EXPOSE 3000 #not needed if ports get exposed in the docker compose. CMD [ "npm", "start" ]
3. Set up the Deployment Server
On your server (e.g., Ubuntu VPS):
3.1 Install Docker (if not installed):
curl -fsSL https://get.docker.com -o get-docker.sh sh get-docker.sh
3.2 Copy the Private Deploy Key to the Server
scp deploy_key user@yourserver:/home/user/.ssh/deploy_key
Then, SSH into the server and set correct permissions:
chmod 600 ~/.ssh/deploy_key
4. Clone the GitHub Repo Using the Deploy Key
Since Git uses SSH, we need to override the SSH config so Git knows to use this key.
Create or modify ~/.ssh/config on the server:
Host github_<projectname> HostName github.com User git IdentityFile ~/.ssh/deploy_key IdentitiesOnly yes
Then test:
ssh github_<projectname>
You should see something like:
Hi username/repo! You've successfully authenticated...
Now clone your private repo:
git clone github_projectname:yourusername/your-node-project.git cd your-node-project
5. Build and Run Docker
5.1 Build the Docker Image
docker build -t my-node-app .
5.2 Run the Container
docker run -d -p 3000:3000 --name my-node-app-container my-node-app
Your app should now be running at http://<your-server-ip>:3000.
Optional: Automate with a Shell Script
You could make a deploy.sh:
#!/bin/bash # Ensure SSH agent is running and key is added eval "$(ssh-agent -s)" ssh-add ~/.ssh/deploy_key # Clone/update the repo if [ ! -d "your-node-project" ]; then git clone git@github.com:yourusername/your-node-project.git else cd your-node-project git pull origin main cd .. fi # Build and run cd your-node-project docker build -t my-node-app . docker stop my-node-app-container && docker rm my-node-app-container docker run -d -p 3000:3000 --name my-node-app-container my-node-app
Tips
- Never commit deploy keys to your repo.
Would you like me to generate an example repo structure, Dockerfile, and script for you?