'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 1:[1]

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

version: '3.9'
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).

Solution 2:[2]

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 )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Maze
Solution 2 Siwei