How to Run Jenkins in Docker using Docker Compose with Volumes


VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

By using Docker Compose and volumes, Jenkins can be run in a container that is isolated from the host operating system. Instead of having to install Java on your local machine you may use docker-compose up -d “jnlp=” command which will start the Jenkins instance with an already installed version of Java for you!

The “docker-compose inside jenkins container” is a way to run Jenkins in Docker using Docker Compose. This method uses volumes for persistent storage of the Jenkins data and logs.

How to Run Jenkins in Docker using Docker Compose with Volumes

Do you want to utilize Docker to run Jenkins but aren’t sure whether you should use volumes or keep the data containerized? How do you backup the data if Jenkins is containerized? Is it necessary to do a full backup of the container? Is it possible for information to survive outside of the container, and if so, how? Using volumes with Docker Compose might help you keep your Jenkins data secure even if the Docker container fails.

Using Docker volumes enables you to exchange Jenkins data across many containers in addition to backing up data.

Continue reading to discover how to run Jenkins using Docker and Docker Compose in this post. You’ll learn where to look for Jenkins data both within and outside the Docker container, as well as how to back it up.


This page will provide several learning examples. If you want to follow along, you’ll need the following items:

The installation and configuration of Docker and Docker Compose are not covered in this article. The official Docker documentation may be found here. You may also want to check out the ATA blog, which includes a number of Docker-related posts.

Using Docker to run Jenkins

To use Docker to launch a Jenkins instance, you must first get the image from a repository. The Jenkins image is obtained from Docker Hub in this case. Before creating a container, you may choose to download the picture first. Alternatively, you may launch a container and let Docker download the image as needed.

Using the docker run command, the line below will start a Jenkins container. The —name [NAME] option is used to give the Docker container a name. Docker gives a random name to the container if the container name is not supplied.

The -p [host-port:container-port] option allows your host computer to see the port or ports within the container. The ports 8080 and 50000 are exposed to the Docker host using the same port numbers in the code below.

Finally, jenkins/jenkins:lts indicates that the container will utilize the Jenkins repository’s jenkins:lts Docker image. If necessary, alter the name and host port numbers, but do not change the container port numbers.

Please copy and paste the code below into your terminal.

docker run —name my-jenkins-1 -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts jenkins/jenkins:lts jenkins/jenkins:lts

Docker creates a new container once you perform the command above. Take note of the first admin password presented on the console after the Jenkins container is up and running. During Jenkins’ initial setup, the admin password is required.

Jenkins is now operating in a new container.Jenkins is now operating in a new container.

Finishing Jenkins’ Initial Setup

You may now visit the Jenkins admin page once the Jenkins container has reached the operating stage. The port number is 8080, as you may recall from the command you used to construct the container. Go to HTTP://localhost:8080 to view the Jenkins instance.

The first admin password must be entered on the Unlock Jenkins page, as seen below. The first admin password should have been copied earlier. Click Continue after entering the password in the Administrator password box.

Jenkins UnlockedJenkins Unlocked

You’ll find the option to Install recommended plugins on the Customize Jenkins page. These are the plugins that the Jenkins community most often installs. Choose the Select plugins to install option if you have a pre-selected collection of plugins to install.

For the sake of simplicity, the Install recommended plugins option is chosen in this example.

Customizing the Jenkins Plugins' InstallationInstallation of Jenkins Plugins with Customization

The plugins will be installed, and all you have to do now is wait for them to complete. As you can see in the screenshot below, fourteen (14) recommended plugins have been installed. The number of plugins that are suggested may change over time. The recommended plugins may have changed by the time you setup your Jenkins instance.

The Suggested Plugins are InstalledThe Suggested Plugins are Installed

The Create First Admin User page will appear as soon as the plugins have been installed. You’ll establish your first Jenkins administrator account on this page. Fill in all of the needed boxes, then click Save and Continue. Make a mental note of the administrator password you just established.

Creating the Jenkins administrator account for the first timeCreating the Jenkins administrator account for the first time

You may alter the Jenkins admin page URL on the Instance Configuration page. The URL value should normally be the real URL that Jenkins users would view. Leave the default setting of http://localhost:8080 and click Save and Finish for this example.

Setting up Jenkins' URLSetting up Jenkins’ URL

You’ll get a confirmation that the Jenkins setup is complete on the final setup page. Select Start using Jenkins from the drop-down menu.

Putting the Jenkins setup togetherPutting the Jenkins setup together

You’ve finished the Jenkins installation when you view the Welcome to Jenkins page, as seen below. You may begin customizing Jenkins and adding new items or projects at this time.

Jenkins' administrative interfaceJenkins’ administrative interface

In the Container, Locating Jenkins Data

You constructed a Docker container to run an instance of Jenkins in the previous step. Docker containers, like anything else on a computer, have the potential to fail. If the container goes down for whatever reason, the Jenkins data goes down with it.

The code following utilizes the docker exec command to show the contents of the /var/jenkins home directory in order to better understand the Jenkins data location. In this case, the Docker container name is my-jenkins-1.

ls -l /var/jenkins home docker exec my-jenkins-1

The output of executing the command above is shown below. The contents of the directory /var/jenkins home are listed. The settings, users, plugins, and other key information are all stored in the /var/jenkins home directory.

The contents of the Jenkins configuration directory are listed below.The contents of the Jenkins configuration directory are listed below.

Jenkins Data Backup from the Container to the Host

You’ve checked that the Jenkins data is within the container at this stage. The Jenkins data should be copied from the container to the host. After the Jenkins data has been removed from the container, you may take a backup.

It is a good thing that Docker has built-in support for copying files between the Docker container and the Docker host. Using the command docker cp <container id>:/path/in/container /path/in/host

The docker cp command is used in the example below to transfer the data from the my-jenkins-1 container to a Docker host location. It’s worth noting that the container doesn’t even have to be running for this command to work.

cp docker /var/jenkins home /jenkins backup my-jenkins-1

The command above is shown in the example below. As you can see, the command creates the target directory for you.

Jenkins Data ReplicationJenkins Data Replication

You may compress and archive the whole folder now that you’ve removed the Jenkins data from the container. When you make modifications to Jenkins, you may need to repeat the process.

Using Docker to run Jenkins with Volume Mount

You should utilize a Docker volume to keep your Jenkins data consistent and durable outside of the Docker container.

The Jenkins configuration files are placed in the Docker container at /var/jenkins home, as stated in the previous section. You may use the -v option with the docker run command to guarantee that the Jenkins files are stored outside of the container.

The following code will build a new Docker container called my-jenkins-2. The host will have access to ports 8080 and 50000. In addition, the Docker container’s /var/jenkins home directory will be mounted as a volume on the host as jenkins home.

docker run -p 8080:8080 -p 50000:50000 -v jenkins home:/var/jenkins home —name my-jenkins-2 jenkins/jenkins:lts

The new Docker container should be formed and in the operating state after executing the command above. The docker ps command may be used to verify if the container is operating.

a list of all currently active containersa list of all currently active containers

a list of all currently active containers

On the Docker Host, Locating Jenkins Data

So, where has Jenkins’ data folder vanished to? The mounted volume is jenkins home, according to the command you used to build the my-jenkins-2 container. Use the docker volume command to determine where the jenkins home location is located on the filesystem.

The following command displays information on the jenkins home volume on the Docker host.

jenkins home docker volume inspect

The information about the jenkins home volume obtained by executing the command above is shown in the screenshot below.

Details on how to locate the mounted volumeDetails on how to locate the mounted volume

Now that you know where to look for Jenkins data, you can double-check by looking for the mount point. The jenkins home volume mount point in this case is /var/lib/docker/volumes/jenkins home/ data.

The contents of the volume are listed below.The contents of the volume are listed below.

You can backup the directory for safekeeping now that Jenkins data is persistent outside of the container. The data will also remain on the Docker host even if the Jenkins container is deleted.

Using Docker Compose with Volumes to run Jenkins

Jenkins may also be launched using the docker-compose command. If necessary, you may use Docker Compose to deploy one or more Jenkins instances. However, you must first create a Docker Compose file for each instance.

Putting the Docker Compose Files Together

The docker-compose command, by default, reads a file called docker-compose.yml or docker-compose.yaml in the same working directory as the command.

Create a new blank file using your preferred text editor. Then copy and paste the YAML code below into your editor, and save the docker-compose.yml file.

# version of docker-compose.yml: ‘3.7’ jenkins: jenkins: jenkins: jenkins: jenkins privileged: jenkins/jenkins:lts image: jenkins/jenkins:lts genuine user: container name: my-jenkins-3 volumes: – 8083:8080 – 50003:50000 root ports: – 8083:8080 – 50003:50000 – /var/run/docker.sock:/var/run/docker.sock – /jenkins data:/var/jenkins home

The structure of a Docker Compose file is not covered in this article. To learn more about what each setting in the compose file signifies, check the official compose file reference documentation.

However, you may split down the most critical parameters in both Docker Compose files in this case, as mentioned below.

  • This is the image that will be used to create the Docker container instance.
  • This is where the port mapping between the Docker host and the container is defined.
  • The volumes mapped between the Docker host and the container are called volumes.
  • container name: This is the name you’d want to give the container you’re building. The container name in this case is my-jenkins-3.

Using Docker Compose to start the Jenkins Container

You may now use the two Docker Compose files to launch the two Jenkins Docker container instances that you’ve stored.

Use the command below to start the my-jenkins-3 container. It’s worth noting that the command expects the docker-compose.yml file is in the same directory as the command.

# Use docker-compose up -d to start the my-jenkins-3 container. docker exec my-jenkins-3 cat /var/jenkins home/secrets/initialAdminPassword # Get the initial admin password # Run docker ps on the my-jenkins-3 container to make sure it’s up and running.

As seen below, the my-jenkins-3 container then begins and enters the operating state.

Docker Compose is used to start the my-jenkins-3 Docker container.Docker Compose is used to start the my-jenkins-3 Docker container.

To access Jenkins and finish the basic setup, go to http://localhost:8083 in your browser.

The Docker container my-jenkins-3 is listening on port 8083.The Docker container my-jenkins-3 is listening on port 8083.

You can verify that the Jenkins data files are in the /jenkins data folder once you’ve finished the initial Jenkins setup for the my-jenkins-3 container.

Jenkins configuration files are stored on the Docker host.Jenkins configuration files are stored on the Docker host.

The setup will survive if the my-jenkins-3 docker container is destroyed since the Jenkins data files are stored on the Docker host. Stop the my-jenkins-3 container using the command below to verify the durability of the Jenkins configuration.

It’s worth noting that invoking the downer-compose down command deletes the container as well.

Docker Compose is used to stop a container.Docker Compose is used to stop a container.

Now, use the command below to restart the my-jenkins-3 service.

When you initially enter the Jenkins web interface, the login page will appear instead of the first setup page. You’ll see that your Jenkins settings, including your login credentials, hasn’t changed.

Jenkins' sign-in pageJenkins’ sign-in page


You learnt how to run Jenkins using Docker in a variety of methods in this post. You’ve seen how to use Docker to run Jenkins with and without a mounted volume. You’ve also figured out how to backup Jenkins data from the container to the host.

You also learnt how to use Docker Compose to run a Jenkins instance with volumes mapped to the Docker host. The Jenkins data files are then stored in the Docker host, ensuring that the Jenkins settings are preserved even if the Docker container is removed.

Using Docker and Docker Compose with volumes gives you the flexibility to back up and ultimately reuse Jenkins’ data. Which method of Using Docker to run Jenkins do you think you would use more?

Additional Reading

The “docker-compose jenkins plugin” is a Jenkins plugin that allows you to run the Jenkins application in Docker. This article will show you how to set up and use this plugin with Docker Compose.

Related Tags

  • docker-compose.yml jenkins example
  • docker-compose in jenkins pipeline
  • install docker-compose in jenkins container
  • jenkins docker in docker
  • jenkins docker-compose github