Docker Community Forums

Share and learn in the Docker community.

--volume via docker run VS docker-compose

docker

(devmtl) #1

Hi,

I’m using a docker-compose up -d to launch a ghost blog. I use two volumes definitions. It’s working as expected, life is good.

version: "2"

services:

  ghost:
    container_name: ghost
    environment:
      - NODE_ENV=production
    volumes:
      - ./ghost/volumes/content:/var/lib/ghost
      - ./ghost/volumes/config.js:/var/lib/ghost/config.js
    ptimof/ghost

When I use docker run to accomplish the same task, the volume is not working as it should. The file config.js is seen within the container as a folder !! Any ideas?

docker run -it \
    --volume /ghost/volumes/content:/var/lib/ghost \
    --volume /ghost/volumes/config.js:/var/lib/ghost/config.js \
    --env NODE_ENV=production \
    --name ghost \
    ptimof/ghost

BTW, the point of switching from compose to to docker run is the use Swarm via docker service create …


(Nathan Le Claire) #2

Why are you storing these files at path /ghost and not in /home/you or similar? Is this Docker for Mac? Native Linux? What does the output of the volumes section in docker inspect for the created containers say for the ones created by Compose and the ones created by docker run?


(devmtl) #3

First thank you very much taking time on this. I didn’t give you enough details previously. Sorry for this. Here you should be able to see the whole picture.

Why are you storing these files at path /ghost and not in /home/you or similar? My ghost directory is side by side with my docker-compose file

$ pwd

/home/myuser/git-repo-projectname/_apps/userid/docker-compose.yml

$ tree

.
├── docker-compose.yml
├── ghost
│   ├── copy
│   │   ├── entrypoint.sh
│   │   └── index.js
│   └── volumes
│       ├── config.js
│       └── content

Is this Docker for Mac? NO
Native Linux? YES

What does the output of the volumes section in docker inspect for the created containers say for the ones created by Compose and the ones created by docker run?

docker inspect after (docker run …)

docker inspect after (docker-compose up -d)

This is good: https://gist.github.com/pascalandy/a3b69ea7d14a61d9cc6b98b153f4ed6e#file-inspect-docker-compose-L124

This is wrong: https://gist.github.com/pascalandy/ec75fecb73fafb5546b5e6ce68366987#file-inspect-docker-run-L123

I don’t understand what is wrong with the way I declare my volume attributes in docker run :confused:

My reflex was to declare the volume this way (like docker-compose) :

docker run -it \
    --volume ./ghost/volumes/content:/var/lib/ghost \
    --volume ./ghost/volumes/config.js:/var/lib/ghost/config.js \
    --env NODE_ENV=production \
    --name ghost \
    ptimof/ghost

but I’m not allowed:

docker: Error response from daemon: create ./ghost/volumes/content: “./ghost/volumes/content” includes invalid characters for a local volume name, only “[a-zA-Z0-9][a-zA-Z0-9_.-]” are allowed.
See ‘docker run --help’.

This is why I’m writing this:

docker run -it \
    --volume /ghost/volumes/content:/var/lib/ghost \
    --volume /ghost/volumes/config.js:/var/lib/ghost/config.js \
    --env NODE_ENV=production \
    --name ghost \
    ptimof/ghost

(Nathan Le Claire) #4

run wants absolute paths. You’re trying to use relative path here but starting them with / (I’m not sure why Compose lets you get away with this – it should be ./ghost/volumes/content if I understand correctly). Since that directory doesn’t exist on the host docker will go and create it then bind mount that empty directory into the container.

If you’re going to use docker run you have to do something like -v $(pwd)/ghost/volumes/config.js:/var/lib/ghost/config.js or (the expansion of that subshell) -v /home/myuser/git-repo-projectname/_apps/userid/ghost/volumes/config.js:/var/lib/ghost/config.js


(devmtl) #5

You’re the man !!

docker run -it \
    -v $(pwd)/ghost/volumes/content:/var/lib/ghost \
    -v $(pwd)/ghost/volumes/config.js:/var/lib/ghost/config.js \
    --env NODE_ENV=production \
    --name ghost \
    ptimof/ghost

Does docker swarm service needs relative path too? I’m almost ready to do the big switch.

docker service create -it \
    -v $(pwd)/ghost/volumes/content:/var/lib/ghost \
    -v $(pwd)/ghost/volumes/config.js:/var/lib/ghost/config.js \
    --env NODE_ENV=production \
    --name ghost \
    ptimof/ghost

(Nathan Le Claire) #6

docker service doesn’t accept -v flag, it accepts --mount which has slightly different syntax (you want mount type of bind).


(devmtl) #7

Ohh, yes I see now.

Do you know if there is some cheatsheet that translate the “same” instructions for:

  1. run
  2. compose
  3. service

Cheers!