# How to install Jenkins using Docker container in Ubuntu?

### Introduction

In this article, i will guide how to install Jenkins in Ubuntu using a Docker container.

### Prerequisites

Docker installed

### Create a bridge network

Open the terminal and create a bridge network using the following command

```bash
sudo docker network create jenkins
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679311260205/ecf0306d-c66d-4244-9601-de4023b9146f.png align="center")

### Run Docker image container

To execute Docker commands inside Jenkins nodes, download and run the docker:dind Docker image using the following docker run command

```bash
sudo docker run --name jenkins-docker --detach \
  --privileged --network jenkins --network-alias docker \
  --env DOCKER_TLS_CERTDIR=/certs \
  --volume jenkins-docker-certs:/certs/client \
  --volume jenkins-data:/var/jenkins_home \
  --publish 2376:2376 \
  docker:dind --storage-driver overlay2
```

* (--name) Specifies the Docker container name to use for running the image.
    
* (--detach) Runs the Docker container in the background.
    
* (--privileged) Running Docker in Docker currently requires privileged access to function properly.
    
* (--network jenkins) The network created in the earlier step.
    
* (--network-alias docker) Makes the Docker in Docker container available as the hostname `docker` within the `jenkins` network.
    
* (--env DOCKER\_TLS\_CERTDIR=/certs) Enables the use of TLS in the Docker server.
    
* (--volume jenkins-docker-certs:/certs/client) Maps the `/certs/client` directory inside the container to a Docker volume named `jenkins-docker-certs` as created above.
    
* (--volume jenkins-data:/var/jenkins\_home) Maps the `/var/jenkins_home` directory inside the container to the directory named `jenkins-data`. This will allow for other Docker containers controlled by this Docker container’s Docker daemon to mount data from Jenkins.
    
* (--publish 2376:2376) Exposes the Docker daemon port on the host machine.
    
* The `docker:dind` image itself.
    
* (--storage-driver overlay2)The storage driver for the Docker volume.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679395968455/949cb4fb-e0cb-4f8d-9df0-7d5b8a323fc4.png align="center")

### Customise official Jenkins Docker image

Open command terminal and create one directory then create Dockerfile in it with the following content

```plaintext
FROM jenkins/jenkins:2.375.1
USER root
RUN apt-get update && apt-get install -y lsb-release
RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \
  https://download.docker.com/linux/debian/gpg
RUN echo "deb [arch=$(dpkg --print-architecture) \
  signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \
  https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list
RUN apt-get update && apt-get install -y docker-ce-cli
USER jenkins
RUN jenkins-plugin-cli --plugins "blueocean:1.26.0 docker-workflow:563.vd5d2e5c4007f"
```

Build a new docker image from this Dockerfile and assign the image name, e.g. "jenkins-blueocean:2.375.1-1" with the following command

```bash
sudo docker build -t myjenkins-blueocean:2.375.1-1 .
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679396732847/4d7b21c2-0ea4-464a-bc91-1815fee95415.png align="center")

It will take some time to build a Docker image. On successful build, you will get the following result in terminal

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679397183840/d8e15bcf-1528-43af-864e-7653303b6f9d.png align="center")

Confirm the image is built with the following command.

```bash
sudo docker images
```

You will see a list of Docker images in terminal

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679397852201/71daae77-8f14-4e53-98b8-5be9af010c9d.png align="center")

### Run customised docker image

Run `jenkins-blueocean:2.375.1-1` image as a container in Docker using the following [`docker run`](https://docs.docker.com/engine/reference/run/) command

```bash
sudo docker run --name jenkins-blueocean --restart=on-failure \
  --detach \
  --network jenkins --env DOCKER_HOST=tcp://docker:2376 \
  --env DOCKER_CERT_PATH=/certs/client --env DOCKER_TLS_VERIFY=1 \
  --publish 8000:8080 --publish 50000:50000 \
  --volume jenkins-data:/var/jenkins_home \
  --volume jenkins-docker-certs:/certs/client:ro \
  jenkins-blueocean:2.375.1-1
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679398650027/4a30baaa-799e-4ea8-9cde-495efd4cd10d.png align="center")

* (--name jenkins-blueocean) the Docker container name for this instance of the Docker image.
    
* (--restart=on-failure) Always restart the container if it stops due to some error.
    
* (--detach) Runs the current container in the background and outputs the container ID.
    
* (--network jenkins) Connects this container to the `jenkins` network defined in the earlier step. This makes the Docker daemon from the previous step available to this Jenkins container through the hostname `docker`.
    
* (--env DOCKER\_HOST=tcp://docker:2376) Specifies the environment variables used to connect to the Docker daemon from the previous step.
    
* (--publish 8000:8080) Maps (i.e. "publishes") port 8080 of the current container to port 8000 on the host machine. The first number represents the port on the host while the last represents the container’s port. Therefore, you would be accessing Jenkins on your host machine through port 8000.
    
* (--volume jenkins-data:/var/jenkins\_home) Maps the `/var/jenkins_home` directory inside the container to the directory named `jenkins-data` of the host machine.
    
* (--volume jenkins-docker-certs:/certs/client:ro) Certificates needed to connect to the Docker daemon available in the path specified by the `DOCKER_CERT_PATH` environment variable.
    
* The name of the Docker image, which you built in the previous step.
    

The above run command returns container ID.

Confirm container is running with following command

```bash
sudo docker ps
```

The above command should output on the screen following the result

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680694304660/6073653e-8914-4f1a-b674-29c71859210a.png align="center")

### Access Jenkins

Browse to http://&lt;host\_machine\_ip&gt;:8000 (port configured for jenkins). You will see **Unlock Jenkins** page appears.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680694704483/ff796a69-d7eb-4927-9c93-49a24e68c236.jpeg align="center")

To get the password for unlocking Jenkins open the container logs in the console with the following command

```bash
sudo docker logs jenkins-blueocean
```

In the console output you will get an alphanumeric password as follows

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1680695094284/e44a2b1e-1aa7-414e-a413-0ecc337d10ee.png align="center")

On the Unlock Jenkins page, paste this password into the password field and click continue

After unlocking Jenkins, the **Customize Jenkins** page appears. Here you can install useful plugins as part of your initial setup.

If you are not sure whats plugins you need, choose **Install Suggested Plugins.** You can install plugins later also.

After jenkins plugin section, Jenkins asks you to create your first administrator user.

Create admin user and start using Jenkins when **Jenkins is ready** page appears.

### Conclusion

With simple use of Docker we can install Jenkins in Ubuntu system very easly.
