Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to specify volumes? #265

Closed
ekkis opened this issue Jul 6, 2016 · 10 comments
Closed

how to specify volumes? #265

ekkis opened this issue Jul 6, 2016 · 10 comments
Labels

Comments

@ekkis
Copy link

ekkis commented Jul 6, 2016

the description for the Volumes key of the options passed to /containers/create as documented here: https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/ indicates it should receive:

"An object mapping mount point paths (strings) inside the container to empty objects."

what? empty objects? what does that mean? I don't see from the source of dockrode how to make the call. if I currently do this in the CLI: -v ~/db:/data, would I:

Volumes: {'~/db': '/data'}

or...

Volumes: {'~/db': { ...what?... }}

there are no examples for guidance. how do I do this?

@apocas
Copy link
Owner

apocas commented Jul 6, 2016

@apocas apocas closed this as completed Jul 6, 2016
@apocas apocas added the question label Jul 6, 2016
@ekkis
Copy link
Author

ekkis commented Jul 6, 2016

apocas, thanks for replying but I'm afraid that reading the test doesn't make this any clearer for me. the one line that perhaps would be relevant is expect(info.Name).to.equal(testVolume.Name); but that tells me that info (perhaps the value passed into Volumes:?) should contain a Name attribute... so... Volumes: {'~/db': {Name: ???}} - help?

@joshrivers
Copy link

For the next person who hits this on search:

Volumes (in this context) are non-copy-on-write directories inside the container. What we want here are 'binds':

var options = { Binds: ['/var/run/docker.sock:/var/run/docker.sock', '/opt/app:/home/mysource'] }

@wyckster
Copy link

I still don't get it. From the command line, I can enter a -v argument like this and it works.

docker run -v myvolume:/mypath     ...

...where myvolume is a volume I created with docker create volume myvolume. And I'm expecting it to get mounted at /mypath within the container.

So, how do I accomplish the exact same thing as above in dockerode?

What do I need to specify for Volumes? What do I need to specify for HostConfig.Binds?

@EdByrnee
Copy link

EdByrnee commented Mar 7, 2018

@wyckster did you manage to figure this out?

@wyckster
Copy link

@EdByrnee The confusion was about finding documentation for the schema of the parameter object that is passed to createContainer. And how to specify the volumes / binds.

docker.createContainer({
  Volumes: { /* Here? */ },
  HostConfig: {
    Binds: [ /* Here ? */ ]
  },
  Binds: [ /* or Here? */ ]
})

If I recall correctly the solution is that you don't specify HostConfig.Binds, you just specify Binds.

Incidentally, I switched to calling out to docker-compose externally. Sadly I dropped dockerode entirely. :(

But Docker-compose is proving to be more useful anyway.

@DonMartin76
Copy link

I can add to this: If you don't need any HostConfig options, it is fine to use Binds at the root of the options. If you specify other options inside HostConfig, you must move Binds inside HostConfig, like this:

    const createOptions = {
        Tty: true,
        ExposedPorts: { '3333/tcp': {} },
        PortBindings: { '3333/tcp': [{ 'HostPort': '3333' }] },
        HostConfig: {
            AutoRemove: true,
            Binds: [
                `${configDir}:/var/tmp/whatever`
            ]
        }
    }

This works, at least for now.

@Tumao727
Copy link

@DonMartin76 I followed your solution but not work now :( By the way, isn't PortBindings a property of HostConfig? Can we just put it at the root of the options?

@devkat
Copy link

devkat commented May 19, 2022

For the record, this works for me:

{
  Image: '…',
  name: '…',
  Volumes: { '/data': {} },
  HostConfig: {
    Mounts: [
      {
        Type: 'volume',
        Source: 'my-volume-name',
        Target: '/data',
        ReadOnly: false,
      },
    ],
  },
};

@PululuK
Copy link

PululuK commented Nov 14, 2022

Thanks @devkat @DonMartin76

the both work for me, but I finally prefer @devkat proposition because in my case I need to set dynamically the configs for the bind, for example enable readonly or not, set consistency, mount type etc etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants