Connor Low
Connor Low

Reputation: 7236

Undefined volume with Docker Compose

I wanted to translate this docker CLI command (from smallstep/step-ca) into a docker-compose.yml file to run with docker compose (version 2):

docker run -d -v step:/home/step \
    -p 9000:9000 \
    -e "DOCKER_STEPCA_INIT_NAME=Smallstep" \
    -e "DOCKER_STEPCA_INIT_DNS_NAMES=localhost,$(hostname -f)" \
    smallstep/step-ca

This command successfully starts the container.

Here is the compose file I "composed":

version: "3.9"
services:
  ca:
    image: smallstep/step-ca
    volumes:
      - "step:/home/step"
    environment:
      - DOCKER_STEPCA_INIT_NAME=Smallstep
      - DOCKER_STEPCA_INIT_DNS_NAMES=localhost,ubuntu
    ports:
      - "9000:9000"

When I run docker compose up (again, using v2 here), I get this error:

service "ca" refers to undefined volume step: invalid compose project

Is this the right way to go about this? I'm thinking I missed an extra step with volume creation in docker compose projects, but I am not sure what that would be, or if this is even a valid use case.

Upvotes: 62

Views: 84497

Answers (3)

David Maze
David Maze

Reputation: 159781

The Compose file also has a top-level volumes: block and you need to declare volumes there.

version: '3.8'
services:
  ca:
    volumes:
      - "step:/home/step"
    et: cetera
volumes:   # add this section
  step:    # does not need anything underneath this

There are additional options possible, but you do not usually need to specify these unless you need to reuse a preexisting Docker named volume or you need non-standard Linux mount options (the linked documentation gives an example of an NFS-mount volume, for example).

Upvotes: 77

otterrisk
otterrisk

Reputation: 730

Citing the Compose specification:

To avoid ambiguities with named volumes, relative paths SHOULD always begin with . or ...

So it should be enough to make your VOLUME's host path relative:

services:
  ca:
    volumes:
      - ./step:/home/step

If you don't intend to share the step volume with other containers, you don't need to define it in the top-level volumes key:

If the mount is a host path and only used by a single service, it MAY be declared as part of the service definition instead of the top-level volumes key.

Upvotes: 36

Siwei
Siwei

Reputation: 21567

it seems that docker-compose don't know the "volume" you created via command: sudo docker volume create my_xx_volume

so ,just manually mkdir to create a folder and chmod 777 <my_folder>, then your mysql docker will use it very well.

( in production env, don't use chmod but chown to change the file permission )

Upvotes: 0

Related Questions