Undefined volume with Docker Compose
Asked Answered
H

3

52

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.

Heatherheatherly answered 6/11, 2021 at 19:18 Comment(0)
G
72

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).

Gringo answered 6/11, 2021 at 23:50 Comment(1)
As is, this compose file will cause a volume named ca_step to be created, while OP's CLI example accessed a volume named step. This is no problem if the volume needs to be created anyway and you don't care for the exact name (or use name: "step"). However, if you want to access an already existing volume, you need to add external: true underneath the last line.Turner
E
27

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.

Eachelle answered 16/12, 2022 at 13:46 Comment(1)
This is the wayBelie
G
0

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 )

Gild answered 21/4, 2022 at 5:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.