简体   繁体   中英

Can't mount volume in Docker Compose right

Firstly, I went all that step by step: https://docs.docker.com/language/golang/develop/ Works perfectly. Then I started to try the same with my golang project. It requires not only db in a volume but also 'assets' and 'creds' directories which I was able to provide working with normal Dockerfile and --mount flag in 'docker run' comand. So my schema was:

  • Create a volume 'roach'.
  • Create a temp container for copying folders.
    docker container create --name temp -v roach:/data busybox \
    docker cp assets/ temp:/data \
    docker rm temp
  • Run my container with
docker run -it --rm \
  --mount 'type=volume,src=roach,dst=/usr/data' \
  --network mynet \
  --name postgres-server \
  -p 80:8080 \
  -e PGUSER=totoro \
  -e PGPASSWORD=myfriend \
  -e PGHOST=db \
  -e PGPORT=26257 \
  -e PGDATABASE=mydb \
 postgres-server
  • Go files have acces to /usr/data/my_folders

BTW here is Dockerfile:

# syntax=docker/dockerfile:1

FROM golang:1.18-buster AS build

WORKDIR /app

COPY go.mod .
RUN go mod download

COPY . .

RUN go mod tidy

RUN go build -o /t main/main.go main/inst_list.go


## Deploy

FROM gcr.io/distroless/base-debian10

ENV GO111MODULE=on
ENV GOOGLE_APPLICATION_CREDENTIALS='/usr/data/credentials/creds.json'

WORKDIR /

COPY --from=build /t /t

EXPOSE 8080

USER root:root


ENTRYPOINT ["/t"]

================================================================

Then I started to try to make a Docker-compose.yml file like in the end of that example. It has no --mount flags but I found plenty ways to specify mount path. I tried much more but left 3 variants of it in code bellow(2 of 3 are commented):

version: '3.8'

services:
  docker-t-roach:
    depends_on: 
      - roach
    build:
      context: .
    container_name: postgres-server
    hostname: postgres-server
    networks:
      - mynet
    ports:
      - 80:8080
    environment:
      - PGUSER=${PGUSER:-totoro}
      - PGPASSWORD=${PGPASSWORD:?database password not set}
      - PGHOST=${PGHOST:-db}
      - PGPORT=${PGPORT:-26257}
      - PGDATABASE=${PGDATABASE-mydb}
    deploy:
      restart_policy:
        condition: on-failure
  roach:
    image: cockroachdb/cockroach:latest-v20.1
    container_name: roach
    hostname: db
    networks:
      - mynet
    ports:
      - 26257:26257
      - 8080:8080
    volumes:
#      - type: volume
#        source: roach
#        target: /usr/data
      
      - roach:/usr/data
        
#     - "${PWD}/cockroach-data/roach:/usr/data"
    command: start-single-node --insecure

volumes:
  roach:


networks:
  mynet:
    driver: bridge

and it still doesn't work. Moreover it creates 2 Volumes: 'roach' and 'WORKDIRNAME_roach'. I actually tried to copy my folders to both of those. It's not working. The output of build command is alwaysl like that:

postgres-server   | STARTED AT
postgres-server   | Sep  4 10:43:10
postgres-server   | lstat /usr/data/assets/current_batch: no such file or directory
postgres-server   | 2022/09/04 10:43:10 lstat /usr/data/assets/current_batch: no such file or directory

(first 2 strings are produced my my go.files, 'assets' is the folder I'm copying) I think that I'm seaking in the wrong place: maybe the way I copy folders doesn't work with this kind of build?

UPDATE: At the same time command

docker run -it --rm -v roach:/data ubuntu ls /data/usr 

showes that my folders are there. But container is in kind of cycle that doesn't let him see them.

Mihai is tried to help but I didn't understand what he meant. He actually meant that I had to add volume to my app service. I did it now and it works. In example bellow I named 2 volumes for db and app different just for better accuracy:

version: '3.8'

services:
  docker-parser:
    depends_on: 
      - roach
    build:
      context: .
    container_name: parser
    hostname: parser
    networks:
      - mynet
    ports:
      - 80:8080
    volumes:
      - assets:/data
    environment:
      - PGUSER=${PGUSER:-totoro}
      - PGPASSWORD=${PGPASSWORD:?database password not set}
      - PGHOST=${PGHOST:-db}
      - PGPORT=${PGPORT:-26257}
      - PGDATABASE=${PGDATABASE-mydb}
    deploy:
      restart_policy:
        condition: on-failure
  roach:
    image: cockroachdb/cockroach:latest-v20.1
    container_name: roach
    hostname: db
    networks:
      - mynet
    ports:
      - 26257:26257
      - 8080:8080
    volumes:
      - roach:/db
    command: start-single-node --insecure

volumes:
  assets:
  roach:

networks:
  mynet:
    driver: bridge

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM