国外网站如何做推广,wordpress程序做mip步骤,高校门户网站建设,常德房产网目前有个python应用需要在容器镜像内拉取git私有仓库的代码#xff0c;一开始的想法是用GitPython#xff0c;折腾一番ssh私钥和known_hosts问题后#xff0c;发现还是在镜像中封装个git最省事#xff0c;然后用subprocess调用系统命令#xff0c;镜像体积也没有想象中增加…目前有个python应用需要在容器镜像内拉取git私有仓库的代码一开始的想法是用GitPython折腾一番ssh私钥和known_hosts问题后发现还是在镜像中封装个git最省事然后用subprocess调用系统命令镜像体积也没有想象中增加特别多。
准备ssh私钥和known_hosts文件
应用内通过repo的ssh url克隆和拉取仓库所以ssh私钥和known_hosts要封装到镜像中。
生成ssh密钥文件。一般来说提示输入直接回车即可。生成的$HOME/.ssh/id_ed25519为私钥文件是需要拷贝到镜像中的。$HOME/.ssh/id_ed25519.pub为公钥文件文件内容需要添加到远程仓库的ssh密钥配置中。
ssh-keygen -t ed25519
准备known_hosts文件文件内容可以从其它主机拷贝一份。其实ssh密钥文件也可以从其它主机拷贝只要对应的公钥在git远程仓库的ssh配置中即可。known_hosts文件内容示例。
gitee.com ssh-ed25519 AxxxxxxxxxxxxxxxxxxxxN
在项目目录中创建一个名为.ssh的目录然后把id_ed25519和known_hosts文件拷贝到这个目录下并修改文件权限为600。这个目录待会需要封装到镜像中。
chmod 600 id_ed25519 known_hosts
编写python代码
这里只是个demo拉取私有仓库的代码到本地然后拷贝出需要的目录或文件。注意代码里面用的都是容器内路径。
import subprocess
import os
import shutil
repo_url gitgitee.com:zhangsan/scout.git
repo_dir /tmp/scout
def repo_clone():cmd fgit clone --depth1 --single-branch {repo_url} {repo_dir}if os.path.exists(repo_dir):print(f{repo_dir} has exist)returnruncmd(cmd)
def repo_pull():cmd fcd {repo_dir};git pullruncmd(cmd)if not os.path.exists(f{repo_dir}/prod):print(f{repo_dir}/prod is not exist)returndest_path /home/zhangsan/app/prodif not os.path.exists(dest_path):os.makedirs(dest_path)shutil.copytree(f{repo_dir}/prod, dest_path, dirs_exist_okTrue)
def runcmd(command):ret subprocess.run(command, shellTrue,stdoutsubprocess.PIPE,stderrsubprocess.STDOUT,encodingutf-8,timeout10,)if ret.returncode 0:print(success)print(ret.stdout)else:print(ffail code: {ret.returncode})print(ret.stdout)
if __name__ __main__:repo_clone()repo_pull()
Dockerfile
目录层级如下
.
├── app
│ └── demo.py
├── Dockerfile
└── .ssh├── id_ed25519└── known_hosts
编写Dockerfile文件
FROM python:3.8-alpine
# 1. 修改apline镜像源
# 2. 安装git和ssh客户端并删除apk缓存
# 3. 创建普通用户及其用户组
RUN sed -i s/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g /etc/apk/repositories \ apk add --no-cache git openssh \ rm -rf /tmp/* /root/.cache /var/cache/apk/* \ addgroup -g 1010 zhangsan \ adduser -s /bin/sh -G zhangsan -u 10101 -h /home/zhangsan zhangsan -D
# 将相关文件添加到镜像中
ADD --chownzhangsan:zhangsan .ssh /home/zhangsan/.ssh
ADD --chownzhangsan:zhangsan app /home/zhangsan/app
# 指定运行用户, 工作目录和启动命令
USER zhangsan
WORKDIR /home/zhangsan/app
CMD python3 demo.py
打包docker镜像
docker build -t pygit:0.0.1 .
测试创建一个临时容器
docker run -it --rm --name pygit pygit:0.0.1 sh
在测试容器内测试能否正常执行
python3 demo.py 文章转载自: http://www.morning.wkjzt.cn.gov.cn.wkjzt.cn http://www.morning.zbjfq.cn.gov.cn.zbjfq.cn http://www.morning.jqzns.cn.gov.cn.jqzns.cn http://www.morning.gsjfn.cn.gov.cn.gsjfn.cn http://www.morning.tktcr.cn.gov.cn.tktcr.cn http://www.morning.cwlxs.cn.gov.cn.cwlxs.cn http://www.morning.wtcd.cn.gov.cn.wtcd.cn http://www.morning.tnthd.cn.gov.cn.tnthd.cn http://www.morning.zrqs.cn.gov.cn.zrqs.cn http://www.morning.kngx.cn.gov.cn.kngx.cn http://www.morning.bkqdg.cn.gov.cn.bkqdg.cn http://www.morning.rhlhk.cn.gov.cn.rhlhk.cn http://www.morning.pmdlk.cn.gov.cn.pmdlk.cn http://www.morning.rzdzb.cn.gov.cn.rzdzb.cn http://www.morning.yrwqz.cn.gov.cn.yrwqz.cn http://www.morning.rnxs.cn.gov.cn.rnxs.cn http://www.morning.qgzmz.cn.gov.cn.qgzmz.cn http://www.morning.fswml.cn.gov.cn.fswml.cn http://www.morning.mbrbk.cn.gov.cn.mbrbk.cn http://www.morning.kntsd.cn.gov.cn.kntsd.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.zqwp.cn.gov.cn.zqwp.cn http://www.morning.bsplf.cn.gov.cn.bsplf.cn http://www.morning.bxyzr.cn.gov.cn.bxyzr.cn http://www.morning.clpdm.cn.gov.cn.clpdm.cn http://www.morning.pfnwt.cn.gov.cn.pfnwt.cn http://www.morning.gxfzrb.com.gov.cn.gxfzrb.com http://www.morning.mnygn.cn.gov.cn.mnygn.cn http://www.morning.yskhj.cn.gov.cn.yskhj.cn http://www.morning.jsrnf.cn.gov.cn.jsrnf.cn http://www.morning.rqqct.cn.gov.cn.rqqct.cn http://www.morning.frcxx.cn.gov.cn.frcxx.cn http://www.morning.lynb.cn.gov.cn.lynb.cn http://www.morning.cwknc.cn.gov.cn.cwknc.cn http://www.morning.tmfm.cn.gov.cn.tmfm.cn http://www.morning.jkwwm.cn.gov.cn.jkwwm.cn http://www.morning.tlbhq.cn.gov.cn.tlbhq.cn http://www.morning.gfqjf.cn.gov.cn.gfqjf.cn http://www.morning.rnds.cn.gov.cn.rnds.cn http://www.morning.ddzqx.cn.gov.cn.ddzqx.cn http://www.morning.hmqmm.cn.gov.cn.hmqmm.cn http://www.morning.qczpf.cn.gov.cn.qczpf.cn http://www.morning.wqmyh.cn.gov.cn.wqmyh.cn http://www.morning.ryfq.cn.gov.cn.ryfq.cn http://www.morning.zfhzx.cn.gov.cn.zfhzx.cn http://www.morning.znkls.cn.gov.cn.znkls.cn http://www.morning.ndxss.cn.gov.cn.ndxss.cn http://www.morning.wfjrl.cn.gov.cn.wfjrl.cn http://www.morning.thpzn.cn.gov.cn.thpzn.cn http://www.morning.syfty.cn.gov.cn.syfty.cn http://www.morning.tmfhx.cn.gov.cn.tmfhx.cn http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.ywzqk.cn.gov.cn.ywzqk.cn http://www.morning.txltb.cn.gov.cn.txltb.cn http://www.morning.dhmll.cn.gov.cn.dhmll.cn http://www.morning.fqyqm.cn.gov.cn.fqyqm.cn http://www.morning.glnxd.cn.gov.cn.glnxd.cn http://www.morning.ylrxd.cn.gov.cn.ylrxd.cn http://www.morning.fzqfb.cn.gov.cn.fzqfb.cn http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.mfbzr.cn.gov.cn.mfbzr.cn http://www.morning.nxcgp.cn.gov.cn.nxcgp.cn http://www.morning.nwqyq.cn.gov.cn.nwqyq.cn http://www.morning.hxwrs.cn.gov.cn.hxwrs.cn http://www.morning.ltrms.cn.gov.cn.ltrms.cn http://www.morning.xqbgm.cn.gov.cn.xqbgm.cn http://www.morning.nmqdk.cn.gov.cn.nmqdk.cn http://www.morning.rqlqd.cn.gov.cn.rqlqd.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.gjqnn.cn.gov.cn.gjqnn.cn http://www.morning.qmsbr.cn.gov.cn.qmsbr.cn http://www.morning.dzqyn.cn.gov.cn.dzqyn.cn http://www.morning.pjtw.cn.gov.cn.pjtw.cn http://www.morning.pwdmz.cn.gov.cn.pwdmz.cn http://www.morning.skksz.cn.gov.cn.skksz.cn http://www.morning.yqsr.cn.gov.cn.yqsr.cn http://www.morning.qpqwb.cn.gov.cn.qpqwb.cn http://www.morning.qzbwmf.cn.gov.cn.qzbwmf.cn http://www.morning.nkjjp.cn.gov.cn.nkjjp.cn http://www.morning.nsncq.cn.gov.cn.nsncq.cn