在docker-compose中挂载一个卷。怎么做? [英] Mount a volume in docker-compose. How is it done?

查看:105
本文介绍了在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初始化的响应是:

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\nCan you make sure this path is a logstash config file?", :level=>:error}
rabbitmq_logstash_1 exited with code 1

编辑:我终于通过删除命令并使用原始图像的默认命令解决了该问题,但我仍然不了解问题以及如何将同一命令传递给docker并工作,但是如果它是通过docker-compose not传递的。
预先感谢

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天全站免登陆