流水线可以让项目发布流程更加清晰,docker可以大大减少Jenkins配置。
使用docker安装jenkins
DockerHub搜索jenkins
数据卷挂载到 /var 磁盘目录下,因为该磁盘空间较大
# DEPRECATED; use "jenkins/jenkins:lts" instead
docker pull jenkins/jenkins
# 启动
docker run -u root -itd --name jenkins -p 8082:8080 -p 50000:50000 -v $(which docker):/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock -e TZ="Asia/Shanghai" -v /etc/localtime:/etc/localtime:ro -v /var/volumes4docker/jenkins/jenkins_home:/var/jenkins_home jenkins/jenkins
# -p 8082:8080 Jenkins默认网页访问端口为8080,将端口映射到外部主机,
# 阿里云服务器要配置防火墙和安全组
-p 8082:8080 \ # 映射主机端口(第一个数字8082)和容器端口(后一个数字8080),用于HTTP访问
-p 50000:50000 \ # 映射主机端口(第一个数字50000)和容器端口(后一个数字50000),用于连接Agent
# -v $(which docker):/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock
# 使Jenkins内部可以使用docker命令
# -e TZ="Asia/Shanghai" -v /etc/localtime:/etc/localtime:ro
# 配置Jenkins容器的时区
# -v /var/volumes4docker/jenkins/jenkins_home:/var/jenkins_home
# 具名挂载 卷名:容器内路径 将Jenkins的配置映射到外部主机卷,容器删除仍可保留配置
# 挂载目录 /var/volumes4docker/jenkins/jenkins_home 下
Running from: /usr/share/jenkins/jenkins.war 表示jenkins启动成功
查看容器信息
# docker inspect containerID
[
{
"Id": "dd61611e0a08c42e8c74d6caf6f08a43ee576acae723b4242d4cbf7ada0cec0f",
"Created": "2021-10-07T08:19:50.973194204Z",
"Path": "/sbin/tini",
"Args": [
"--",
"/usr/local/bin/jenkins.sh"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 15916,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-10-07T08:31:26.25836015Z",
"FinishedAt": "2021-10-07T08:30:49.508319144Z"
},
"Image": "sha256:d1be8383ce8b4976456a8408d7c5263b1ff36d887216131eeb94e3e6c8c8eeb3",
"ResolvConfPath": "/var/lib/docker/containers/dd61611e0a08c42e8c74d6caf6f08a43ee576acae723b4242d4cbf7ada0cec0f/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/dd61611e0a08c42e8c74d6caf6f08a43ee576acae723b4242d4cbf7ada0cec0f/hostname",
"HostsPath": "/var/lib/docker/containers/dd61611e0a08c42e8c74d6caf6f08a43ee576acae723b4242d4cbf7ada0cec0f/hosts",
"LogPath": "/var/lib/docker/containers/dd61611e0a08c42e8c74d6caf6f08a43ee576acae723b4242d4cbf7ada0cec0f/dd61611e0a08c42e8c74d6caf6f08a43ee576acae723b4242d4cbf7ada0cec0f-json.log",
"Name": "/jenkins",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/usr/bin/docker:/usr/bin/docker",
"/var/run/docker.sock:/var/run/docker.sock",
"/etc/localtime:/etc/localtime:ro",
"/var/volumes4docker/jenkins/jenkins_home:/var/jenkins_home"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"50000/tcp": [
{
"HostIp": "",
"HostPort": "50000"
}
],
"8080/tcp": [
{
"HostIp": "",
"HostPort": "8082"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/0293c2371b66db4028a138952309d3e9c8d67477a9fdbf34d2e8523d14a90b63-init/diff:/var/lib/docker/overlay2/594c79d6b867335e0d3cba6beff9068a2881e70aedebfbdbe765cf681846d402/diff:/var/lib/docker/overlay2/ababb979acfee1b0fb4ebc078e41849b3998866e89334a9c1981b8b14c26ec6d/diff:/var/lib/docker/overlay2/d5c9b846126480c1bc5bb329c15cc81796e7c4ded5d0d07b61d2d56da5ede4ee/diff:/var/lib/docker/overlay2/7ecf232ddc190042dbaebb0eec97b8b7677679e5109296ba4258fcbfaf602c40/diff:/var/lib/docker/overlay2/d880a90f682016a5c1e65ad409327da5554dff317632d2d01a61c15f6abe86db/diff:/var/lib/docker/overlay2/6635d79e7c57657d5200cc4eec9c84e40488f3013415b14093853ba87d998231/diff:/var/lib/docker/overlay2/dbad0d02b0641f5ceb126d41ed67f5cea5c4e668d47bddeeecf77962905cb290/diff:/var/lib/docker/overlay2/70f1d11b7fca6f970339fa0fc1a08dd15a0f92b23cda2a9e4b1e241a75987577/diff:/var/lib/docker/overlay2/c76ffd9dac15f7f77bf1fa9ede92ead04888647bba180b361a60bec7c04f4bf1/diff:/var/lib/docker/overlay2/ee8b58c94ef4699bb14fcd5c1d9dd6766d0b850fb7dc8df1eed6acfa2fc7a096/diff:/var/lib/docker/overlay2/da638011364fa24f3b17b994ba85530fc96d850906ffbd4287d2b021d57f5f51/diff:/var/lib/docker/overlay2/162028340c25fb6568da119a4e08e0da1b4f5bd6b47e0ed775c2e5c2db85ffda/diff:/var/lib/docker/overlay2/7193bfcdaadc04c8dd578459185e5a517484c77973ee99ab0571e71d84323ea5/diff:/var/lib/docker/overlay2/6310d645f34aa9dcf56271518b23e448ba53737226503ba104b6df763bd07ff7/diff:/var/lib/docker/overlay2/ce15fc78d857ac74372f6b15b1b65d8c5a70bae58555c16ff43a6928a110330f/diff:/var/lib/docker/overlay2/5122a05d3e3d5141403d70b3d8360962f08518c91a3d9013e413b1b3ca98ff86/diff:/var/lib/docker/overlay2/649d241945635331ec166baa9afb4dcbed387f7a3c01ae909de2f4d2b28e0698/diff",
"MergedDir": "/var/lib/docker/overlay2/0293c2371b66db4028a138952309d3e9c8d67477a9fdbf34d2e8523d14a90b63/merged",
"UpperDir": "/var/lib/docker/overlay2/0293c2371b66db4028a138952309d3e9c8d67477a9fdbf34d2e8523d14a90b63/diff",
"WorkDir": "/var/lib/docker/overlay2/0293c2371b66db4028a138952309d3e9c8d67477a9fdbf34d2e8523d14a90b63/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/usr/bin/docker",
"Destination": "/usr/bin/docker",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/var/run/docker.sock",
"Destination": "/var/run/docker.sock",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/etc/localtime",
"Destination": "/etc/localtime",
"Mode": "ro",
"RW": false,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/var/volumes4docker/jenkins/jenkins_home",
"Destination": "/var/jenkins_home",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "dd61611e0a08",
"Domainname": "",
"User": "root",
"AttachStdin": false,
"AttachStdout": true,
"AttachStderr": true,
"ExposedPorts": {
"50000/tcp": {},
"8080/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"TZ=Asia/Shanghai",
"PATH=/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"LANG=C.UTF-8",
"JENKINS_HOME=/var/jenkins_home",
"JENKINS_SLAVE_AGENT_PORT=50000",
"REF=/usr/share/jenkins/ref",
"JENKINS_VERSION=2.315",
"JENKINS_UC=https://updates.jenkins.io",
"JENKINS_UC_EXPERIMENTAL=https://updates.jenkins.io/experimental",
"JENKINS_INCREMENTALS_REPO_MIRROR=https://repo.jenkins-ci.org/incrementals",
"COPY_REFERENCE_FILE_LOG=/var/jenkins_home/copy_reference_file.log",
"JAVA_HOME=/opt/java/openjdk"
],
"Cmd": null,
"Image": "jenkins/jenkins",
"Volumes": {
"/var/jenkins_home": {}
},
"WorkingDir": "",
"Entrypoint": [
"/sbin/tini",
"--",
"/usr/local/bin/jenkins.sh"
],
"OnBuild": null,
"Labels": {
"org.opencontainers.image.description": "The Jenkins Continuous Integration and Delivery server",
"org.opencontainers.image.licenses": "MIT",
"org.opencontainers.image.source": "https://github.com/jenkinsci/docker",
"org.opencontainers.image.title": "Official Jenkins Docker image",
"org.opencontainers.image.url": "https://www.jenkins.io/",
"org.opencontainers.image.vendor": "Jenkins project",
"org.opencontainers.image.version": "2.315"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "1cd79de9c2420e53d5881242b400b174628a31e3693ca613f91ac436e69ef69b",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"50000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "50000"
}
],
"8080/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8082"
}
]
},
"SandboxKey": "/var/run/docker/netns/1cd79de9c242",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "2677c1159f6b61d7f7b9a7b1bd31f53fe059792409e5ad9cb9203c9d49ddcf08",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "8d10d0e8db5eaee9a179ad0c38340b01c68ac5135f648e9149aac24ac4113ccb",
"EndpointID": "2677c1159f6b61d7f7b9a7b1bd31f53fe059792409e5ad9cb9203c9d49ddcf08",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
宿主机访问jenkins控制台日志
docker logs -f jenkins
# -f 查看实时日志
宿主机访问jenkins主目录
# 容器内/var/jenkins_home挂载目录
/var/volumes4docker/jenkins/jenkins_home
启动问题及处理
curl localhost:8082
Authentication required
防火墙要开放50000 和8082 端口,同时也要配置阿里云安全组规则。