Skip to content

Commit e409a19

Browse files
Jaime Salas ZancadaJaime Salas Zancada
authored andcommitted
added custom Jenkins Dockerfile
1 parent 058936d commit e409a19

File tree

3 files changed

+223
-0
lines changed

3 files changed

+223
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
FROM jenkinsci/blueocean:latest
2+
3+
USER root
4+
5+
# insatll node
6+
RUN apk add --update nodejs npm
7+
8+
# compose deps
9+
RUN apk add --no-cache \
10+
gcc \
11+
libc-dev \
12+
libffi-dev \
13+
make \
14+
openssl-dev \
15+
python3-dev \
16+
py-pip
17+
18+
# install docker-compose via python
19+
RUN pip install docker-compose
20+
21+
# .NET Core deps
22+
RUN apk add --no-cache \
23+
ca-certificates \
24+
icu-libs \
25+
krb5-libs \
26+
libgcc \
27+
libintl \
28+
libssl1.1 \
29+
libstdc++ \
30+
zlib
31+
32+
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT="true" \
33+
PATH="${PATH}:/root/.dotnet"
34+
35+
# .NET Core SDK
36+
# see https://github.com/dotnet/dotnet-docker/blob/master/3.1/sdk/alpine3.11/amd64/Dockerfile
37+
RUN dotnet_sdk_version=3.1.201 \
38+
&& wget -O dotnet.tar.gz https://dotnetcli.azureedge.net/dotnet/Sdk/$dotnet_sdk_version/dotnet-sdk-$dotnet_sdk_version-linux-musl-x64.tar.gz \
39+
&& dotnet_sha512='9a8f14be881cacb29452300f39ee66f24e253e2df947f388ad2157114cd3f44eeeb88fae4e3dd1f9687ce47f27d43f2805f9f54694b8523dc9f998b59ae79996' \
40+
&& echo "$dotnet_sha512 dotnet.tar.gz" | sha512sum -c - \
41+
&& mkdir -p /usr/share/dotnet \
42+
&& tar -C /usr/share/dotnet -oxzf dotnet.tar.gz \
43+
&& ln -s /usr/share/dotnet/dotnet /usr/bin/dotnet \
44+
&& rm dotnet.tar.gz \
45+
# Trigger first run experience by running arbitrary cmd
46+
&& dotnet help
47+
48+
USER jenkins
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
## Downloading and running Jenkins in Docker
2+
3+
### 1. Create a network for Jenkis
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+
--ntework-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+
--ntework-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 conatiner name
51+
2. Removes the Docker conatiner instance when it is shut down.
52+
3. Runs the Docker container in the background.
53+
4. Running Docker in Docker currently requires provileged 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+
* Annotation free version
63+
64+
```bash
65+
docker container run --name jenkins-docker --rm --detach \
66+
--privileged --network jenkins --network-alias docker \
67+
--env DOCKER_TLS_CERTDIR=/certs \
68+
--volume jenkins-docker-certs:/certs/client \
69+
--volume jenkins-data:/var/jenkins_home \
70+
--publish 2376:2376 docker:dind
71+
```
72+
73+
### 4. Run jenkins as a container
74+
75+
```bash
76+
docker container run \
77+
--name jenkins-blueocean \
78+
--rm \
79+
--detach \
80+
--network jenkins \
81+
--env DOCKER_HOST=tcp://docker:2376 \
82+
--env DOCKER_CERT_PATH=/certs/client \
83+
--env DOCKER_TLS_VERIFY=1 \
84+
--publish 8080:8080 \
85+
--publish 50000:50000 \
86+
--volume jenkins-data:/var/jenkins_home \
87+
--volume jenkins-docker-certs:/certs/client:ro \
88+
jenkinsci/blueocean
89+
```
90+
91+
#### Commands explantion
92+
93+
```
94+
docker container run \
95+
--name jenkins-blueocean \ # 1
96+
--rm \ # 2
97+
--detach \ # 3
98+
--network jenkins \ # 4
99+
--env DOCKER_HOST=tcp://docker:2376 \ # 5
100+
--env DOCKER_CERT_PATH=/certs/client \
101+
--env DOCKER_TLS_VERIFY=1 \
102+
--publish 8080:8080 \ # 6
103+
--publish 50000:50000 \ # 7
104+
--volume jenkins-data:/var/jenkins_home \ # 8
105+
--volume jenkins-docker-certs:/certs/client:ro \ # 9
106+
jenkinsci/blueocean # 10
107+
```
108+
109+
1. Specifies the Docker container name for this instance of the _jenkinsci/blueocean_ Docker image.
110+
111+
2. Removes the Docker conatiner instance when it is shut down.
112+
113+
3. Runs the Docker container in the background.
114+
115+
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.
116+
117+
5. Specifies the environment variables used by `docker`, `docker-compose`, and other Docker tools to connect to the Docker daemon from the previous step.
118+
119+
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.
120+
121+
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.
122+
123+
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
124+
*--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.
125+
126+
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.
127+
128+
10. The _jenkinsci/blueocean_ Docker image itself.
129+
130+
* Annotation-free version
131+
132+
```bash
133+
docker container run --name jenkins-blueocean --rm --detach \
134+
--network jenkins --env DOCKER_HOST=tcp://docker:2376 \
135+
--env DOCKER_CERT_PATH=/certs/client --env DOCKER_TLS_VERIFY=1 \
136+
--volume jenkins-data:/var/jenkins_home \
137+
--volume jenkins-docker-certs:/certs/client:ro \
138+
--publish 8080:8080 --publish 50000:50000 jenkinsci/blueocean
139+
```
140+
141+
## Starting Jenkins
142+
143+
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
144+
145+
```bash
146+
$ docker container exec -it jenkins-blueocean bash
147+
```
148+
149+
```bash
150+
$ ls
151+
bin certs dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
152+
$ cat /var/jenkins_home/secrets/initialAdminPassword
153+
8b0ee7a1a0214fe0a3029b8232c56087
154+
```
155+
156+
Now install the suggested plugins and wait until Jenkins finishes
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
IMAGE=$1
2+
CERTS_VOLUME=$2
3+
DATA_VOLUME=$3
4+
5+
# StartDocker in Docker into jenkins network
6+
docker container run --name jenkins-docker --rm --detach \
7+
--privileged --network jenkins --network-alias docker \
8+
--env DOCKER_TLS_CERTDIR=/certs \
9+
--volume "$CERTS_VOLUME":/certs/client \
10+
--volume "$DATA_VOLUME":/var/jenkins_home \
11+
--publish 2376:2376 docker:dind
12+
13+
# Start Jenkins in the same network
14+
docker container run --name jenkins-blueocean --rm --detach \
15+
--network jenkins --env DOCKER_HOST=tcp://docker:2376 \
16+
--env DOCKER_CERT_PATH=/certs/client --env DOCKER_TLS_VERIFY=1 \
17+
--volume "$DATA_VOLUME":/var/jenkins_home \
18+
--volume "$CERTS_VOLUME":/certs/client:ro \
19+
--publish 8080:8080 --publish 50000:50000 "$IMAGE"

0 commit comments

Comments
 (0)