1、编写一个Dockerfile文件,内容如下
# 基础镜像jdk,jdk里包含里操作系统
FROM openjdk:8u282-jdk
# 工作目录,也就是容器里目录
WORKDIR /home/prq/
# 添加ppp目录下的文件到容器/home/prq/里
ADD ./ppp /home/prq/
# 暴露端口8080
EXPOSE 8080
# 启动脚本
CMD ./startup.sh
2、ppp目录下准备以下文件,用于运行项目(ppp目录和Dockerfile文件在同一层目录下)
a、一个运行jar ,例如 test.jar ,后面修改代码升级时,用到的jar名称需修改成test.jar
b、启动脚本startup.sh
c、日志目录 logs
d、配置文件目录 conf
3、在Dockerfile所在的目录下执行以下命令
1、 生成test:1.0.1镜像,最后的点不用省略
docker build -t test:1.0.1 .
2、 查看生成的镜像
docker images
3、 通过test:1.0.1镜像启动容器
docker run -itd --privileged=true --name test test:1.0.1
4、进容器(可以在进容器里添加一些基本命令,如telnet、ifconfig命令,不然制作的镜像是没有这些基本命令的)
docker exec -it test /bin/bash
安装一些常用的工具
yum -y install vim
yum -y install net-tools
5、 将镜像test:1.0.1保存为test.tar.gz,默认保存在当前目录
docker save -o test.tar.gz test:1.0.1
6、将test.tar.gz 导入镜像
docker load -i test.tar.gz
7、 通过test:1.0.1镜像启动容器
docker run -itd --privileged=true --name test test:1.0.1
8、如果知道容器id,也可以通过容器id转镜像(将294oajfhfnci容器保存为镜像abcdef)
docker commit 294oajfhfnci test:1.0.1
4、每次升级时,需要把修改的test.jar 通过docker cp命令传到容器里,比较麻烦,所有有了另一种方式,通过compose文件volumes对宿主机和容器做个映射关系。
1、把javaDump.hprof从容器id为db0906d6e687,容器路径为/home/ppp/logs中把javaDump.hprof文件拿到宿主机/home/ppp/目录下
docker cp db0906d6e687:/home/ppp/logs/javaDump.hprof /home/ppp/
2、把宿主机/home/ppp/logs/javaDump.hprof复制到容器id为db0906d6e687的/tmp目录下
docker cp /home/ppp/logs/javaDump.hprof db0906d6e687:/tmp
compose.yml内容
# yaml 配置
version: '3'
services:
nacos-server:
image: nacos/nacos-server:v1.4.3
container_name: nacos-server
network_mode: "host"
environment:
- PREFER_HOST_MODE=ip
- MODE=standalone
volumes:
- /home/docker/volumes/ppp/_data/nacos_data/logs/:/home/nacos/logs
- /home/docker/volumes/ppp/_data/nacos_data/conf/application.properties:/home/nacos/conf/application.properties
- /home/docker/volumes/ppp/_data/nacos_data/data/:/home/nacos/data
ports:
- 8848:8848
restart: always
portainer:
image: "portainer/portainer-ce"
container_name: "portainer"
ports:
- "8000:8000"
- "7000:9000"
restart: always
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
- "/home/docker/volumes/portainer_data/_data:/data"
- "/etc/localtime:/etc/localtime"
- "/etc/timezone:/etc/timezone"
portainer-agent:
image: "portainer/agent"
container_name: "portainer-agent"
ports:
- "9001:9001"
restart: always
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
- "/home/docker/volumes:/var/lib/docker/volumes"
- "/etc/localtime:/etc/localtime"
- "/etc/timezone:/etc/timezone"
redis:
image: "redis:6.2.1"
container_name: "redis"
ports:
- "6379:6379"
restart: always
sysctls:
- net.core.somaxconn=1024
volumes:
- "/home/docker/volumes/ppp/_data/redis_data/conf/redis.conf:/etc/redis/redis.conf"
- "/home/docker/volumes/ppp/_data/redis_data/data:/data"
- "/etc/localtime:/etc/localtime"
- "/etc/timezone:/etc/timezone"
command: redis-server /etc/redis/redis.conf
test:
image: "test:1.0.1"
container_name: "test"
network_mode: "host"
restart: always
environment:
- TZ=Asia/Shanghai
- NACOS_SERVER=10.12.12.12:8848
- NACOS_NAMESPACE=yreyehhfdsd-ea28-47b9-a3fa-e818gege5
- ADMIN_SERVER_URL=http://10.12.12.12:8060
volumes:
- "/home/docker/volumes/ppp/_data/test/startup.sh:/home/ppp/startup.sh"
- "/home/docker/volumes/ppp/_data/test/conf:/home/ppp/conf"
- "/home/docker/volumes/ppp/_data/logs_data:/home/ppp/logs"
- "/home/docker/volumes/ppp/_data/test/node1/test.jar:/home/ppp/test.jar"
- "/home/docker/volumes/ppp/_data/wait-for-it.sh:/home/ppp/wait-for-it.sh"
- "/etc/localtime:/etc/localtime"
- "/etc/timezone:/etc/timezone"
logging:
options:
max-size: 500m
privileged: true
command: /home/ppp/wait-for-it.sh 10.12.12.12:8848 -t 0 --strict -- /bin/sh -c 'sh startup.sh'
其中wait-for-it.sh(网上可下载这个文件)
#wait-for-it.sh ip:端口 检测该ip与端口是否已经能连接,能了就执行后面的命令
#-t 0 超时参数 检测该ip与端口是否已经能连接,超过指定时间后无论ip与端口是否已经能连接都执行后续命令
#-t 0 不设置超时参数 ip与端口不能连接就不执行后续命令
#不加 -t 0 或者 -t 超时时间 都会在超时时间过了之后执行后续命令 不管ip与端口是否已经能连接
#默认超时时间 15秒
./wait-for-it.sh 10.12.12.12:8848 -t 0 -- java -jar /user_ms_docker.jar
10.12.12.12:8848通的话,才执行echo,不通不执行,一直等待
执行脚本insatll.sh
#!/bin/bash
#引入外部脚本
#source ./sysctl.sh
echo "preparing for system envrionment ..."
image_home="../images"
image_redis="redis-6.2.1.tar.gz"
image_portainer="portainer.tar.gz"
image_portainer_agent="portainer-agent.tar.gz"
image_nacos="nacos-v1.4.3.tar.gz"
image_test="test.tar.gz"
ppp="/home/docker/volumes/ppp/_data"
echo "=============update docker path ... ============"
cp ../conf/common/docker.service /etc/systemd/system/multi-user.target.wants/
systemctl daemon-reload
systemctl restart docker
echo "Loading docker images ..."
docker load -i ${image_home}/${image_redis}
docker load -i ${image_home}/${image_portainer}
docker load -i ${image_home}/${image_portainer_agent}
docker load -i ${image_home}/${image_test}
docker load -i ${image_home}/${image_nacos}
echo "Creating volumes ..."
docker volume create ppp
docker volume create portainer_data
echo "preparing for envrionment ..."
mkdir -vp ${ppp}/redis_data ${ppp}/portainer_data ${ppp}/nacos_data
mkdir -vp ${ppp}/redis_data/conf ${ppp}/redis_data/data
mkdir -vp ${ppp}/nacos_data/conf ${ppp}/nacos_data/data ${ppp}/nacos_data/logs
mkdir -vp ${ppp}/zookeeper_data ${ppp}/keepalived_data
mkdir -vp ${ppp}/logs_data
mkdir -vp ${ppp}/lib
mkdir -vp ${test} ${test}/node1 ${test}/node1/logs ${test}/node1/data
cp ../conf/ioms/startup.sh ${test}/
cp -r ../conf/ioms/custom-config ${test}/
cp ../conf/common/wait-for-it.sh ${ppp}
cp -r ../lib ${ppp}/lib
cp ../conf/common/redis.conf ${ppp}/redis_data/conf/
cp ../conf/common/application.properties ${ppp}/nacos_data/conf/
echo "copy keepalived config file ..."
cp ../conf/keepalived/keepalived.conf ${ppp}/keepalived_data/
chmod 777 -R ${ppp}
chmod 644 ${ppp}/keepalived_data/keepalived.conf
echo "=============Starting containers...========== "
echo "=============creating all containers ...==================="
docker-compose -f ../compose.yml up -d nacos-server
docker-compose -f ../compose.yml up -d redis
docker-compose -f ../compose.yml up -d portainer
docker-compose -f ../compose.yml up -d portainer-agent
docker-compose -f ../compose.yml up -d test
卸载脚本uninstall.sh
#! /bin/bash
# 停止容器
docker-compose -f ../compose.yml down
# 删除镜像
docker rmi -f $(docker images -qa)
# 删除数据
docker volume rm test portainer_data
查看完整的容器id和command
docker ps -a --no-trunc