In this example we'll see how to run a single-node Jenkins system in Docker.

I found this article about the official Docker image that lead me to the jenkins/jenkins image.

Launch the first time

docker run -d -v jenkins_home:/var/jenkins_home -p 8080:8080 -p 50000:50000 --name jenkins_master --rm jenkins/jenkins:lts

  • This will run The container in the background (as a daemon) because of the -d flag.
  • It will make the /var/jenkins_home of the container to a Jenkins volume called jenkins_home. If the volume does not exist it will create it. We need the volume to have persistence between re-launches.
  • -p maps the ports
  • --name jenkins_master will call Docker container jenkins_master. You can of course use any other name, but we'll need this name later on. Internally Jenkins refers to this node as "master" regardless of the name of the container you choose here.
  • --rm tells Docker to remove the container when we stopped running it.

Once it launched we need to copy the one-time secret created by Jenkins, so type in:

docker container cp jenkins_master:/var/jenkins_home/secrets/initialAdminPassword .

(There is a separate dot at the end!)

The take the content of the file:

If your host is Linux/OSX use:

cat initialAdminPassword

If your host is Windows user:

type initialAdminPassword

It will print a string that looks like this: 2cead49b41fa41888c78d06be80fd5e7

Open this page in your browser: http://localhost:8080/, that's where Jenkins will live. Paste the string you got into the window.

Then select the Install default plugins, it will take some time to install the plugins.

It will then ask for the initial user. Give it whatever you like.

At this point you can use Jenkins and you have a single node called "master".

Try to run the Hello World pipeline

See the container running

docker ps

To stop the Jenkins server

When you are done using the Jenkins server you might want to shut down the container so it won't use your CPU.

docker container stop jenkins_master

Because we used the --rm flag this will also remove the container. However the volume where we have all the data will remain.

To see the container is gone type in

docker ps -a

List volumes

To see that the volume exists type in:

docker volume ls

Start Jenkins again

In order to start the Jenkins server again type in the same command as you typed in when you first launched it above.

You can change basically any of the options now, for example you can use a different name for the container or you can decide to map the server to another port. The only thing that must remain the same is the content of the -v flag and the name of the base image. That will ensure we use the already existing volume that has all of our installations and configurations.