[Docker命令] 几条Docker命令回顾(yobot的Docker安装脚本)

前言

前段时间, 有一个部署项目使用Docker进行部署. 其实部分命令和参数含义已经有点遗忘. 记在此处防止遗忘.


Docker命令

docker run

实战命令

  • Tips1
# 创建Docker网络
docker network create qqbot

# 拉取2个镜像. 默认版本为latest.
docker pull alpine
docker pull yobot/yobot
  • Tips2
# 运行python3镜像. 通关其处理一些文本操作. -v表示文件夹共享
docker run --rm -v ${PWD}:/work -w /work python:3.7-slim-buster python3 -c "
import json
import urllib.request
import ssl
url = 'https://api.github.com/repos/Mrs4s/go-cqhttp/releases'
resp = urllib.request.urlopen(url).read().decode('utf-8')
assets = json.loads(resp)[0]['assets']
for item in assets:
    if item['name'].endswith('linux-amd64.tar.gz'):
        download = item['browser_download_url']
        break
resp = urllib.request.urlopen(download).read()
f = open('go-cqhttp.tar.gz', 'wb')
f.write(resp)
f.close()
"
  • Tips3
# 写入一个Dockerfile文件 
# 随后通过docker build进行构建镜像
# 随后删除Dockerfile文件
echo "
FROM alpine:latest
ADD go-cqhttp /bin/go-cqhttp
WORKDIR /bot
ENTRYPOINT /bin/go-cqhttp
">Dockerfile
docker build . -t gocqhttp
rm Dockerfile -f
  • Tips4
# 初始化镜像. 其实为了go-cqhhtp初始化文件
echo "initializing gocqhttp configure file"
docker run --rm \
           -v ${PWD}/gocqhttp_data:/bot \
           gocqhttp >/dev/null 2>&1
  • Tips5
# 使用python3镜像更改配置文件
docker run --rm -v ${PWD}:/work -w /work -e qqid -e qqpassword python:3.7-slim-buster python3 -c "
import json, os, random, string
access_token = ''.join(random.choices(string.ascii_uppercase + string.ascii_lowercase + string.digits, k=16))
with open('yobot_data/yobot_config.json', 'w') as f:
    json.dump({'access_token': access_token}, f, indent=4)
with open('gocqhttp_data/config.json', 'r+') as f:
    config = json.load(f)
    config['uin'] = int(os.environ['qqid'])
    config['password'] = os.environ['qqpassword']
    config['access_token'] = access_token
    config['enable_db'] = False
    config['web_ui']['enabled'] = False
    config['http_config']['enabled'] = False
    config['ws_config']['enabled'] = False
    config['ws_reverse_servers'] = [{
        'enabled': True,
        'reverse_url': 'ws://yobot:9222/ws/',
        'reverse_api_url': '',
        'reverse_event_url': '',
        'reverse_reconnect_interval': 3000
    }]
    f.seek(0)
    f.truncate()
    json.dump(config, f, indent=4)
"
  • Tips6
# 启动镜像 开放端口9222 使用网络qqbot 使用共享文件夹 
echo "starting yosbot"
docker run -d \
           --name yobot \
           -p 9222:9222 \
           --network qqbot \
           -v ${PWD}/yobot_data:/yobot/yobot_data \
           yobot/yobot
  • Tips7
# 启动qqhttp镜像
echo "starting gocqhttp"
docker run -it \
           --name gocqhttp \
           --network qqbot \
           -v ${PWD}/gocqhttp_data:/bot \
           gocqhttp
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页