dockerdocker-composedocker-volume

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.


Solution

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