简体   繁体   English

无法在 Docker 中安装卷 撰写正确

[英]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.首先,我一步一步地完成了所有这些: https://docs.docker.com/language/golang/develop/完美运行。 Then I started to try the same with my golang project.然后我开始对我的 golang 项目进行同样的尝试。 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.它不仅需要卷中的 db,还需要 'assets' 和 'creds' 目录,我能够提供这些目录与正常的 Dockerfile 和 'docker run' 命令中的 --mount 标志一起使用。 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 Go 文件可以访问/usr/data/my_folders

BTW here is Dockerfile:顺便说一句,这里是 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.然后我开始尝试制作一个 Docker-compose.yml 文件,就像在该示例的末尾一样。 It has no --mount flags but I found plenty ways to specify mount path.它没有 --mount 标志,但我找到了很多方法来指定安装路径。 I tried much more but left 3 variants of it in code bellow(2 of 3 are commented):我尝试了更多,但在下面的代码中留下了 3 个变体(注释了 3 个中的 2 个):

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'.此外,它还创建了 2 个卷:“roach”和“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:构建命令的 output 总是这样:

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? (前 2 个字符串是我的 go.files 产生的,'assets' 是我要复制的文件夹)我认为我在错误的位置:也许我复制文件夹的方式不适用于这种构建?

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:在下面的示例中,我为 db 和 app 命名了 2 个卷,只是为了提高准确性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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