Loading

离线部署-docker

离线部署---docker

关键词:docker离线部署,images离线安装,docker compose,shell,minio

docker离线安装

docker install offline

下载相应的版本的deb包,我的系统是ubuntu20.04,因此选择focal文件夹,路径:

image-20230918160059759

按照文档下载相应的包,挑选自己想要的版本。

从部署脚本中截取安装docker的部分:

#!/bin/bash

echo "Please run this script as root. Because it will install docker and docker-compose.
If you have installed docker and docker-compose, please ignore this message.
And i use docker-compose version is 3.8, so please make sure your docker engine version is 19.03.0+."

echo

# check user is root
whoami=$(whoami)
if [ "$whoami" != "root" ]; then
	echo "Please run as root."
	exit 1
fi

# check docker is installed

# verlt 24.0.5 19.03.0 && echo "yes" || echo "no" # no
# this function check $1 is less than $2
versionLessThan() {
	if [ "$1" = "$2" ]; then
		return 1
	else
		# sort -V: natural sort of (version) numbers within text
		[ "$1" = "$(echo -e "$1\n$2" | sort -V | head -n1)" ]
	fi
}

if ! which docker >/dev/null 2>&1; then

	echo "docker is not installed. Start install docker."

	# install docker use offline package
	cd docker-install || exit

	sudo dpkg -i ./containerd.io_1.6.21-1_amd64.deb \
		./docker-ce_24.0.2-1~ubuntu.20.04~focal_amd64.deb \
		./docker-ce-cli_24.0.2-1~ubuntu.20.04~focal_amd64.deb \
		./docker-buildx-plugin_0.10.5-1~ubuntu.20.04~focal_amd64.deb \
		./docker-compose-plugin_2.18.1-1~ubuntu.20.04~focal_amd64.deb

	sudo service docker start

	cd ..
fi

如果当前机器已经安装docker了,那么我检查当前docker engine版本,我用的docker compose版本为3.8,因此我需要docker engine 大于等于 19.03.0

# check docekr engine version
docker_version=$(docker version --format '{{.Server.Version}}')
echo "docker engine version is $docker_version"
# if docker_version less than 19.03.0, exit
if versionLessThan "$docker_version" "19.03.0"; then
	echo "docker engine version is less than 19.03.0, please upgrade docker engine."
	exit 1
fi

离线安装镜像

将docker镜像备份:docker save -o yourImage.tar company/yourImage:v1.0

将备份的tar上传到离线的机器,加载镜像:docker load -i yourImage.tar

docker compose 启动容器

镜像加载好后,就可以用docker compose启动容器了

我的docker compose:

version: "3.8"
services:
  cogent-admin:
    container_name: cogent-admin
    image: cogent/cogent-admin:v1.0
    volumes:
      - cogent-admin-data:/opt/cogent-admin/logs
      - cogent-admin-data:/opt/cogent-admin/config
      - cogent-admin-data:/opt/cogent-admin/uploadPath
      - cogent-admin-data:/opt/device-upgrade-pack-repo
    network_mode: "host"
    depends_on:
      - redis
      - mysql
  redis:
    image: cogent/redis:v1.0
    container_name: redis
    command: redis-server /etc/redis/redis.conf --appendonly yes
    volumes:
      - redis-data:/data #数据文件挂载
      - redis-data:/etc/redis
    network_mode: "host"
  mysql:
    container_name: mysql
    image: cogent/mysql:v1.0
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --default-time-zone='+08:00'
    restart: always
    environment:
      MYSQL_DATABASE: 'cogent-admin'
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - mysql-data:/var/lib/mysql #数据文件挂载
      - mysql-conf-data:/etc/mysql #配置文件挂载    记得设置时区,东八区
      - mysql-log-data:/var/log/mysql #日志文件挂载
    network_mode: "host"
  mediakit:
    image: zlmediakit/zlmediakit:Debug.latest
    container_name: mediakit
    volumes:
      - mediakit-data:/opt/media/bin/log
      - mediakit-data:/opt/media/conf/config.ini
    network_mode: "host"
  minio:
    image: quay.io/minio/minio
    container_name: minio
    command: server /data --console-address=":9090" --address=":9002" --ftp="address=:8021" --ftp="passive-port-range=30000-40000"
    environment:
      MINIO_ROOT_USER: ROOTNAME
      MINIO_ROOT_PASSWORD: CHANGEME123
      MINIO_NOTIFY_WEBHOOK_ENABLE_PRIMARY: "on"
      MINIO_NOTIFY_WEBHOOK_ENDPOINT_PRIMARY: "http://127.0.0.1:8080/buckets/event"
    volumes:
      - minio-data:/data
    network_mode: "host"

volumes:
  cogent-admin-data:
  mediakit-data:
  mysql-data:
  mysql-conf-data:
  mysql-log-data:
  redis-data:
  minio-data:

mysql的数据文件不能和日志文件等放到一起,否则启动时会报错,说数据目录已经有文件

对应用进行初始化

如果你有在初次部署时有对某些应用进行初始化的操作,比如我需要在初次部署时,将minio创建Access key,创建默认bucket,开启webhook。那么可以在minio容器启动后:

# check minio is running
if pgrep -x "minio" >/dev/null; then
	echo "minio init..."
	# minio deploy: create access key and secret key, create bucket, webhook event
	docker exec -i minio bash < minioDeploy.sh
else
	echo "minio is not running. This will influence file upload and download."
fi

这样会在容器中执行minioDeploy.sh脚本。

minioDeploy.sh脚本内容:

#!/bin/bash

# minio deploy: create access key and secret key, create bucket, webhook event
chmod +x /opt/bin/mc
cd /opt/bin || exit
mc alias set myminio http://127.0.0.1:9002 ROOTNAME CHANGEME123
mc admin user svcacct add --access-key "xFkEokGRAlwIs1234" --secret-key "jaz4FlvCDDKvbBuQYJ9NIohMUlRDsT132465789" myminio ROOTNAME
mc mb myminio/cogent
mc event add myminio/cogent arn:minio:sqs::PRIMARY:webhook --event put
mc event add myminio/cogent arn:minio:sqs::PRIMARY:webhook --event delete

完整的部署脚本:

#!/bin/bash

echo "Please run this script as root. Because it will install docker and docker-compose.
If you have installed docker and docker-compose, please ignore this message.
And i use docker-compose version is 3.8, so please make sure your docker engine version is 19.03.0+."

echo

# check user is root
whoami=$(whoami)
if [ "$whoami" != "root" ]; then
	echo "Please run as root."
	exit 1
fi

# check docker is installed

# verlt 24.0.5 19.03.0 && echo "yes" || echo "no" # no
# this function check $1 is less than $2
versionLessThan() {
	if [ "$1" = "$2" ]; then
		return 1
	else
		# sort -V: natural sort of (version) numbers within text
		[ "$1" = "$(echo -e "$1\n$2" | sort -V | head -n1)" ]
	fi
}

if ! which docker >/dev/null 2>&1; then

	echo "docker is not installed. Start install docker."

	# install docker use offline package
	cd docker-install || exit

	sudo dpkg -i ./containerd.io_1.6.21-1_amd64.deb \
		./docker-ce_24.0.2-1~ubuntu.20.04~focal_amd64.deb \
		./docker-ce-cli_24.0.2-1~ubuntu.20.04~focal_amd64.deb \
		./docker-buildx-plugin_0.10.5-1~ubuntu.20.04~focal_amd64.deb \
		./docker-compose-plugin_2.18.1-1~ubuntu.20.04~focal_amd64.deb

	sudo service docker start

	cd ..
fi

echo "docker is installed."
# check docekr engine version
docker_version=$(docker version --format '{{.Server.Version}}')
echo "docker engine version is $docker_version"
# if docker_version less than 19.03.0, exit
if versionLessThan "$docker_version" "19.03.0"; then
	echo "docker engine version is less than 19.03.0, please upgrade docker engine."
	exit 1
fi

# laod images
docker load -i redis.tar
docker load -i mysql.tar
docker load -i mediakit.tar
docker load -i cogent-admin.tar
docker load -i minio.tar

# start docker containers
docker compose up -d redis mysql minio
docker compose up -d cogent-admin mediakit

# check minio is running
if pgrep -x "minio" >/dev/null; then
	echo "minio init..."
	# minio deploy: create access key and secret key, create bucket, webhook event
	docker exec -i minio bash < minioDeploy.sh
else
	echo "minio is not running. This will influence file upload and download."
fi
posted @ 2023-09-18 16:24  KeBoom  阅读(34)  评论(0编辑  收藏  举报