Git Meets Docker: Turbocharge Your Local Web Dev Workflow
When building a web app with Docker on a local server (e.g., a Raspberry Pi or your dev machine), integrating Git version control streamlines development, tracks changes, and ensures smooth deployment. This guide shows how to combine Git and Docker effectively, using a Node.js web app as an example.
Why Combine Git and Docker?
- Git: Tracks code changes, enables experimentation (branches), and supports collaboration or backups (remote repos).
- Docker: Ensures consistent environments and simplifies deployment.
- Interplay: Git manages your source code, while Docker builds and runs it. Volume mounting in Docker syncs live changes without rebuilding, and Git commits lock in stable versions.
Step 1: Set Up Git Locally
- Install Git:
On Ubuntu:
Verify:sudo apt install git
git --version
- Initialize a Git Repository:
In your project folder (e.g.,
/path/to/webapp
):git init
- Add Files:
Stage your web app files (
server.js
,Dockerfile
, etc.):git add .
- Exclude Sensitive Files:
Create
.gitignore
:
Re-stage:node_modules/ .env
git add .gitignore
- Commit:
Save your initial state:
git commit -m "Initial commit: Web app setup"
Step 2: Dockerize Your Web App
Assume a Node.js app with this Dockerfile
:
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 4000
CMD ["npm", "run", "dev"] # Assumes nodemon in package.json
- Build Once:
docker build -t webapp .
- Run with Volume:
docker run -d -p 4000:4000 --name webapp-container --env-file .env -v $(pwd):/app webapp
-v $(pwd):/app
: Syncs local files to the container, enabling live updates.
Step 3: Git and Docker Workflow
Here’s how they interplay:
- Develop with Live Updates:
Edit
server.js
or HTML files. Docker’s volume mount syncs changes instantly.nodemon
(if used) restarts the app—no rebuild needed. Test:http://localhost:4000
. - Commit Changes:
When satisfied:
git add server.js git commit -m "Added new endpoint"
- Handle Dependencies or Dockerfile Changes:
Update
package.json
orDockerfile
? Rebuild:
Commit:docker stop webapp-container docker rm webapp-container docker build -t webapp . docker run -d -p 4000:4000 --name webapp-container --env-file .env -v $(pwd):/app webapp
git commit -m "Added dependency X"
- Branch for Features:
Experiment safely:
git checkout -b feature/new-ui # Edit files, test in Docker git commit -m "Updated UI" git checkout main git merge feature/new-ui
Step 4: Backup with a Remote Repo (Optional)
- Push to GitHub:
Create a repo on GitHub, then:
After changes:git remote add origin https://github.com/yourusername/webapp.git git push -u origin main
git push
Step 5: Version Your Docker Images
- Tag with Git Commit:
After a commit:
git rev-parse --short HEAD # e.g., "a1b2c3d" docker build -t webapp:a1b2c3d .
- Deploy: Run the tagged image without
-v
for production.
How Git and Docker Interplay
- Development:
Git tracks source code (
server.js
, etc.). Docker runs the app, syncing changes via-v
. No rebuilds for code edits—commit when stable. - Stabilization: Git branches isolate features; Docker tests them live.
- Deployment: Git pushes a stable version; Docker builds a final image.
Example Workflow
- Start:
docker run ... -v $(pwd):/app ...
- Edit
server.js
, see changes atlocalhost:4000
. - Commit:
git commit -m "Feature done"
- Push:
git push
- Deploy: Rebuild and run without
-v
.
Tips
.env
: Never commit—keep it local.- Rebuilds: Only for dependencies or
Dockerfile
. - Logs: Check
if stuck.docker logs webapp-container
With Git and Docker, you get fast iteration and reliable versioning for your web app—happy coding!