Docker卷不保留数据 [英] Docker volume does not persist data

查看:94
本文介绍了Docker卷不保留数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的docker文件:

Here is my docker file:

FROM ubuntu:14.04

RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list

RUN apt-get update && apt-get -y -q install python-software-properties software-properties-common \
    && apt-get -y -q install postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3

USER postgres
RUN /etc/init.d/postgresql start \
    && psql --command "CREATE USER pguser WITH SUPERUSER PASSWORD 'pguser';" \
    && createdb -O pguser pgdb
USER root
RUN echo "host all  all    0.0.0.0/0  md5" >> /etc/postgresql/9.3/main/pg_hba.conf
RUN echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf
EXPOSE 5432

RUN mkdir -p /var/run/postgresql && chown -R postgres /var/run/postgresql
VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
USER postgres

CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]

这是我所做的...


  1. 我构建docker映像:

  1. I build the docker image:

docker build --rm=true -t my_image/postgresql:9.3


  • 然后,在当前目录中创建一个名为 data 的新目录,并运行以下命令:

  • Then, I create a new directory called data in my current directory and ran the following command:

    docker run -i -t -v="data:/data" -p 5432:5432 my_image/postgresql:9.3
    


  • 我打开另一个终端,并通过运行以下命令进入postgres shell:

  • I open another terminal and enter the postgres shell by running:

    psql -h my_docker_ip -p 5432 -U pguser -W pgdb
    

    ,然后创建一个表:

    pgdb=# create table test (test_id bigserial primary key);
    


  • 我使用 \dt 并退出postgres shell

  • I verify the table exist using \dt and exit the postgres shell

    我终止docker进程并重新运行以下命令:

    I terminate the docker process and rerun the following:

    docker run -i -t -v="data:/data" -p 5432:5432 my_image/postgresql:9.3
    


  • 我再次进入posgrest外壳并运行 \dt


    • 没有桌子。

    • data 目录中没有文件。

    • there are no tables.
    • in the data directory there are no files.

    我肯定做错了,因为我假设我创建的表将持久存在。有人可以指出我的错误吗?

    I must be doing something wrong since I am assuming that the table I created will persist. Can someone point out my mistake?

    推荐答案

    根据您的评论:

    数据仍然存在,但是我仍然无法在主机./data目录中找到持久数据。

    the data persisted, but I still can't find the persist data in my host ./data directory

    并运行以下命令:

    docker run -i -t -v="data:/data" -p 5432:5432 my_image/postgresql:9.3
    

    您似乎混淆了命名卷和主机卷。当您给卷命名时没有路径,例如 data ,就使用命名卷。命名卷使用泊坞窗驱动程序(通常是本地)以可重用的给定名称存储数据。它的优点是在 docker volume ls 中列出,并被初始化为安装位置的映像内容。

    You appear to be confusing a named volume and a host volume. The named volume is used when you give the volume a name without a path, like data. The named volume stores the data using the docker driver (typically local) under a given name that you can reuse. It has the advantage of being listed in docker volume ls, and being initialized to the content of the image at the mounted location.

    如果您包含完整路径,例如 / home / username / data ,它将从Docker主机挂载目录而不使用命名卷。最大的缺点是您没有使用图像中的内容初始化目录,并且可能会遇到权限问题,其中容器进程的uid与您在主机上使用的uid不匹配。

    If you include a full path, like /home/username/data that would mount the directory from the docker host instead of using the named volume. The biggest disadvantage is that you don't get the directory initialized with the contents from the image, and you will likely encounter permission issues where the uid of the container process won't match the uid you use on your host.

    有关更多详细信息,请参见 https:// docs .docker.com / engine / tutorials / dockervolumes /

    For more details, see https://docs.docker.com/engine/tutorials/dockervolumes/

    这篇关于Docker卷不保留数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆