启动报错
Failed to start Docker Application Container Engine.
修改/etc/sysconfig/selinux,设置selinux的值为disabled
Error starting daemon: error initializing graphdriver
Apr 3 15:31:11 Docker kernel: bio: create slab <bio-2> at 2
Apr 3 15:31:11 Docker dockerd: time="2018-04-03T15:31:11.835565555+08:00" level=info msg="devmapper: Creating filesystem xfs on device docker-253:1-34265854-base, mkfs args: [-m crc=0,finobt=0 /dev/mapper/docker-253:1-34265854-base]"
Apr 3 15:31:11 Docker dockerd: time="2018-04-03T15:31:11.836336636+08:00" level=info msg="devmapper: Error while creating filesystem xfs on device docker-253:1-34265854-base: exit status 1"
Apr 3 15:31:11 Docker dockerd: time="2018-04-03T15:31:11.836350296+08:00" level=error msg="[graphdriver] prior storage driver devicemapper failed: exit status 1"
Apr 3 15:31:11 Docker dockerd: Error starting daemon: error initializing graphdriver: exit status 1
Apr 3 15:31:11 Docker systemd: docker.service: main process exited, code=exited, status=1/FAILURE
Apr 3 15:31:11 Docker systemd: Failed to start Docker Application Container Engine
很明显了:mkfs.xfs版本太低,遂更新:
yum update xfsprogs
重启docker服务,正常!
Docker架构图
Docker安装
在线安装docker,首先需要yum工具
1. 安装yum-utils
若未安装,则会安装最新版本;若已安装,则会更新至最新版本;
ps:可以用rpm -qa yum-utils查询一下是否安装
[root@VM-24-4-centos ~]# yum install -y yum-utils
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
os | 3.6 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/5): epel/7/x86_64/group_gz | 96 kB 00:00:00
(2/5): epel/7/x86_64/updateinfo | 1.0 MB 00:00:00
(3/5): extras/7/x86_64/primary_db | 247 kB 00:00:00
(4/5): updates/7/x86_64/primary_db | 16 MB 00:00:00
(5/5): epel/7/x86_64/primary_db | 7.0 MB 00:00:00
Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
Nothing to do
[root@VM-24-4-centos ~]#
2. 设置 docker仓库地址
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@VM-24-4-centos ~]# yum-config-manager \
> --add-repo \
> http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror, langpacks
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@VM-24-4-centos ~]#
3. 安装
[root@VM-24-4-centos ~]# yum install -y docker-ce
...
Installed:
docker-ce.x86_64 3:26.1.4-1.el7
Dependency Installed:
audit-libs-python.x86_64 0:2.8.5-4.el7 checkpolicy.x86_64 0:2.5-8.el7
container-selinux.noarch 2:2.119.2-1.911c772.el7_8 containerd.io.x86_64 0:1.6.33-3.1.el7
docker-buildx-plugin.x86_64 0:0.14.1-1.el7 docker-ce-cli.x86_64 1:26.1.4-1.el7
docker-ce-rootless-extras.x86_64 0:26.1.4-1.el7 docker-compose-plugin.x86_64 0:2.27.1-1.el7
fuse-overlayfs.x86_64 0:0.7.2-6.el7_8 fuse3-libs.x86_64 0:3.6.1-4.el7
libcgroup.x86_64 0:0.41-21.el7 libsemanage-python.x86_64 0:2.5-14.el7
policycoreutils-python.x86_64 0:2.5-34.el7 python-IPy.noarch 0:0.75-6.el7
setools-libs.x86_64 0:3.3.8-4.el7 slirp4netns.x86_64 0:0.4.3-4.el7_8
[root@VM-24-4-centos ~]#
Docker常用命令
查看Docker状态:systemctl status docker
启动Docker:systemctl start docker
停止Docker:systemctl stop docker
重启Docker:systemctl restart docker
查看Docker版本:docker info(前提docker已启动)
查询仓库镜像:docker search 镜像名,可以加搜索条件,详见下文
拉取镜像:docker pull 镜像名<:镜像版本>,不知道版本时默认最新
查看本地镜像列表:docker images
查看所有容器:docker ps -a
查看正在运行的容器:
创建并启动容器:docker run 镜像名:镜像版本
启动容器:docker start 容器ID
停止容器:docker stop 容器ID
删除容器:docker rm 容器ID
以命令行模式进入该容器:docker exec -it 容器ID /bin/bash
退出容器:exit
docker search
搜索结果只显示10个:docker search busybox --limit=10
只列出官方镜像:docker search busybox --filter is-official=true
过滤自动构建的并且收藏数量大于等于3的镜像:docker search busybox --filter is-automated=true --filter stars=3
由于国内网络问题,需要配置加速器来加速。修改配置文件 /etc/docker/daemon.json
下面命令直接生成文件 daemon.json,直接在命令行执行即可
cat <<EOF > /etc/docker/daemon.json
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"http://hub-mirror.c.163.com"
],
"max-concurrent-downloads": 10,
"log-driver": "json-file",
"log-level": "warn",
"log-opts": {
"max-size": "10m",
"max-file": "3"
},
"data-root": "/var/lib/docker"
}
EOF
Nginx镜像
安装Nginx镜像
常用路径
/etc/nginx/nginx.conf
/etc/nginx/conf.d/default.conf
/usr/share/nginx/html
以手动拉取方式镜像,然后启动
docker pull nginx:latest
docker run --name nginx-test -p 8080:80 -d nginx
–name 容器名
-p 主机端口:docker容器端口(Nginx默认是80)
-d 表示后台运行
进入容器发现,Nginx服务在/etc/nginx目录下
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bbe1cd5ab68b nginx "/docker-entrypoint.…" 14 minutes ago Up 6 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx-test
[root@localhost ~]#
[root@localhost ~]# docker exec -it bbe1cd5ab68b /bin/bash
root@bbe1cd5ab68b:/# cd /etc/nginx
查看nginx.conf,文件最后使用include包含了/etc/nginx/conf.d/*.conf,在/etc/nginx/conf.d有一个default.conf,关于server{}的配置都在这里面。
root@bbe1cd5ab68b:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
root@bbe1cd5ab68b:/etc/nginx# cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
root@bbe1cd5ab68b:/etc/nginx#
默认是监听容器的80端口,监听localhost域名。
如果我们是在虚拟机centos7中启动Nginx镜像,那么这个监听localhost,在宿主机访问将访问不到Nginx服务,实际使用时会使用自己的nginx.conf,将在后面讲述。
root@bbe1cd5ab68b:/etc/nginx# cd conf.d/
root@bbe1cd5ab68b:/etc/nginx/conf.d# ls
default.conf
root@bbe1cd5ab68b:/etc/nginx/conf.d#
root@bbe1cd5ab68b:/etc/nginx/conf.d# cat default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
root@bbe1cd5ab68b:/etc/nginx/conf.d#
启动nginx镜像
以host网络方式启动,这样宿主机win10能访问虚拟机centos,虚拟机也能访问docker容器。
不指定network时,默认以桥接模式启动,这时虚拟机访问不了容器,宿主机更访问不到了,原因未知。
centos IP:192.168.3.55
容器IP:172.17.0.2
[root@localhost sbin]# docker run --network host --name nginx-test -p 80:80 -d nginx
WARNING: Published ports are discarded when using host network mode
075250a91fb813a8955d6b21e5eaa7acd9ffb8026dea0e7a723cb61636444edc
[root@localhost sbin]#
[root@localhost sbin]# docker exec -it nginx-test /bin/bash
root@localhost:/#
root@localhost:/#
root@localhost:/# curl 172.17.0.2
<!DOCTYPE html>
<html>
...
</html>
root@localhost:/# exit
exit
[root@localhost sbin]#
[root@localhost sbin]# curl 192.168.3.55
<!DOCTYPE html>
<html>
...
</html>
Jenkins镜像
通过docker search 查询到Jenkins有很多镜像,在这里我们拉取jenkins/jenkins
拉取完即可在镜像列表里看到
创建Jenkins挂载目录并授权权限,这样我们就可以很方便地对容器内的配置文件进行修改。
在docker中,挂载就是用宿主机的文件或文件夹覆盖容器内的文件或文件夹,可以实现宿主机和容器目录(文件)的双向数据自动同步。
[root@VM-24-4-centos ~]# mkdir -p /opt/jenkins_mount
[root@VM-24-4-centos ~]# chmod 777 /opt/jenkins_mount
创建并启动Jenkins容器
-d 后台运行镜像
-p 10240:8080 将镜像的8080端口映射到服务器的10240端口。
-p 10241:50000 将镜像的50000端口映射到服务器的10241端口
-v /opt/jenkins_mount:/var/jenkins_home /var/jenkins_home目录为容器jenkins工作目录,我们将硬盘上的一个目录挂载到这个位置,方便后续更新镜像后继续使用原来的工作目录。这里我们设置的就是上面我们创建的/opt/jenkins_mount目录
-v /etc/localtime:/etc/localtime 让容器使用和服务器同样的时间设置。
–name myJenkins 给容器起一个别名,启停删除可以用此别名
[root@VM-24-4-centos ~]# docker run -d -p 10240:8080 -p 10241:50000 -v /opt/jenkins_mount:/var/jenkins_home -v /etc/localtime:/etc/localtime --name myJenkins jenkins/jenkins
34627d1e8154a0ae1fdb8d098e10f2944236dea588c9a0a75b30011cd0f25fd7
[root@VM-24-4-centos ~]#
[root@VM-24-4-centos ~]# docker logs myJenkins
自动创建了admin用户,密码为92d8fbef89464104ac68a25772083a59,密码存在容器里的 /var/jenkins_home/secrets/initialAdminPassword下,因为我们挂载了目录,所以在宿主机/opt/jenkins_mount下也可以查询到。
访问
访问地址 http://ip:挂在的宿主机端口,这里是http://101.43.242.145:10240/
但是无法访问,是因为我的服务器是腾讯云服务器,对入网流量进行了控制,在控制台添加该10240的端口的放行即可。
按步骤进行,admin登录后安装推荐的插件,并添加第一个管理员zj,配置实例地址为http://101.43.242.145:10240/
全局配置
在学习Windows版的Jenkins时,已经知道自动化部署需要配置jdk、maven和git。在Jenkins镜像中,自带了jdk和git,但是没有maven。
查询jdk和git的路径方法:
先进入 jenkins 的容器中,通过echo $JAVA_HOME来查看 java 路径,通过which git查看 git 执行文件路径。
[root@VM-24-4-centos ~]#
[root@VM-24-4-centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c8e8710bddb jenkins/jenkins "/sbin/tini -- /usr/…" 5 hours ago Up 4 hours 0.0.0.0:10240->8080/tcp, :::10240->8080/tcp, 0.0.0.0:10241->50000/tcp, :::10241->50000/tcp myJenkins
[root@VM-24-4-centos ~]#
[root@VM-24-4-centos ~]#
[root@VM-24-4-centos ~]# docker exec -it myJenkins /bin/bash
jenkins@9c8e8710bddb:/$
jenkins@9c8e8710bddb:/$ echo $JAVA_HOME
/opt/java/openjdk
jenkins@9c8e8710bddb:/$ which git
/usr/bin/git
jenkins@9c8e8710bddb:/$
对于maven,这里选择Jenkins的自动安装,详情见下文。
注:maven会在第一次构建的时候下载
构建任务
可参照Windows的构建:https://blog.csdn.net/qq_42873640/article/details/121491364
遇到的问题
第一次构建时,会下载maven,存放在/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven,可去修改仓库地址。
仓库在/var/jenkins_home/.m2/repository
git clone遇到问题。