Node.js 容器销毁前Docker git推送
我已停靠一个节点项目。此项目生成文件 每当容器被销毁时,我都会丢失生成的所有文件。我正在考虑使用“SIGTERM”事件,并在容器即将销毁时进行git推送 在这种情况下,还有其他方法可以使用吗?您可以使用(请参阅)。它允许从容器访问主机文件系统,这意味着容器能够将文件存储在主机可用的目录中,即使它退出Node.js 容器销毁前Docker git推送,node.js,git,docker,Node.js,Git,Docker,我已停靠一个节点项目。此项目生成文件 每当容器被销毁时,我都会丢失生成的所有文件。我正在考虑使用“SIGTERM”事件,并在容器即将销毁时进行git推送 在这种情况下,还有其他方法可以使用吗?您可以使用(请参阅)。它允许从容器访问主机文件系统,这意味着容器能够将文件存储在主机可用的目录中,即使它退出 对于您的情况,这意味着项目将使用容器本地的文件路径在某个文件夹中生成文件,而您仍将使用主机系统路径从外部看到更改。据我所知,您希望能够在销毁容器之前将文件保存在容器中。您有几种可能性: 在您的容器
对于您的情况,这意味着项目将使用容器本地的文件路径在某个文件夹中生成文件,而您仍将使用主机系统路径从外部看到更改。据我所知,您希望能够在销毁容器之前将文件保存在容器中。您有几种可能性:
- 在您的容器上-即使在容器被销毁后,您的数据仍然可用,并且可以装载到新容器上或备份到其他地方
- 正如-所指出的,在容器销毁后,您的数据将在主机上可用
- 使用销毁前备份主机上的数据
#!/usr/bin/env bash
set -ex
# The docker registry should be located at a hostname:port address. (see push ref link below)
REGISTRY=TESTRACK:5000
TAG=${2}
CONTAINER_NAME=${1}
function get-container-id() {
# When executed from within a docker container, the kernel cgroup file contains a control group for docker processes
# The container id of the current container (from within that container) can be fetched this way.
cat /proc/self/cgroup | grep "cpuset:/docker" | sed 's/\([0-9]\+\):cpuset:\/docker\///g'
# Below, using head, is less reliable; the docker line is not always the first line in the cgroup
# head -1 /proc/self/cgroup | cut -d/ -f3
}
CONTAINER_ID=$(get-container-id)
if [ -z ${CONTAINER_ID} ]; then
echo "Error. Did not find a docker container value!"
exit 1
fi
# https://docs.docker.com/engine/reference/commandline/commit/#examples
docker commit ${CONTAINER_ID} ${REGISTRY}/${CONTAINER_NAME}:$TAG
docker tag ${REGISTRY}/${CONTAINER_NAME}:$TAG ${REGISTRY}/${CONTAINER_NAME}:latest
#remove existing images without tags
OLD=`docker images -f "reference=TESTRACK-1:5000/$CONTAINER_NAME" -f "dangling=true" -q`
if [ -n "${OLD}" ]; then
docker rmi -f ${OLD} || true
fi
# https://docs.docker.com/engine/reference/commandline/push/
#do not show push progress
docker push ${REGISTRY}/${CONTAINER_NAME}:$TAG &> push_result_tag.txt
docker push ${REGISTRY}/${CONTAINER_NAME}:latest &> push_result.txt
请注意,如果容器有权访问Docker守护进程,则只能在容器内运行此脚本。因此,您应该启动一个可以访问docker守护进程套接字的容器
-v /var/run/docker.sock:/var/run/docker.sock
可以将绑定挂载添加到本地文件系统,然后在方便的时候推送。