简体   繁体   English

使用持久卷启动PostgreSQL容器[docker compose]

[英]starting postgres container with persistant volume [docker compose]

I have a problem with my database container, so I want to mount an existant database(from host) to the generated container (with docker-compose). 我的数据库容器有问题,所以我想将现有的数据库(从主机)挂载到生成的容器(使用docker-compose)。 I will show you the important part of the yaml file : 我将向您展示yaml文件的重要部分:

postgres:
        image: postgres
        environment:
            POSTGRES_USER: user
            POSTGRES_PASSWORD: pass
        ports:
        - "5432:5432"
        volumes:
        - ./postgres/data:/var/lib/postgresql/data

So, when I build and turn on docker-compose, postgres container exited: 因此,当我构建并打开docker-compose时,postgres容器退出了:

在此处输入图片说明

If I remove the persistant volume, it wokrs ! 如果删除持久性卷,它将无法正常运行! I have also tested with mysql, mongodb -> same issue. 我也用mysql,mongodb->同样的问题进行了测试。

Could you light me up ? 你能照亮我吗?

Thanks. 谢谢。

I resolve this matter and I want to share it. 我解决了这个问题,我想分享。 In fact, for getting our container turns on after ' docker-compose up ' command, we should add tty and command arguments like : 实际上,为使容器在“ docker-compose up”命令后打开,我们应添加tty和命令参数,例如:

postgres:
        image: postgres
        environment:
            POSTGRES_USER: user
            POSTGRES_PASSWORD: pass
        ports:
        - "5432:5432"
        volumes:
        - ./postgres/data:/var/lib/postgresql/data
        tty: true
        command: /bin/bash

I hope that would be useful. 我希望这会有所帮助。

Easiest way is to named volumes that can also be reused across multiple services, and are easily retrieved and inspected using the docker command line... 最简单的方法是命名卷,这些卷也可以在多个服务之间重用,并且可以使用docker命令行轻松检索和检查...

  1. First create volume docker volume create db-data 首先创建卷docker volume create db-data
  2. Now modify your yml file 现在修改您的yml文件
    services
        postgres:
            image: postgres
            environment:
                POSTGRES_USER: user
                POSTGRES_PASSWORD: pass
            ports:
            - "5432:5432"
            volumes:
            - db-data:/var/lib/postgresql/data
    volumes:
       db-data:
  1. You can inspect the volume docker volume inspect db-data 您可以检查卷docker volume inspect db-data

Source: Compose file reference 来源: 撰写文件参考

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

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