我在云服务器上使用命令“docker-compose up”运行算法。其中一项服务 (productionbackend) 在其容器 (./logs/Log_< UTCTIMENOW >.txt) 内的某个位置写入日志作为其执行的一部分。
我知道我可以在服务运行时使用“docker logs”命令检查日志,但是一旦使用“docker-compose down”停止它,日志就会永远丢失(因为容器被销毁)。
所以我想我想将一个卷“挂载”到我的 docker compose 文件,并将该 log.txt 文件的输出重定向到容器外的 ~ 目录。这是我的目录树的样子:
我在 docker compose 文件中尝试了各种组合,但都没有成功。
例如添加以下失败
volumes:
- type: volume # Also tried `bind` type and other syntaxes
- ./logs:/productionbackend/logs
.
├── docker-compose.yaml #THIS IS THE DOCKER-COMPOSE FILE STARTING MY SERVICES
├── productionfrontend
│ ├── Dockerfile
│ ├── app
│ │ ├── database.py
│ │ ├── main.py
│ │ └── requirements.txt
│ └── docker-compose.yaml
└── productionbackend
├── Dockerfile
├── backend.py
├── logs
│ ├── Log_03_Jun_14.18.19_GMT.txt
├── requirements.txt
├── settings
│ └── somefile.py
........
谢谢。
请您参考如下方法:
下一步:
volumes:
- type: volume # Also tried `bind` type and other syntaxes
- ./logs:/productionbackend/logs
不知道是你的错别字还是其他的,这个语法好像很奇怪,看official grammar
接下来我举一个最简单的例子,可以供大家引用:
docker-compose.yaml:
version: '3'
services:
backend:
image: alpine
container_name: my_try
volumes:
- ./log_on_host:/productionbackend/logs
command: sh -c 'echo "hi" > /productionbackend/logs/my_log.txt'
接下来是执行历史:
shubuntu1@shubuntu1:~/abc$ ls
docker-compose.yaml
shubuntu1@shubuntu1:~/abc$ docker-compose up -d
Creating network "abc_default" with the default driver
Creating my_try ... done
shubuntu1@shubuntu1:~/abc$ docker-compose down
Removing my_try ... done
Removing network abc_default
shubuntu1@shubuntu1:~/abc$ ls
docker-compose.yaml log_on_host
shubuntu1@shubuntu1:~/abc$ cat log_on_host/my_log.txt
hi
从上面可以看出,虽然容器my_try已经被销毁,但是我们仍然可以在host上看到内容为hi的日志my_log.txt。您需要像上面的最小示例一样修改您的,仅供引用。
