Centos7 install docker, visual management, springboot package docker image, jenkins deployment

You can use yum -y install docker to install, this installation version is relatively low

It is recommended to use the following

1.把yum包更新到最新 
yum update
2安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的 
yum install -y yum-utils device-mapper-persistent-data lvm2
3,设置yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
4,可以查看所有仓库中所有docker版本,并选择特定版本安装
yum list docker-ce --showduplicates | sort -r
5,安装Docker,命令:yum install docker-ce-版本号
yum install docker-ce-18.03.1.ce
或者使用curl安装最新版
curl -fsSL https://get.docker.com/ | sh
6.启动docker 并加入开机启动
systemctl start docker & systemctl enable docker
7,验证安装是否成功(有client和service两部分表示docker安装启动都成功了)
docker version 

Modify the mirror source

vi /etc/docker/daemon.json Add any of the following mirror addresses I added below are from Alibaba Cloud and NetEase

{
    "registry-mirrors":["https://6kx4zyno.mirror.aliyuncs.com","http://hub-mirror.c.163.com"]
}

Restart docker

systemctl daemon-reload & systemctl restart docker

View the mirror source configuration docker info command at the bottom of the output information

Open 2375 and add -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock after ExecStart=/usr/bin/dockerd

vi /usr/lib/systemd/system/docker.service

#重启docker
systemctl daemon-reload & systemctl restart docker
#测试是否有效
curl http://127.0.0.1:2375/version
#linux开放2375端口
firewall-cmd --add-port=2375/tcp --permanent && firewall-cmd --reload

Install docker-compose

方法一:
#下载程序
curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose 
#加上可执行权限
chmod +x /usr/local/bin/docker-compose
方法二:
#安装python-pip
yum -y install epel-release
yum -y install python-pip
#安装docker-compose
pip install docker-compose
#------------------------------------------------------
#检查是否安装成功
docker-compose version
#选择性升级p ip install  docker-compose==1.25.4

swarm cluster creation (this on-demand configuration)

1.manager节点初始化swarm
docker swarm init --advertise-addr 192.168.0.202
2.manager节点启动swarm
docker swarm init --listen-addr 192.168.1.101:2377
3.防火墙开放2377
firewall-cmd --add-port=2377/tcp --permanent && firewall-cmd --reload
4.slav节点 连接
docker swarm join --token SWMTKN-1-16w909b22xrmlagydv9pqe1t9hj3mvlmdn2ac723c591ujfsqc-anyw63h25rbodd32jqcb81udn 192.168.1.101:2377

5.退出集群
docker swarm leave

创建网络
docker network create -d overlay -o swarm app-net
-d 驱动
-o 作用域

 

Install portainer and start 

docker pull docker.io/portainer/portainer &&
docker run -d -p 9000:9000 \
--restart=always \
-v /var/run/docker.sock:/var/run/docker.sock \
--name prtainer_9000 \
docker.io/portainer/portainer

Web interface access http://192.168.0.201:9000/   when you log in, you can connect to Local

 

Build springboot image

1.Maven's pom.xml file is added to the docker build plugin

<!--  docker镜像构建插件 构建时会去本地docker环境,找不到会找环境变量DOCKER_HOST docker的2375地址  -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.1.1</version>
                <executions>
                    <execution>
                        <!-- mvn build时同时构建docker镜像  -->
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>   <!-- 这个就相当于dockerfile  -->
                    <imageName>aaa/springboot1</imageName><!-- 这个名字随便取 一般是 空间名/项目名:版本号 -->
                    <baseImage>java:8-jre</baseImage>
                    <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>   <!-- 这是容器启动要执行的命令 -->
                    <resources> <!-- 这个是copy命令 -->
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>

2.docker-compose.yml 配置   

  springboot1:
    container_name: springboot1 
    image: aaa/springboot1 #空间名/项目:版本号  没有版本号就是最新版本,本地找不到会去网络上找
    ports:
      - 8082:8080  #宿主机端口:容器内部端口(就是能访问springboot的端口)

3 Put it in the server with docker, cd to this directory and run docker-compose up -d 

Publish to Alibaba Cloud private docker space

1. Create a namespace. This is free and you can use your own account to register

2. Push the local mirror to the namespace. This is described in detail in the basic information of your own warehouse

Note: When pushing, if your machine is not an Alibaba Cloud server, use the public network push address registry.cn-hangzhou.aliyuncs.com/aaa/springboot1: version number. If it is an Alibaba Cloud server, please note that your server network is proprietary For the network or the classic network, you must choose the corresponding domain name proprietary (registry-vpc.cn-hangzhou.aliyuncs.com//aaa/springboot1: version number), classic (registry-internal.cn-hangzhou.aliyuncs.com/aaa/springboot1 :Version number) Do not select the public network address when the server pushes, it will be very slow

#登陆
docker login --username=阿里云用户名 --password=阿里云密码 registry.cn-hangzhou.aliyuncs.com
#标记
docker tag aaa/springboot registry-internal.cn-hangzhou.aliyuncs.com/aaa/springboot:0.0.1
#推送
docker push registry-internal.cn-hangzhou.aliyuncs.com/aaa/springboot:0.0.1

3. Run Alibaba Cloud image

  springboot1:
    container_name: springboot1 
    image: registry.cn-hangzhou.aliyuncs.com/aaa/springboot1:版本号 #空间名/项目:版本号  没有版本号就是最新版本,本地找不到会去网络上找
    ports:
      - 8082:8080  #宿主机端口:容器内部端口(就是能访问springboot的端口)

Use jenkins to automate 

Process: build the image locally --> push the image locally --> then deploy the machine to run the image using docker-compose

The configuration of docker-compose.yml under /usr/local/sh/docker-compose/aaa is as follows

  springboot1:
    container_name: springboot1 
    image: registry.cn-hangzhou.aliyuncs.com/aaa/springboot1:${Version}
    ports:
      - 8082:8080  #宿主机端口:容器内部端口(就是能访问springboot的端口)

Some scripts of jenkins rely on the input variables Status and Version, which are set by themselves

#!/bin/bash -ilex
#加入下面这一行,防止jenkins运行完成之后kill脚本
BUILD_ID=DONTKILLME
#设置版本号 如果没有输入就使用svn版本号
export Version=${Version}
if [ ! -n "$Version" ]; then
  export Version=${SVN_REVISION}
fi
echo "projects : ${projects} Version:${Version} Status:${Status} "

#docker-compose文件目录
docker_compose_dir="/usr/local/sh/docker-compose/aaa"
#部署机器ip
remote_ip=192.168.0.202
remote_user=root
#=============================== 部署 ===============================
function run(){ #定义部署和启动项目 函数
  #远程登录 需要配置ssh信任
  ssh $remote_user@$remote_ip "mkdir -p $docker_compose_dir"
  #复制docker-componse文件到需要部署的机器
  scp -r $docker_compose_dir $remote_user@$remote_ip:$docker_compose_dir/../
  ssh -t $remote_user@$remote_ip << remotessh
  #因为docker-compose在文件访问的环境变量,所以需要在ssh环境下重新设置环境变量
  export Version=${Version}
  #登陆
  docker login --username=阿里云用户名 --password=阿里云密码 registry.cn-hangzhou.aliyuncs.com
  echo "---------------- ${remote_ip}开始运行docker-compose Version=${Version}----------------------"
  #提前拉取镜像,减少docker-compose up时间
  cd "${docker_compose_dir}/wudao-supermanex-base" &&  docker-compose pull && docker-compose stop && docker-compose up --build -d
  echo "---------------- ${remote_ip}结束运行docker-compose Version=${Version} ----------------------"
  #登出远程服务
  exit
remotessh
exit
}
#=============================== 打包 ===============================
function build(){
    #删除未使用到的镜像
    #docker image prune -a -f
    echo "---------------- 开始maven打包 镜像版本为${Version} ----------------------"
    cd ${WORKSPACE} && mvn clean package -plcom.aaa:springboot -am
    echo "---------------- 完成maven打包 镜像版本为${Version}----------------------"
    echo "---------------- 开始maven clean ----------------------"
    cd ${WORKSPACE} && mvn clean -pl com.aaa:springboot  -am
    echo "---------------- 完成maven clean ----------------------"
    #删除没有名字的镜像
    #docker rmi $(docker images -f "dangling=true" -q) || echo "没有版本号为none的镜像"
    echo "---------------- 开始tag和push:${Version}  ----------------------"
    #登陆
    docker login --username=阿里云用户名 --password=阿里云密码 registry.cn-hangzhou.aliyuncs.com
    #标记
    docker tag aaa/springboot registry.cn-hangzhou.aliyuncs.com/aaa/springboot1:"${Version}"
    sleep 1s
    #推送
    docker registry.cn-hangzhou.aliyuncs.com/aaa/springboot1:"${Version}"
    sleep 30s
    echo "---------------- 结束tag和push:${Version}  ----------------------"
}

if [[ $Status =~ 'build' ]]; then
    build
fi
if [[ $Status =~ 'run' ]]; then
    run
fi

 

Ubntu install docker

Alibaba Cloud Server ubntu16.04

更新源文件
apt-get update
#阿里云服务器直接通过镜像安装
curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/intranet | sh

#启动docekr
sudo service docker start
#停止docekr
sudo service docker stop
#重启docekr
sudo service docker restart

开启2375端口
方法1
修改/lib/systemd/system/docker.service文件内容
[Service]
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375

方法2
修改/etc/docker/daemon.json 
{ "hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"] } 

#加载docker守护线程 && 重启docker
systemctl daemon-reload && systemctl restart docker 


ubntu防火墙命令
关闭防火墙
sudo ufw disable
查看防火墙状态 包含开放的端口
ufw status
ufw default allow/deny:外来访问默认允许/拒绝
ufw allow/deny 20:允许/拒绝 访问20端口,20后可跟/tcp或/udp,表示tcp或udp封包。
ufw allow 2375 开放2375
ufw deny 20	关闭80
ufw allow proto tcp from 10.0.1.0/10 to 本机ip port 25:允许自10.0.1.0/10的tcp封包访问本机的25端口。
ufw delete allow/deny 20:删除以前定义的"允许/拒绝访问20端口"的规则



报错docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "process_linux.go:293: copying bootstrap data to pipe caused "write init-p: broken pipe"": unknown.
linux内核和docker版本不兼容,解决方案(升级内核或者降低 docker 版本) 
uname -r 查看内核版本     输出3.13.0-86-generic
docker -v 查看docker版本  输出Docker version 18.06.3-ce, build d7080c1

#卸载docker
方法1
sudo apt-get remove docker  
sudo apt-get remove --auto-remove docker
方法2
sudo apt remove docker-ce

# 安装指定版本的Docker-CE:
# Step 1: 查找Docker-CE的版本:
apt-cache madison docker-ce

# 出现的查找结果如下
docker-ce | 17.03.1~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
docker-ce | 17.03.0~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages

# Step 2: 安装指定版本的Docker-CE: (VERSION 例如上面的 17.03.1~ce-0~ubuntu-xenial)
sudo apt-get -y install docker-ce=[VERSION]

 

 

 

Guess you like

Origin blog.csdn.net/chen_cxl/article/details/106780209