13

I'm using the github.com/samalba/dockerclient and want to create a Container. So, the method is CreateContainer, which needs a ContainerConfig.

The ContainerConfig is a struct. And there's a field Volumes, the type of which is type map[string] struct{}.

I know that I could create such a map with make(map[string]struct{})

But how do I assign values to the map?

1
  • Read up on map composite literals. E.g. cc := dockerclient.ContainerConfig{Volumes: map[string]struct{}{ "key1": struct{}{}, "key2": struct{}{} } }. E.g.: play.golang.org/p/-ldjIDxVzN. You could also use empty := struct{}{} to make it more readable. Commented Aug 1, 2015 at 18:10

3 Answers 3

17
cc := &dockerclient.ContainerConfig{
    // ...
    Volumes: map[string]struct{}{
        "foo": struct{}{},
        "bar": struct{}{},
        // ...
    },
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, do you know for the docker client how to use not empty struct struct{}{} but with actual values? I need an equivalent of -v /host/directory:/container/directory command line option... let's say I want to mount /mnt/foo directory from host to /mnt/foo on container... How can I do that?
3

Volumes: map[string]struct{}{ "dir1": struct{}{}, "dir2": struct{}{}, },

Maps only the folder from localhost to docker container. No contents will be mapped.

1 Comment

Hey, do you know hot to actually map content with it?.. How to put anything instead of just struct{}{}? To map dir1 to dir1 on host for example.
0

In Go 1.19, syntax can be simplified by removing redundant struct{}:

cc := &dockerclient.ContainerConfig{
    // ...
    Volumes: map[string]struct{}{
        "foo": {},
        "bar": {},
        // ...
    },
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.