如何将EC2实例上运行的Jenkins作业迁移至Docker容器
Hey there! Migrating Jenkins jobs from an EC2 instance to a Docker container is a smart move for better consistency, portability, and scalability. Let’s break down the process step by step to make it smooth.
Before touching anything, make sure you have a full backup of your Jenkins instance on EC2—this is your safety net.
- Locate your
JENKINS_HOMEdirectory (usually/var/lib/jenkinson EC2). - Create a compressed backup with:
tar -czf jenkins_full_backup.tar.gz /var/lib/jenkins - Transfer this backup file to your local machine or a secure storage location (like S3) to avoid losing it if something goes wrong.
Start with a clean, official Jenkins Docker image to build your new environment:
- Pull the LTS (long-term support) image for stability:
docker pull jenkins/jenkins:lts - Run the container with persistent storage (so your data survives container restarts) and port mappings:
docker run -d -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home --name migrated_jenkins jenkins/jenkins:lts8080: Jenkins web UI port50000: Port for agent node communicationjenkins_home: Docker volume to persist all Jenkins data (jobs, plugins, configs)
Your job definitions and dependencies live in the JENKINS_HOME backup—let’s move them over:
Migrate Jobs
- Extract your EC2 backup locally, then copy the
jobsdirectory to the Docker container:docker cp /path/to/extracted_backup/jobs migrated_jenkins:/var/jenkins_home/ - For individual jobs, copy specific job folders instead of the entire directory:
docker cp /path/to/extracted_backup/jobs/your-job-folder migrated_jenkins:/var/jenkins_home/jobs/
Migrate Plugins
Jobs often rely on specific plugins—don’t forget these:
- Copy the
pluginsdirectory from your EC2 backup to the container:docker cp /path/to/extracted_backup/plugins migrated_jenkins:/var/jenkins_home/ - Alternatively, you can reinstall plugins via the Jenkins web UI (go to Manage Jenkins > Plugins) if you prefer a fresh start.
EC2 and Docker containers have different system environments—you’ll need to adjust configurations:
- Tool Paths: If your jobs reference tools like Java, Maven, or Python, update their paths in Manage Jenkins > Global Tool Configuration. Docker containers might have these tools in different locations, or you may need to install them first (e.g., via
docker execto run commands inside the container). - Environment Variables: Any system-level env vars from EC2 can be passed to the container when starting it:
Or set them globally in Jenkins via Manage Jenkins > Configure System > Global properties.docker run -d -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home -e MY_VAR=my-value --name migrated_jenkins jenkins/jenkins:lts
Don’t switch over production until you’ve verified all jobs work:
- Access the Jenkins web UI at
http://<your-docker-host-ip>:8080 - Run a few test builds for your critical jobs
- Check build logs for errors (e.g., missing tools, path issues, plugin conflicts)
- Verify that artifacts, reports, and notifications work exactly as they did on EC2
For long-term management, use Docker Compose to simplify container setup and persistence. Create a docker-compose.yml file:
version: '3' services: jenkins: image: jenkins/jenkins:lts ports: - "8080:8080" - "50000:50000" volumes: - jenkins_home:/var/jenkins_home restart: always # Auto-restart if the container crashes volumes: jenkins_home:
Start it with:
docker-compose up -d
Pro tip: Always test the migration in a staging environment first before touching your production EC2 instance!
内容的提问来源于stack exchange,提问作者Eswanth Reddy

