Step-by-Step Guide to Create a Jenkins Job
Prerequisites
Jenkins Installed: Make sure you have Jenkins installed and running.
Jenkins Node with SSH Access: Ensure that you have a Jenkins node (agent) where you can install Docker.
SSH Credentials: Configure SSH access for the Jenkins user to the target node.
Step 1: Create a New Freestyle Job
Open your Jenkins dashboard.
Click on "New Item".
Enter a name for your job (e.g.,
Install Docker and Launch Container
).Select "Freestyle project" and click OK.
Step 2: Configure the Job
In the job configuration page, scroll down to the "Build" section.
Click on "Add build step" and select "Execute shell".
Step 3: Add the Shell Script
In the "Command" text area, add the following shell script:
#!/bin/bash
# Update package index
sudo apt-get update -y
# Install required packages for Docker installation
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Add the Docker APT repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# Install Docker
sudo apt-get update -y
sudo apt-get install -y docker-ce
# Start and enable Docker service
sudo systemctl start docker
sudo systemctl enable docker
# Pull the Nginx Docker image
sudo docker pull nginx
# Launch Nginx container
sudo docker run -d --name my_nginx -p 80:80 nginx
Explanation of the Shell Script
Update and Install Packages: The script updates the package index and installs necessary packages for Docker installation.
Add Docker GPG Key and Repository: It adds the Docker GPG key and repository to the system.
Install Docker: The script installs Docker Community Edition (CE).
Start Docker Service: It starts and enables the Docker service.
Pull Nginx Image and Launch Container: Finally, it pulls the Nginx image and launches it in a container named
my_nginx
, mapping port 80 of the host to port 80 of the container.
Step 4: Save and Run the Job
Click "Save" at the bottom of the job configuration page.
Go back to the job's main page and click "Build Now" to execute the job.
Step 5: Verify the Installation
Once the job completes successfully, you can verify the installation and check if the container is running:
SSH into your Jenkins node (if it’s a different server).
Run the following command to check if the Nginx container is running:
sudo docker ps
You can also access the Nginx web server by navigating to
http://<your_server_ip>
in a web browser.
Additional Considerations
Jenkins User Permissions: Ensure that the Jenkins user has the necessary permissions to run Docker commands. You might need to add the Jenkins user to the Docker group:
sudo usermod -aG docker jenkins
Error Handling: You may want to add error handling in your shell script to manage any installation failures.
Docker Compose: If you want to launch more complex applications, consider using Docker Compose and modifying the shell script accordingly.
Conclusion
By following these steps, you have successfully created a Jenkins job that installs Docker and launches a container automatically. This setup can be further customized to suit more complex deployment scenarios.