在 docker-compose 中挂载一个卷.它是如何完成的? [英] Mount a volume in docker-compose. How is it done?

查看:17
本文介绍了在 docker-compose 中挂载一个卷.它是如何完成的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在控制台中执行这个 cmd:

If I execute this cmd in a console:

 docker run -it --rm --link rabbit --link elasticsearch -v "$PWD"/logstash:/config-dir logstash logstash -f /config-dir/logstash.conf

它运行良好.在./logstash 文件夹中有一个logstash.conf.但是现在我正在尝试放入 docker-compose 并且同样不起作用:

It runs fine. Inside ./logstash folder there is a logstash.conf. But now I'm trying to put in a docker-compose and the same doesn't works:

  logstash:
  image: logstash:latest
  links:
    - "elasticsearch:elasticsearch"
    - "rabbit:rabbit"
  volumes:
      - $PWD/logstash:/config_dir
  command:
    - "-f /config_dir/logstash.conf"

但是我看不出这两个命令之间的区别.一些帮助?卷安装是如何完成的?还是命令不起作用?来自 logstash init 的响应是:

But I cannot see the difference between both commands. Some help? How is it volume mounting done? Or is the command that doesn't works? Response from logstash init is:

logstash_1        | {:timestamp=>"2016-07-06T15:43:06.663000+0000", :message=>"No config files found: / /config_dir/logstash.conf
Can you make sure this path is a logstash config file?", :level=>:error}
rabbitmq_logstash_1 exited with code 1

我终于通过删除命令并使用原始图像的默认命令解决了问题,但我仍然不明白问题以及如何将相同的命令传递给docker和有效,但如果它通过 docker-compose 传递,则不会.提前致谢

I finally solved the problem by removing the command and using the default command of the original image, but I still don't understand the problem and how the same command is passed to docker and works but if it is passed throught docker-compose don't. Thanks in advance

推荐答案

您的配置可能不起作用,因为您的 docker-compose 版本在创建容器时没有执行 shell 扩展.这意味着 docker compose 试图找到一个文字路径 $PWD/logstash 而不是将 $PWD 扩展到您当前的目录.更高版本的 docker compose 允许环境变量扩展.

Your config is probably not working because your version of docker-compose does not execute shell expansions while creating your container. That means that docker compose is trying to find a literal path $PWD/logstash instead of expanding $PWD to your present directory. Later versions of docker compose do allow for environment variable expansion.

Docker-compose 确实允许相对路径,通过使用./,它引用撰写文件所在的文件夹,不一定是你的pwd所以你只需要将你的撰写文件更改为:

Docker-compose does allow relative paths though, through the use of ./, which references the folder the compose file is in, not necessarily your pwd, so you just need to change your compose file to be:

volumes:
    - ./logstash:/config_dir

这篇关于在 docker-compose 中挂载一个卷.它是如何完成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆