一千萬個為什麽

搜索

為什麽在`docker-compose up`運行時創建未命名的卷,並在發出`docker-compose down && docker-compose up`後執行這些操作。



在啟動Docker撰寫應用程序時,它會在第一次創建命名卷,然後在每次啟動時創建額外的卷。這是正常行為,因為它會創建增量卷,或者是我的docker-compose文件被破壞了嗎?另外,在運行 docker volume prune 之後,它刪除了兩個未命名的卷

這是兩次啟動後的輸出:

DRIVER              VOLUME NAME
local               2b33c81e6e955ff36061f4120b7181c7efc7aebded2f87eaa7290027f1e7a725
local               74301eadc75018d6f7da76aec44bcf743e03261492cfcc33211941684a570030
local               c42b1e2c17342c52b4b9e90f4d4b7fa24ccb30de5479e65cf4366e2373dfb0bf
local               dcfa63dbf99362fc92fe3b981f50abebfe1bba03063d4dd9dd790f4c058817cf
local               dockerinfluxdbgrafana_grafana-data
local               dockerinfluxdbgrafana_influxdb-data

並參考我的docker-compose文件

version: "3"
services:
  influxdb:
    image: influxdb:latest
    container_name: influxdb
    ports:
      - "8086:8086"
    env_file:
      - 'env.influxdb'
    networks:
      - backend
    volumes:
      - influxdb-data:/var/lib/influxdb

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"
    env_file:
      - 'env.grafana'
      - 'secrets.grafana'
    networks:
      - backend
    volumes:
      - grafana-data:/var/lib/grafana

networks:
  backend:

volumes:
  influxdb-data:
  grafana-data:

重現步驟

docker-compose up -d && docker-compose down && docker-compose up -d && docker-compose down && docker-compose up -d && docker-compose down

轉載註明原文: 為什麽在`docker-compose up`運行時創建未命名的卷,並在發出`docker-compose down && docker-compose up`後執行這些操作。

一共有 2 個回答:

研究表明,匿名卷是由 influxdb 創建的和 grafana/grafana

匿名卷在第一個時沒有明確的名稱   安裝到一個容器中,所以Docker給他們一個隨機的名字   保證在給定的Docker主機中是唯一的。除了名字,   命名和匿名卷的行為方式相同。

結果

version: "3"
services:
  influxdb:
    image: influxdb:latest
    container_name: influxdb
    ports:
      - "8086:8086"
    #env_file:
    #  - 'env.influxdb'
    networks:
      - backend
    volumes:
      - influxdb-data:/var/lib/influxdb

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"
    #env_file:
    #  - 'env.grafana'
    #  - 'secrets.grafana'
    networks:
      - backend
    volumes:
      - grafana-data:/var/lib/grafana

networks:
  backend:

volumes:
  influxdb-data:
  grafana-data:

結果是:

DRIVER              VOLUME NAME
local               604a07040367512b09c618c6dcc71a7f55390c9c23de6ab08be7466414ed62da
local               7f4b630073b31b6e772d3edef6da81b48643525edfc34281ea13fbd6b86ec270
local               devopsstackexchange_grafana-data
local               devopsstackexchange_influxdb-data

每當 docker-compose down 和subsequenlty up 運行時,匿名卷的數量就會增加一倍。

如果nginx將被取代influxdb和grafana,會發生什麽?

DRIVER              VOLUME NAME
local               devopsstackexchange_grafana-data
local               devopsstackexchange_influxdb-data

和 after docker-compose down && docker-compose up -d?

DRIVER              VOLUME NAME
local               devopsstackexchange_grafana-data
local               devopsstackexchange_influxdb-data

它看起來像某些圖像創建了額外的匿名卷。讓我們用nginx替換influxdb並使用grafana。

DRIVER              VOLUME NAME
local               15b80416ab06abb629d9f634a0feff08f7c560f31d614b9b430855c16cdb75c7
local               205a6f19cbf992c95b2e3be9f2fb1ca9ecec35fce550d0b7a4b9f32b0ef163b1
local               474108f5b7b14fba92a3e5a980f3bf851388b2ee25d7417df5c42d9f176e084b
local               5830a31a470ec8a42ddae7a37bb50487f3f36360318b2f9f5301b338507782b4
local               9f00868a2fec0cfc0d34dc12d0879d39487a13128863722f400ad4c47df2d340
local               devopsstackexchange_grafana-data
local               devopsstackexchange_influxdb-data
local               f47b1b7bbec8e50b32a7c39704c7c218165b284298d852313fa24bc7cbe6acc5

每次碼頭工作組合再次運行,三個匿名卷由 grafana/grafana 碼頭圖像創建。讓我們用nginx替換influxdb並將grafana恢復為nginx:

DRIVER              VOLUME NAME
local               devopsstackexchange_grafana-data
local               devopsstackexchange_influxdb-data

如果重新啟動,它仍然是兩個。

看起來格拉法納正在造成這個問題。

為什麽每次grafana/grafana重新啟動時創建三個新的匿名卷?

The grafana/grafana dockerfile indicates that three anonymous volumes will be created:

 VOLUME [“/ var/lib/grafana”,“/ var/log/grafana”,“/ etc/grafana”]
</代碼> 

https://docs.docker.com/engine/reference/builder/#volume

FROM ubuntu
RUN mkdir /myvol
RUN echo "hello world" > /myvol/greeting
VOLUME /myvol

This Dockerfile results in an image that causes docker run, to create a new mount point at /myvol 和 copy the greeting file into the newly created volume.

docker volume inspect returned grafana data when the path that was returned was checked of two out of three anonymous volumes:

sudo ls /var/lib/docker/volumes/a0ecd00df8fc68ef36e777c7bf9ec5a496ee30e313b86889487501a53fa2e28e/_data
grafana.ini  ldap.toml

sudo ls /var/lib/docker/volumes/34cfafd4603dbc7e71a83e2520f978c8307b084143b3192de65a1995dc1d2f86/_data
grafana.db  plugins

VOLUME 指令檢查在 Dockerfile 中定義的卷。

If you don't want volume with random names to be automatically created, you can provide a host path to be bind-mounted to the volume, check the -v or --mount options, https://docs.docker.com/engine/admin/volumes/volumes/#choose-the--v-or-mount-flag

例如:

-v /srv/lib/grafana:/var/lib/grafana