Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mount a volume in docker-compose. How is it done?

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 

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" 

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 

Edit: 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

like image 617
Killrazor Avatar asked Jul 06 '16 15:07

Killrazor


People also ask

How does docker volume mount work?

The docker run -v option takes some unit of storage, either a named volume or a specific host directory, and mounts it (as in the mount(8) command) in a specific place in the container filesystem. This will hide what was originally in the image and replace it with the volume content.

Why do we need volumes in docker compose?

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker.

What is difference between mount and volume in docker?

The most notable difference between the two options is that --mount is more verbose and explicit, whereas -v is more of a shorthand for --mount . It combines all the options you pass to --mount into one field. On the surface, both commands create a PostgreSQL container and set a volume to persist data.


1 Answers

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 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 
like image 177
Will Barnwell Avatar answered Oct 06 '22 13:10

Will Barnwell