Install Jenkins Inside Docker Container in Linux

Install Jenkins Inside Docker Container in Linux

Jenkins is CI/CD platform that allows to automate software building, testing and deploying. Jenkins is an open-source project available under the MIT license.

This tutorial explains how to install Jenkins inside a Docker container in the Linux. Commands have been tested on Ubuntu.

Prepare environment

Make sure you have installed Docker in your system. If you are using Ubuntu, installation instructions can be found in the post.

Install Jenkins

Before starting, create directory for data:

sudo mkdir -p /opt/jenkins/data

Set jenkins user (ID: 1000) as owner for newly created directory:

sudo chown -R 1000:1000 /opt/jenkins

Note: it doesn't matter that jenkins user doesn't exist on host system. The jenkins user will be created in the container.

  • Host network

Run the following command to create a container for Jenkins that uses host network:

docker run -d --name=jenkins --restart=always --network=host \
    -v /opt/jenkins/data:/var/jenkins_home \
    jenkins/jenkins
  • User-defined bridge network

User-defined bridge network can be used for listening on different port. By default, Jenkins service is listening for TCP agent connections on port 50000. Web UI is available on port 8080. Both ports can be changed with -p option.

docker network create app-net
docker run -d --name=jenkins --restart=always --network=app-net \
    -p 8081:8080 -p 8082:50000 \
    -v /opt/jenkins/data:/var/jenkins_home \
    jenkins/jenkins

Testing Jenkins

By default, a random password is generated during installation. The password can be found by running the following command:

docker exec -it jenkins cat /var/jenkins_home/secrets/initialAdminPassword

Open a web browser and go to http://<IP_ADDRESS>:8080, where <IP_ADDRESS> is the IP address of the system. The admin username and password from a file can be used to log in to the dashboard.

Jenkins Inside Docker Container in Linux

Uninstall Jenkins

To completely remove Jenkins, remove its container:

docker rm --force jenkins

Remove Jenkins image:

docker rmi jenkins/jenkins

You can also remove Jenkins data:

sudo rm -rf /opt/jenkins

If a user-defined bridge network was created, you can delete it as follows:

docker network rm app-net

Leave a Comment

Cancel reply

Your email address will not be published.