|
| 1 | +## Downloading and running Jenkins in Docker |
| 2 | + |
| 3 | +### 1. Create a network for Jenkins |
| 4 | + |
| 5 | +```bash |
| 6 | +$ docker network create jenkins |
| 7 | +``` |
| 8 | + |
| 9 | +### 2. Create the following volumes to share the Docker client TLS certificates needed to connect to the Docker daemon and persist the Jenkins data |
| 10 | + |
| 11 | +```bash |
| 12 | +$ docker volume create jenkins-docker-certs |
| 13 | +$ docker volume create jenkins-data |
| 14 | +``` |
| 15 | + |
| 16 | +### 3. In order to execute Docker commands inside Jenkins nodes, download and run the docker:dind Docker image |
| 17 | + |
| 18 | +```bash |
| 19 | +docker container run \ |
| 20 | + --name jenkins-docker \ |
| 21 | + --rm \ |
| 22 | + --detach \ |
| 23 | + --privileged \ |
| 24 | + --network jenkins \ |
| 25 | + --network-alias docker \ |
| 26 | + --env DOCKER_TLS_CERTDIR=/certs \ |
| 27 | + --volume jenkins-docker-certs:/certs/client \ |
| 28 | + --volume jenkins-data:/var/jenkins_home \ |
| 29 | + --publish 2376:2376 \ |
| 30 | + docker:dind |
| 31 | +``` |
| 32 | + |
| 33 | +#### Commands explanation |
| 34 | + |
| 35 | +``` |
| 36 | +docker container run \ |
| 37 | + --name jenkins-docker \ # 1 |
| 38 | + --rm \ # 2 |
| 39 | + --detach \ # 3 |
| 40 | + --privileged \ # 4 |
| 41 | + --network jenkins \ # 5 |
| 42 | + --network-alias docker \ # 6 |
| 43 | + --env DOCKER_TLS_CERTDIR=/certs \ # 7 |
| 44 | + --volume jenkins-docker-certs:/certs/client \ # 8 |
| 45 | + --volume jenkins-data:/var/jenkins_home \ # 9 |
| 46 | + --publish 2376:2376 \ # 10 |
| 47 | + docker:dind # 11 |
| 48 | +``` |
| 49 | + |
| 50 | +1. The Docker container name. |
| 51 | +2. Removes the Docker container instance when it is shut down. |
| 52 | +3. Runs the Docker container in the background. |
| 53 | +4. Running Docker in Docker currently requires privileged access to function properly. |
| 54 | +5. Join to previous created network. |
| 55 | +6. Makes the Docker in Docker container available as the hostname _docker_ within the _jenkins_ network. |
| 56 | +7. Enables the use of TLS in the Docker server. Due to the use of a privileged container, this is recommended, though it requires the use of the shared volume described below. This environment variable controls the root directory where Docker TLS certificates are managed. |
| 57 | +8. Maps the _/certs/client_ directory inside the container to a Docker volume named _jenkins-docker-certs_ as created above. |
| 58 | +9. Maps the _/var/jenkins_home_ directory inside the container to the Docker volume named _jenkins-data_ as created above. This will allow for other Docker containers controlled by this Docker container's Docker daemon to mount data from Jenkins. |
| 59 | +10. Exposes the Docker daemon port on the host machine. This is useful for executing _docker_ commands on the host machine to control this inner Docker daemon. |
| 60 | +11. The _docker:dind_ image itself. |
| 61 | + |
| 62 | +### 4. Run jenkins as a container |
| 63 | + |
| 64 | +```bash |
| 65 | +docker container run \ |
| 66 | + --name jenkins-blueocean \ |
| 67 | + --rm \ |
| 68 | + --detach \ |
| 69 | + --network jenkins \ |
| 70 | + --env DOCKER_HOST=tcp://docker:2376 \ |
| 71 | + --env DOCKER_CERT_PATH=/certs/client \ |
| 72 | + --env DOCKER_TLS_VERIFY=1 \ |
| 73 | + --publish 8080:8080 \ |
| 74 | + --publish 50000:50000 \ |
| 75 | + --volume jenkins-data:/var/jenkins_home \ |
| 76 | + --volume jenkins-docker-certs:/certs/client:ro \ |
| 77 | + jenkinsci/blueocean |
| 78 | +``` |
| 79 | + |
| 80 | +#### Commands explanation |
| 81 | + |
| 82 | +``` |
| 83 | +docker container run \ |
| 84 | + --name jenkins-blueocean \ # 1 |
| 85 | + --rm \ # 2 |
| 86 | + --detach \ # 3 |
| 87 | + --network jenkins \ # 4 |
| 88 | + --env DOCKER_HOST=tcp://docker:2376 \ # 5 |
| 89 | + --env DOCKER_CERT_PATH=/certs/client \ |
| 90 | + --env DOCKER_TLS_VERIFY=1 \ |
| 91 | + --publish 8080:8080 \ # 6 |
| 92 | + --publish 50000:50000 \ # 7 |
| 93 | + --volume jenkins-data:/var/jenkins_home \ # 8 |
| 94 | + --volume jenkins-docker-certs:/certs/client:ro \ # 9 |
| 95 | + jenkinsci/blueocean # 10 |
| 96 | +``` |
| 97 | + |
| 98 | +1. Specifies the Docker container name for this instance of the _jenkinsci/blueocean_ Docker image. |
| 99 | + |
| 100 | +2. Removes the Docker container instance when it is shut down. |
| 101 | + |
| 102 | +3. Runs the Docker container in the background. |
| 103 | + |
| 104 | +4. 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. |
| 105 | + |
| 106 | +5. Specifies the environment variables used by `docker`, `docker-compose`, and other Docker tools to connect to the Docker daemon from the previous step. |
| 107 | + |
| 108 | +6. Maps (i.e. "publishes") port 8080 of the _jenkinsci/blueocean_ container to port 8080 on the host machine. The first number represents the port on the host while the last represents the container's port. Therefore, if you specified _-p 49000:8080_ for this option, you would be accessing Jenkins on your host machine through port 49000. |
| 109 | + |
| 110 | +7. Maps port 50000 of the jenkinsci/blueocean container to port 50000 on the host machine. This is only necessary if you have set up one or more inbound Jenkins agents on other machines, which in turn interact with the _jenkinsci/blueocean_ container (the Jenkins "controller"). Inbound Jenkins agents communicate with the Jenkins controller through TCP port 50000 by default. You can change this port number on your Jenkins controller through the Configure Global Security page. If you were to change the TCP port for inbound Jenkins agents of your Jenkins controller to 51000 (for example), then you would need to re-run Jenkins (via this docker run … command) and specify this "publish" option with something like _--publish 52000:51000_, where the last value matches this changed value on the Jenkins controller and the first value is the port number on the machine hosting the Jenkins controller. Inbound Jenkins agents communicate with the Jenkins controller on that port (52000 in this example). Note that WebSocket agents in Jenkins 2.217 do not need this configuration. |
| 111 | + |
| 112 | +8. Maps the _/var/jenkins_home_ directory in the container to the Docker volume with the name _jenkins-data_. Instead of mapping the _/var/jenkins_home_ directory to a Docker volume, you could also map this directory to one on your machine's local file system. For example, specifying the option |
| 113 | + _--volume $HOME/jenkins:/var/jenkins_home_ would map the container's _/var/jenkins_home_ directory to the jenkins subdirectory within the _$HOME_ directory on your local machine, which would typically be _/Users/<your-username>/jenkins_ or _/home/<your-username>/jenkins_. Note that if you change the source volume or directory for this, the volume from the _docker:dind_ container above needs to be updated to match this. |
| 114 | + |
| 115 | +9. Maps the _/certs/client_ directory to the previously created _jenkins-docker-certs_ volume. This makes the client TLS certificates needed to connect to the Docker daemon available in the path specified by the _DOCKER_CERT_PATH_ environment variable. |
| 116 | + |
| 117 | +10. The _jenkinsci/blueocean_ Docker image itself. |
| 118 | + |
| 119 | +## Starting Jenkins |
| 120 | + |
| 121 | +To unlock Jenkins we have to paste a password, we can find the password inside the running container, run the following command `cat /var/jenkins_home/secrets/initialAdminPassword` to obtain the initial password |
| 122 | + |
| 123 | +```bash |
| 124 | +$ docker container exec jenkins-blueocean cat /var/jenkins_home/secrets/initialAdminPassword |
| 125 | +``` |
| 126 | + |
| 127 | +Now install the suggested plugins and wait until Jenkins finishes |
0 commit comments