let's integrate Docker and your Jenkins declarative pipeline
Use your Docker Build and Run Knowledge
docker build - you can use sh 'docker build . -t <tag>' it
in your pipeline stage block to run the docker build command. (Make sure you have docker installed with correct permissions.
docker run: you can use sh 'docker run -d <image>'
in your pipeline stage block to build the container.
How will the stages look
stages {
stage('Build') {
steps {
sh 'docker build -t trainwithshubham/django-app:latest'
}
}
}
Task-01
Create a docker-integrated Jenkins declarative pipeline
Click on new-items > Pipeline project type >Name the pipeline
Save and run the pipeline.
You should see the pipeline execute each stage and run your application inside a Docker container.
-
Task-02
- Create a docker-integrated Jenkins declarative pipeline using the
docker
groovy syntax inside the stage block.
To include docker in the stage block, the docker pipeline and docker plugin need to be installed.
Go to Dashboard > Manage Jenkins > Manage Plugins > Available Plugins > Select the required plugins ‘Docker’ and ‘Docker Pipeline’> Click on Install without restart.
Write the code
The above defines a pipeline with two stages: build
and Run
. The build
stage uses the Docker agent to build a Docker image based on the specified image name. The reuseNode
option is set to true
, indicate that the same node (Jenkins agent) should be reused for both the build
and Run
stages. The steps
block within the build
stage contains a shell command that prints the Python version
We tried running docker-compose in the above code.
save and run the pipeline.
This script defines a pipeline with two stages: Build
and Run
. The Build
stage uses the Docker agent to build a Docker image based on the specified image name. The steps
block within the Build
stage prints a message indicating that the app is being built using the Docker image.
The Run
stage contains two shell steps that execute docker-compose
commands. Specifically, it stops and removes any existing containers defined in your docker-compose.yml
file using docker-compose down
, and then starts the containers again in detached mode using docker-compose up -d
.
I hope you like this article.
Happy learning!