#!/bin/bash
echo -e "\e[35m 输入1自定义安装,2默认docker-ce-18.06.1.ce-3.el7安装 \e[0m"
read -p " 请输入1,2: " select_docker_version
if [ $select_docker_version == 1 ]; then
read -p "docker_version版本:" docker_version
yum install -y yum-utils && \
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && \
yum -y install $docker_version && \
systemctl start docker && \
systemctl enable docker > /dev/null
if [ $? -eq 0 ]; then
echo -e "\e[35m $docker_version 安装完成,启动完成并开启开机自启 \e[0m"
else
echo -e "\e[31m $docker_version 安装失败 \e[0m"
fi
echo "安装docker-compose version 1.18.0"
curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose && \
chmod +x /usr/local/bin/docker-compose && \
ln -s /usr/local/bin/docker-compose /usr/bin/ > /dev/null
if [ $? -eq 0 ]; then
echo -e "\e[35m docker-compose version 1.18.0 安装完成,已内置系统命令 \e[0m"
else
echo -e "\e[31m docker-compose version 1.18.0安装失败 \e[0m"
fi
elif [ $select_docker_version == 2 ]; then
yum install -y yum-utils && \
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && \
yum -y install docker-ce-18.06.1.ce-3.el7 && \
systemctl start docker && \
systemctl enable docker > /dev/null
if [ $? -eq 0 ]; then
echo -e "\e[35m docker-ce-18.06.1.ce-3.el7 安装完成,启动完成并开启开机自启 \e[0m"
else
echo -e "\e[31m docker-ce-18.06.1.ce-3.el7 安装失败 \e[0m"
fi
echo "安装docker-compose version 1.18.0"
curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose && \
chmod +x /usr/local/bin/docker-compose && \
ln -s /usr/local/bin/docker-compose /usr/bin/ > /dev/null
if [ $? -eq 0 ]; then
echo -e "\e[35m docker-compose version 1.18.0 安装完成,已内置系统命令 \e[0m"
else
echo -e "\e[31m docker-compose version 1.18.0安装失败 \e[0m"
fi
else
echo -e "输入1,2"
fi
安装扩展yum功能,以便使用yum-config-manager添加docker软件源信息
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装指定版本的格式 ,注意3:xxx 请移除3: 我这里使用docker-ce-18.06.1.ce-3.el7
yum list docker-ce --showduplicates | sort -r
yum -y install docker-ce-[VERSION]
yum install -y docker-ce-18.06.1.ce-3.el7
systemctl start docker && systemctl enable docker
docker info
Containers: 15
Running: 12
Paused: 0
Stopped: 3
Images: 26
Server Version: 18.06.1-ce
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: systemd
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 468a545b9edcd5932818eb9de8e72413e616e86e
runc version: 69663f0bd4b60df09991c08812a60108003fa340
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-693.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 7.585GiB
Name: k8s-node3
ID: 5BLO:XOV2:EFX3:5SXB:M2JF:L6D7:TWGR:UQ6E:A56P:MXAZ:TTQR:R2DG
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose && ln -s /usr/local/bin/docker-compose /usr/bin/
docker-compose --version
docker-compose version 1.18.0, build 8dd22a9
version: '3'
networks:
data_security:
ipam:
config:
- subnet: 172.62.0.0/16 #自定义固定容器ip 实现容器间通讯,增强docker-compose项目可移植性
services:
mysql:
image: mysql:8.0.20
container_name: mysql
ports:
- 30306:3306
command:
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
environment:
- TZ=Asia/Shanghai
- LANG=en_US.UTF-8
- MYSQL_ROOT_PASSWORD=xxxxxx
privileged: true
restart: always
volumes:
- ./config/mysql/conf/my.cnf:/etc/mysql/my.cnf
- ./config/mysql/logs:/var/log/mysql
- ./config/mysql/data:/var/lib/mysql
- ./config/mysql/init:/docker-entrypoint-initdb.d/
#- ./config/mysql/sql:/opt/sql #初始化sql第一次启动mysql并导入数据可以将注释去掉,导完之后注释掉。
networks:
data_security:
ipv4_address: 172.62.0.5
gateway-service:
build: #在docker-compose.yml文件中通过build指定上下文及dockerfile来进行构建镜像
context: ./java/gateway-service/
dockerfile: Dockerfile
image: gateway-service:1.0
container_name: gateway-service
expose:
- 8999
privileged: true
restart: always
environment:
- TZ=Asia/Shanghai
- LANG=en_US.UTF-8
networks:
data_security:
ipv4_address: 172.62.0.6
group-module:
build: #在docker-compose.yml文件中通过build指定上下文及dockerfile来进行构建镜像
context: ./java/group-module/
dockerfile: Dockerfile
image: group-module:1.0
container_name: group-module
expose:
- 8890
privileged: true
restart: always
volumes:
- ./java/xxl-job/:/opt/
environment:
- TZ=Asia/Shanghai
- LANG=en_US.UTF-8
networks:
data_security:
ipv4_address: 172.62.0.7
core4ct-module-system:
build: #在docker-compose.yml文件中通过build指定上下文及dockerfile来进行构建镜像
context: ./java/core4ct-module-system/
dockerfile: Dockerfile
image: core4ct-module-system:1.0
container_name: core4ct-module-system
expose:
- 8080
- 30201
privileged: true
restart: always
environment:
- TZ=Asia/Shanghai
- LANG=en_US.UTF-8
networks:
data_security:
ipv4_address: 172.62.0.8
core4ct-gateway:
build: #在docker-compose.yml文件中通过build指定上下文及dockerfile来进行构建镜像
context: ./java/core4ct-gateway/
dockerfile: Dockerfile
image: core4ct-gateway:1.0
container_name: core4ct-gateway
expose:
- 9000
privileged: true
restart: always
environment:
- TZ=Asia/Shanghai
- LANG=en_US.UTF-8
networks:
data_security:
ipv4_address: 172.62.0.9
#province-module:
# image: province-module:1.0
# container_name: province-module
# expose:
# - 8888
# privileged: true
# restart: always
# environment:
# - TZ=Asia/Shanghai
# - LANG=en_US.UTF-8
# networks:
# data_security:
# ipv4_address: 172.62.0.10
redis:
image: redis:latest
container_name: redis
hostname: redis
restart: always
ports:
- 6379:6379
volumes:
- ./config/redis/conf/redis.conf:/etc/redis/redis.conf #挂载redis.conf不然后续重启redis密码会失效
- ./config/redis/data:/data
environment:
- TZ=Asia/Shanghai
- LANG=en_US.UTF-8
command: ["redis-server","/etc/redis/redis.conf"] #指定配置文件启动redis
networks:
data_security:
ipv4_address: 172.62.0.13
xxl-job-admin:
build: #在docker-compose.yml文件中通过build指定上下文及dockerfile来进行构建镜像
context: ./java/xxl-job-admin/
dockerfile: Dockerfile
image: xxl-job-admin:1.0
container_name: xxl-job-admin
ports:
- 30998:8998
#- 30988:9988
restart: always
environment:
- TZ=Asia/Shanghai
- LANG=en_US.UTF-8
networks:
data_security:
ipv4_address: 172.62.0.11
nginx:
image: nginx:1.21.3
container_name: nginx-group
ports:
- 30028:80
restart: always
volumes:
- ./config/nginx/:/etc/nginx/
- ./config/html/dist/:/usr/share/nginx/html/
environment:
- TZ=Asia/Shanghai
- LANG=en_US.UTF-8
networks:
data_security:
ipv4_address: 172.62.0.12
# nginx-province:
# image: province-frontend:1.20.1
# container_name: nginx-province
# ports:
# - 30029:443
# restart: always
# volumes:
# - ./config/nginx-province/:/etc/nginx/
# - ./config/html-province/dist/:/usr/share/nginx/html/
# environment:
# - TZ=Asia/Shanghai
# - LANG=en_US.UTF-8
# networks:
# data_security:
# ipv4_address: 172.62.0.22
介于某些环境中java微服务需要到nacos拿配置文件,而这个时候nacos的启动又很慢,就会导致我们的java微服务找不到nacos而启动失败。docker-compos.yml中有一个depends_on参数可用,但是实际用起来还是不太灵光,它只是把相互有依赖的容器按照顺序启动;只是容器的启动,容器内的服务启动还是跟之前一样,所以并没有采用。我这里采用的是wait-for的一个思路,将写好的shell脚本check_nacos.sh嵌入容器。通过curl循环判断去探测nacos状态码返回值,如果为200则跳出循环执行run.sh,run.sh的内容也就是java项目的启动命令。以下是代码,路径,dockerfile等。
#!/bin/bash
while :
do
# 访问nacos注册中心,获取http状态码
CODE=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://172.62.0.15:8848/nacos/#/login`
# 判断状态码为200
if [[ $CODE -eq 200 ]]; then
# 输出绿色文字,并跳出循环
echo -e "\033[42;34m nacos-server is ok \033[0m"
break
else
# 暂停1秒
sleep 1
echo -e "\033[41;37m nacos-server is not ok \033[0m"
fi
done
# while结束时,也就是nacos启动完成后,执行容器中的run.sh。
bash /root/run.sh
#!/bin/bash
java -jar /root/core4ct-gateway-1.0-SNAPSHOT.jar
FROM java:8
COPY ./core4ct-gateway-1.0-SNAPSHOT.jar /root
COPY ./check_nacos.sh /root
COPY ./run.sh /root
EXPOSE 9000
ENTRYPOINT ["bash","/root/check_nacos.sh"]
以上项目启动时会自动生成一个docker网络,因为我们docker-compose.yml内定义了网络的子网,所以单项目正常使用起来也没什么问题。如果有多个项目多个docker-compose.yml的场景下,多个项目之间也有微服务需要通过docker内部网络进行通信,或者我们docker-compose down -v时这个网卡都会随着我们项目的关闭而被释放掉。介于这种情况我们需要在外建立一个docker网格并指定网卡,再通过docker-compose.yml使用此网卡,即可实现多项目、多docker-compose.yml互相通过内部调用微服务、单独启动关闭某个项目;以下是配置摘要:
docker network create --driver bridge --subnet 172.62.0.0/16 --gateway 172.62.0.1 cq-data-security
docker-compose.yml
version: '3'
networks:
cq-data-security:
external: true
services:
mysql:
image: mysql:8.0.20
container_name: mysql
networks:
cq-data-security:
ipv4_address: 172.62.0.5