forked from PacktPublishing/Docker-Quick-Start-Guide
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchapter08-source.txt
398 lines (334 loc) · 12.2 KB
/
chapter08-source.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# If Java has not yet been installed, install it now
sudo apt install openjdk-8-jre-headless
# Install Jenkins on an Ubuntu system
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins
// Our hello world pipeline script, named "hello-test"
node {
stage('Say Hello') {
echo 'Hello Docker Quick Start Guide Readers!'
}
}
// Our Docker hello world pipeline script, named "hello-docker-test"
node {
stage('Hello via Alpine') {
docker.image('alpine:latest').inside {
sh 'echo Hello DQS Readers - from inside an alpine container!'
}
}
}
# Add the jenkins user to the docker group
sudo usermod -aG docker jenkins
# Then restart the jenkins service
sudo service jenkins restart
# Setup volume location to store Jenkins configuration
mkdir $HOME/jenkins_home
chown 1000 $HOME/jenkins_home
# Deploy a Jenkins server that is configured to build Docker images
docker container run -d -p 8080:8080 -p 50000:50000 \
-v $HOME/jenkins_home:/var/jenkins_home \
--name jenkins --rm jenkins/jenkins:lts
# Setup volume location to store Jenkins configuration
mkdir $HOME/jenkins_home
chown 1000 $HOME/jenkins_home
# Deploy a Jenkins server that is configured to build Docker images
docker container run -d -p 8080:8080 -p 50000:50000 \
-v $HOME/jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
--name jenkins --rm h1kkan/jenkins-docker:lts
# Start the Docker service in the Jenkins docker container
docker container exec -it -u root jenkins service docker start
# Setup volume location to store Jenkins configuration
mkdir $HOME/jenkins_home
chown 1000 $HOME/jenkins_home
# jenkins-stack.yml
version: "3"
services:
jenkins:
image: h1kkan/jenkins-docker:lts
ports:
- 8080:8080
- 50000:50000
volumes:
- $HOME/jenkins_home:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
deploy:
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints: [node.role == manager]
registry:
image: registry
ports:
- 5000:5000
deploy:
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints: [node.role == manager]
# Deploy our Jenkins application via a Docker stack
docker stack deploy -c jenkins-stack.yml jenkins
# Make a new folder to use for the build context of your new Docker image, and cd into it
mkdir jenkins-agent
cd jenkins-agent
# jenkins-agent Dockerfile
FROM h1kkan/jenkins-docker:lts-alpine
USER root
ARG user=jenkins
ENV HOME /home/${user}
ARG VERSION=3.26
ARG AGENT_WORKDIR=/home/${user}/agent
RUN apk add --update --no-cache curl bash git openssh-client openssl procps \
&& curl --create-dirs -sSLo /usr/share/jenkins/slave.jar https://repo.jenkins-ci.org/public/org/jenkins-ci/main/remoting/${VERSION}/remoting-${VERSION}.jar \
&& chmod 755 /usr/share/jenkins \
&& chmod 644 /usr/share/jenkins/slave.jar \
&& apk del curl
ENV AGENT_WORKDIR=${AGENT_WORKDIR}
RUN mkdir -p /home/${user}/.jenkins && mkdir -p ${AGENT_WORKDIR}
USER ${user}
VOLUME /home/${user}/.jenkins
VOLUME ${AGENT_WORKDIR}
WORKDIR /home/${user}
# Build our new Jenkins agent image
docker image build -t jenkins-agent:latest .
# Tag the image with our swarm service registry
docker image tag jenkins-agent:latest ubuntu-node01:5000/jenkins-agent:latest
# Push the Jenkins agent image to the registry
docker image push ubuntu-node01:5000/jenkins-agent:latest
# if you get an error like:
#The push refers to repository [ubuntu-node00:5000/jenkins-agent]
#Get https://ubuntu-node00:5000/v2/: http: server gave HTTP response to HTTPS client
sudo vi /etc/docker/daemon.json
# add this line
{ "insecure-registries":["ubuntu-node00:5000"] }
# restart docker
service docker restart
# verify docker is up
docker version
# Retry the to push the Jenkins agent image to the registry
docker image push ubuntu-node00:5000/jenkins-agent:latest
# Here are all of the custom (or non-default) values entered to configure the Docker "Cloud" in our example:
Field Name Value Used
Docker Host URI unix:///var/run/docker.sock
Docker API Version 1.38 (match the version shown in connection test)
Docker Cloud Enabled Checked
Container Cap 10
Docker Agent Enabled Checked
Docker Agent Template Name agent
Docker Image ubuntu-node01:5000/jenkins-agent:latest
Instance Capacity 1
Remote File System Root /home/jenkins/agent
Usage Use this node as much as possible
Connection Method Attach Docker container
User root
Pull Strategy Pull once and update latest
Docker Command java -jar /usr/share/jenkins/slave.jar
Volumes /var/run/docker.sock:/var/run/docker.sock
Allocate a pseudo-TTY Checked
Here is the contents of the file: Dockerfile
FROM node:10-alpine
COPY . .
RUN npm install
EXPOSE 8000
CMD npm start
Here is the contents of the file: Jenkinsfile
node {
def app
stage('Clone repository') {
/* Clone the repository to our workspace */
checkout scm
}
stage('Build image') {
/* Builds the image; synonymous to docker image build on the command line */
/* Use a registry name if pushing into docker hub or your company registry, like this */
/* app = docker.build("earlwaud/jenkins-example-app") */
app = docker.build("jenkins-example-app")
}
stage('Test image') {
/* Execute the defined tests */
app.inside {
sh 'npm test'
}
}
stage('Push image') {
/* Now, push the image into the registry */
/* This would probably be docker hub or your company registry, like this */
/* docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-credentials') */
/* For this example, We are using our jenkins-stack service registry */
docker.withRegistry('https://ubuntu-node01:5000') {
app.push("latest")
}
}
}
Here is the contents of the file: main.js
// load the http module
var http = require('http');
// configure our HTTP server
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello Docker Quick Start\n");
});
// listen on localhost:8000
server.listen(8000);
console.log("Server listening at http://127.0.0.1:8000/");
Here is the contents of the file: package.json
{
"name": "dqs-example-app",
"version": "1.0.0",
"description": "A Docker Quick Start Example HTTP server",
"main": "main.js",
"scripts": {
"test": "node test.js",
"start": "node main.js"
},
"repository": {
"type": "git",
"url": "https://github.com/earlwaud/dqs-example-app/"
},
"keywords": [
"node",
"docker",
"dockerfile",
"jenkinsfile"
],
"author": "[email protected]",
"license": "ISC",
"devDependencies": { "test": ">=0.6.0" }
}
And finally, here is the contents of the file: test.js
var assert = require('assert')
function test() {
assert.equal(1 + 1, 2);
}
if (module == require.main) require('test').run(test);
# Initial commit of our application files to the new repo
git add Dockerfile Jenkinsfile main.js package.json test.js
git commit -m "Initial commit"
git push origin master
#Here is the full console log output for reference:
Started by an SCM change
Started by user Earl Waud
Obtained Jenkinsfile from git https://github.com/EarlWaud/dqs-example-app.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Still waiting to schedule task
‘agent-00042y2g983xq’ is offline
Running on agent-00042y2g983xq on docker in /home/jenkins/agent/workspace/dqs-example-app
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Clone repository)
[Pipeline] checkout
Cloning the remote Git repository
Cloning repository https://github.com/EarlWaud/dqs-example-app.git
> git init /home/jenkins/agent/workspace/dqs-example-app # timeout=10
Fetching upstream changes from https://github.com/EarlWaud/dqs-example-app.git
> git --version # timeout=10
> git fetch --tags --progress https://github.com/EarlWaud/dqs-example-app.git +refs/heads/*:refs/remotes/origin/*
> git config remote.origin.url https://github.com/EarlWaud/dqs-example-app.git # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url https://github.com/EarlWaud/dqs-example-app.git # timeout=10
Fetching upstream changes from https://github.com/EarlWaud/dqs-example-app.git
> git fetch --tags --progress https://github.com/EarlWaud/dqs-example-app.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 0a0f3eae4b9b1fdd73c8125bf19e1756ff298d1f (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 0a0f3eae4b9b1fdd73c8125bf19e1756ff298d1f
Commit message: "Initial commit"
First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build image)
[Pipeline] sh
[dqs-example-app] Running shell script
+ docker build -t jenkins-example-app .
Sending build context to Docker daemon 68.1kB
Step 1/5 : FROM node:10-alpine
---> e35872f034fd
Step 2/5 : COPY . .
---> 48f0f4fd042c
Step 3/5 : RUN npm install
---> Running in 34fe22900383
[91mnpm notice created a lockfile as package-lock.json. You should commit this file.
[0madded 2 packages from 7 contributors and audited 2 packages in 2.353s
found 0 vulnerabilities
Removing intermediate container 34fe22900383
---> 3d5a079a29bc
Step 4/5 : EXPOSE 8000
---> Running in aa0603f85fd9
Removing intermediate container aa0603f85fd9
---> bd7a70301f3d
Step 5/5 : CMD npm start
---> Running in 188d624ab5f2
Removing intermediate container 188d624ab5f2
---> b228cd7c0013
Successfully built b228cd7c0013
Successfully tagged jenkins-example-app:latest
[Pipeline] dockerFingerprintFrom
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test image)
[Pipeline] sh
[dqs-example-app] Running shell script
+ docker inspect -f . jenkins-example-app
.
[Pipeline] withDockerContainer
agent-00042y2g983xq on docker seems to be running inside container b45aa7df45c4d030a2a9449ec7942a5bd69432ce7065dfd753f6243af4230c5f
$ docker run -t -d -u 0:0 -w /home/jenkins/agent/workspace/dqs-example-app --volumes-from b45aa7df45c4d030a2a9449ec7942a5bd69432ce7065dfd753f6243af4230c5f -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** jenkins-example-app cat
$ docker top 8b60885146d8f2c9d0ece014288bcee81621a1c9cd516ef1ef46eb45296549e3 -eo pid,comm
[Pipeline] {
[Pipeline] sh
[dqs-example-app] Running shell script
+ npm test
> [email protected] test /home/jenkins/agent/workspace/dqs-example-app
> node test.js
Running all tests:
[32m✓ passed[39m
Passed:1 Failed:0 Errors:0
[Pipeline] }
$ docker stop --time=1 8b60885146d8f2c9d0ece014288bcee81621a1c9cd516ef1ef46eb45296549e3
$ docker rm -f 8b60885146d8f2c9d0ece014288bcee81621a1c9cd516ef1ef46eb45296549e3
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Push image)
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withDockerRegistry
[Pipeline] {
[Pipeline] sh
[dqs-example-app] Running shell script
+ docker tag jenkins-example-app ubuntu-node01:5000/jenkins-example-app:latest
[Pipeline] sh
[dqs-example-app] Running shell script
+ docker push ubuntu-node01:5000/jenkins-example-app:latest
The push refers to repository [ubuntu-node01:5000/jenkins-example-app]
cc986e676572: Preparing
9838bda55e16: Preparing
f87e587403a6: Preparing
819d7ee3ee24: Preparing
df64d3292fd6: Preparing
df64d3292fd6: Mounted from jenkins-agent
9838bda55e16: Pushed
cc986e676572: Pushed
f87e587403a6: Pushed
819d7ee3ee24: Pushed
latest: digest: sha256:2ab2fd2016610c451ebc68741c9a5d8f0d546e05f4c966cf5136fd581660a90e size: 1370
[Pipeline] }
[Pipeline] // withDockerRegistry
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
All that is left to do now is celebrate our success!