docker(完结)

docker(完结)

部署Redis集群

img

# 创建网卡
docker network create redis --subnet 172.38.0.0/16
# 通过脚本创建六个redis配置
for port in $(seq 1 6);\
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >> /mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done

# 通过脚本运行六个redis
for port in $(seq 1 6);\
docker run -p 637${port}:6379 -p 1667${port}:16379 --name redis-${port} \
-v /mydata/redis/node-${port}/data:/data \
-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
docker exec -it redis-1 /bin/sh #redis默认没有bash
redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379  --cluster-replicas 1

img

docker搭建redis集群完成!

img

SpringBoot打包Docker镜像

1、构建SpringBoot项目

2、打包运行

mvn package

3、编写dockerfile

FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]

4、构建镜像

# 1.复制jar和DockerFIle到服务器
# 2.构建镜像
$ docker build -t xxxxx:xx  .

5、发布运行

以后我们使用了Docker之后,给别人交付就是一个镜像即可!

容器中安装vim

#进入镜像命令
docker exec -it 镜像Id或镜像name /bin/bash

#配置国内镜像源
mv /etc/apt/sources.list /etc/apt/sources.list.bak
echo "deb http://mirrors.163.com/debian/ jessie main non-free contrib" >/etc/apt/sources.list
echo "deb http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list
echo "deb-src http://mirrors.163.com/debian/ jessie main non-free contrib" >>/etc/apt/sources.list
echo "deb-src http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list

#更新安装源
apt-get update 

#安装命令
apt-get install -y vim

docker-compose

下载

image-20200914145252017
# 查看文件所在路径
[root@centos8 compose]# ls
docker-compose-Linux-x86_64
[root@centos8 compose]# pwd 
/usr/local/docker/compose
# 移动文件到bin目录
mv /usr/local/docker/compose/docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
# 执行权限
sudo chmod +x /usr/local/bin/docker-compose
# 验证安装
[root@centos8 compose]# docker-compose version
docker-compose version 1.27.2, build 18f557f9
docker-py version: 4.3.1
CPython version: 3.7.7
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

DockerHub私有仓库

# 拉取镜像
docker pull registry:2.7.1
# 启动镜像
docker run -d -p 5000:5000 --name cxyRegistry -v /usr/local/docker/compose/registry:/var/lib/registry 镜像id
# 访问
http://ip:5000/v2/