π― Automating Docker Workflows with Jenkins: A Step-by-Step Guide π
π Task 1: Jenkins Freestyle Project for Your Dockerized App π³
π Step 1: Set Up Jenkins Agent for Your App
Install Jenkins on Your Machine π οΈ:
Deploy Jenkins using Docker:
docker run -d -p 8080:8080 -p 50000:50000 --name jenkins-server -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts
Access Jenkins at
http://localhost:8080
π.
Install the Necessary Plugins β‘:
Go to Manage Jenkins > Plugin Manager.
Install these must-have plugins:
π³ Docker Pipeline
π§° Git
π SSH Agent
Create an Agent (a.k.a. Jenkins Node π€):
Navigate to Manage Jenkins > Manage Nodes and Clouds > New Node.
Configure:
π Remote Directory
π·οΈ Labels for Agent Selection
π Launch Method (e.g., SSH).
π― Step 2: Build the Freestyle Project
Create the Project π οΈ:
- Go to New Item > Name it something cool like "DockerAppBuilder" > Select Freestyle Project > Click OK.
Connect Your Repository π:
In the Source Code Management section:
Select Git.
Enter the URL of your app repository π¦.
Add Build Steps βοΈ:
In the Build section:
Step 1: Add a shell script to build your Docker image π³:
docker build -t my-awesome-app-image .
π οΈ This step creates your app image. Cool, right?
Step 2: Add another shell script to run your Docker container π’:
docker run -d --name my-awesome-app-container -p 80:80 my-awesome-app-image
π Your app will now be live!
Save and Test π:
- Hit Save and then click Build Now βΆοΈ to see your app come to life. π
π Task 2: Jenkins Project with Docker Compose Magic π§ββοΈ
π Step 1: Create the Jenkins Project
Create Another Project π οΈ:
- Go to New Item > Name it "ComposeWizard" β¨ > Select Freestyle Project > Click OK.
Link Your Repo π:
In the Source Code Management section:
- Select Git and provide the repo URL with your
docker-compose.yml
file ποΈ.
- Select Git and provide the repo URL with your
π― Step 2: Docker Compose Setup
Add Build Steps π§:
Step 1: Add a shell script to bring up your containers π’:
docker-compose up -d
β¨ This will spin up your app and database effortlessly!
Step 2: Add a cleanup step π§Ή:
docker-compose down
𧽠Tidy up those containers when youβre done!
Save and Build π:
- Click Save and then Build Now βΆοΈ to see your multi-container setup in action!