【发布时间】:2015-06-12 14:50:38
【问题描述】:
我有一个正在修改的 Postgres Dockerfile,不幸的是,在应用修改后,Postgres 容器停止按预期工作。我想请你解释我做错了什么。
工作示例
这是我修改的 Postgres Dockerfile:
# Use ubuntu image
FROM ubuntu
# Install database
RUN apt-get update && apt-get install -y postgresql-9.3
# Switch to postgres user.
USER postgres
# Create databse and user with all privileges to the database.
RUN /etc/init.d/postgresql start && \
psql --command "CREATE DATABASE docker;" && \
psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
psql --command "GRANT ALL PRIVILEGES ON DATABASE docker TO docker;"
# Allow remote connections to the database.
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
# Add VOLUMEs to allow backup of config, logs and databases
VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
# Set the default command to run when starting the container
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"]
我是这样构建的:
docker build --tag postgres-image .
然后我创建一个容器:
docker run -d -it -p 32768:5432 --name=postgres postgres-image
我连接数据库:
psql -h localhost -p 32768 -d docker -U docker --password
第一次修改
我不需要任何卷,因为我将使用存储所有 Postgres 数据的纯数据容器。当我删除该行时:
VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
并像工作示例中那样执行所有步骤,在最后一步中传递密码后出现以下错误:
psql: FATAL: the database system is starting up
FATAL: the database system is starting up
所以问题是:为什么我需要 Dockerfile 中的 VOLUME 指令?
二次修改
此修改不包括第一个修改。两种修改都是独立的。
CMD 中使用的参数指向默认的 Postgres 数据目录和配置文件,所以我想通过将 CMD 设置为我经常用来启动 Posgres 的命令来简化它:
service postgres start
将 CMD 设置为:
CMD ["service", "postgres", "start]
并像在工作示例中那样执行所有步骤,在最后一步中传递密码后出现以下错误:
psql: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 32768?
问题是:为什么在我的主机系统上运行的命令在 Docker 容器中不起作用?
【问题讨论】:
标签: postgresql docker dockerfile