365cb6692e10731a5ad3af0c34225fd1950d7bf8
Jenkins Docker Deploy Example
Example repository demonstrating a Jenkins pipeline that:
- Builds a Docker image
- Pushes the image to a container registry (Docker Hub, etc.)
- SSHs to a deployment machine
- Clones (or pulls) this repo to get
docker-compose.yml - Deploys with
docker compose up -d
Repository Structure
.
├── Jenkinsfile # Pipeline definition
├── Dockerfile # Application image
├── docker-compose.yml # Deployment compose (pulled by deploy host)
├── app.py # Minimal Python web app
└── README.md
Prerequisites
Jenkins
- Docker installed and Jenkins user in
dockergroup - Pipeline and SSH Agent plugins
- Git for cloning
Jenkins Credentials
Create these in Manage Jenkins → Credentials:
| ID | Type | Purpose |
|---|---|---|
docker-registry-credentials |
Username/Password | Docker Hub or registry login |
deploy-ssh-key |
SSH Username with private key | SSH to deploy host |
Deploy Host
- Docker and Docker Compose installed
- SSH access for the deploy user
- If using a private registry: run
docker loginon the deploy host (or add to the pipeline)
Configuration
Edit the environment block in Jenkinsfile:
environment {
DOCKER_REGISTRY = 'docker.io' // or your registry (ghcr.io, etc.)
DOCKER_IMAGE = 'myorg/myapp' // e.g., username/repo
DEPLOY_HOST = 'deploy-server.example.com'
DEPLOY_USER = 'deploy'
DEPLOY_PATH = '/opt/myapp' // Where to clone & run compose
GIT_REPO_URL = 'https://github.com/myorg/jenkins-docker-deploy-example.git'
}
Pipeline Stages
- Build –
docker buildwith tag from branch (e.g.latestfor main,123-abc1234for others) - Push –
docker pushto registry using stored credentials - Deploy – SSH to host, clone/pull repo, run
docker compose up -d
First-Time Deploy Host Setup
On the deploy host:
# Create deploy directory
sudo mkdir -p /opt/myapp
sudo chown deploy:deploy /opt/myapp
# Ensure deploy user can run docker (add to docker group)
sudo usermod -aG docker deploy
Manual Test
# Build and run locally
docker build -t myapp:test .
docker run -p 8080:8080 myapp:test
# Visit http://localhost:8080
Branch Behavior
- main → image tag
latest - other branches → image tag
{BUILD_NUMBER}-{GIT_SHA}
Description
Languages
Python
57.7%
HTML
29.3%
CSS
12.1%
Dockerfile
0.8%