Preparing the Docker Environment for Jenkins
Installing Docker
Before setting up Jenkins in a Docker environment, you'll need to install Docker on your system. Here's how you can install Docker on Ubuntu 22.04:
## Update the package index
sudo apt-get update
## Install packages to allow apt to use a repository over HTTPS
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
## Add Docker's official GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
## Set up the Docker repository
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
## Install Docker Engine
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
Verifying Docker Installation
After installing Docker, you can verify the installation by running the following command:
sudo docker run hello-world
This command will download a test image and run a container, which should output a message confirming that the Docker installation is working correctly.
Creating a Docker Network for Jenkins
To ensure that the Jenkins container can communicate with other containers (e.g., for building and testing applications), you should create a dedicated Docker network for Jenkins. You can do this using the following command:
sudo docker network create jenkins
This will create a new Docker network named "jenkins" that you can use when running the Jenkins container.
Preparing Persistent Storage for Jenkins
Jenkins requires persistent storage to store its configuration, plugins, and build artifacts. You can create a Docker volume to provide this persistent storage:
sudo docker volume create jenkins-data
This will create a Docker volume named "jenkins-data" that you can use when running the Jenkins container.
Configuring Docker Permissions
To allow Jenkins to interact with the Docker daemon, you'll need to grant the Jenkins user the necessary permissions. You can do this by adding the Jenkins user to the "docker" group:
sudo usermod -aG docker jenkins
After making this change, you'll need to restart the Jenkins service for the changes to take effect.