我有一个工作的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 instraction中使用的参数指向默认的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容器中不起作用?
答案 0 :(得分:1)
我不确定第一个问题。 可能是Postgres不喜欢在UFS上运行。
第二个问题是容器在主进程结束时将退出。所以命令" service postgres start"运行,在后台启动Postgres然后立即退出并且容器停止。第一个版本有效,因为Postgres一直在前台运行。
但你为什么要这样做?为什么不使用official Postgres image?