考虑这样的docker-compose.yml
文件:
version: '3'
services:
test:
image: ubuntu:18.04
tty: true
我可以通过运行在后台启动服务 docker-compose up -d
现在,我可以使用以下命令在此容器上运行命令 docker-compose exec
例如:
docker-compose exec test ls -lrt | head -3
当我运行上面的命令时,我在终端上得到了输出
total 64
drwxr-xr-x 8 root root 4096 May 23 2017 lib
drwxr-xr-x 2 root root 4096 Apr 24 2018 home
现在我想要的是能够在后台运行上面的命令
docker-compose exec -d test ls -lrt | head -3
该命令将在后台运行,这很好。但是,如何访问以上命令的输出?
您可以将输出重定向到容器内的文件中,然后再输出该文件的内容,例如:
# execute command and redirect to output.log in container
docker-compose exec -d test sh -c 'ls -lrt | head -3 > /tmp/output.log'
# retrieve output content
docker-compose exec test cat /tmp/output.log
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句