我正在使用官方 Postgres Docker 映像来尝试自定义其配置。为此,我使用命令sed进行更改max_connections,例如:
sed
max_connections
sed -i -e"s/^max_connections = 100.*$/max_connections = 1000/" /var/lib/postgresql/data/postgresql.conf
我尝试了两种方法来应用此配置。第一种是将命令添加到脚本并将其复制到初始化文件夹“/docker-entrypoint- initdb.d”中。第二种方法是使用“RUN”命令直接在我的 Dockerfile 中运行它们(此方法适用于配置文件“/etc/postgres/…”的路径不同的非官方 Postgresql 映像)。在这两种情况下,更改都失败了,因为缺少配置文件(我认为它尚未创建)。
我应该如何更改配置?
编辑1:
这是用于创建映像的 Dockerfile:
# Database (http://www.cs3c.ma/) FROM postgres:9.4 MAINTAINER Sabbane <contact@cs3c.ma> ENV TERM=xterm RUN apt-get update RUN apt-get install -y nano ADD scripts /scripts # ADD scripts/setup-my-schema.sh /docker-entrypoint-initdb.d/ # Allow connections from anywhere. RUN sed -i -e"s/^#listen_addresses =.*$/listen_addresses = '*'/" /var/lib/postgresql/data/postgresql.conf RUN echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf # Configure logs RUN sed -i -e"s/^#logging_collector = off.*$/logging_collector = on/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#log_directory = 'pg_log'.*$/log_directory = '\/var\/log\/postgresql'/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#log_filename = 'postgresql-\%Y-\%m-\%d_\%H\%M\%S.log'.*$/log_filename = 'postgresql_\%a.log'/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#log_file_mode = 0600.*$/log_file_mode = 0644/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#log_truncate_on_rotation = off.*$/log_truncate_on_rotation = on/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#log_rotation_age = 1d.*$/log_rotation_age = 1d/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#log_min_duration_statement = -1.*$/log_min_duration_statement = 0/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#log_checkpoints = off.*$/log_checkpoints = on/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#log_connections = off.*$/log_connections = on/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#log_disconnections = off.*$/log_disconnections = on/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^log_line_prefix = '\%t \[\%p-\%l\] \%q\%u@\%d '.*$/log_line_prefix = '\%t \[\%p\]: \[\%l-1\] user=\%u,db=\%d'/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#log_lock_waits = off.*$/log_lock_waits = on/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#log_temp_files = -1.*$/log_temp_files = 0/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#statement_timeout = 0.*$/statement_timeout = 1800000 # in milliseconds, 0 is disabled (current 30min)/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^lc_messages = 'en_US.UTF-8'.*$/lc_messages = 'C'/" /var/lib/postgresql/data/postgresql.conf # Performance Tuning RUN sed -i -e"s/^max_connections = 100.*$/max_connections = 1000/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^shared_buffers =.*$/shared_buffers = 16GB/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#effective_cache_size = 128MB.*$/effective_cache_size = 48GB/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#work_mem = 1MB.*$/work_mem = 16MB/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#maintenance_work_mem = 16MB.*$/maintenance_work_mem = 2GB/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#checkpoint_segments = .*$/checkpoint_segments = 32/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#checkpoint_completion_target = 0.5.*$/checkpoint_completion_target = 0.7/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#wal_buffers =.*$/wal_buffers = 16MB/" /var/lib/postgresql/data/postgresql.conf RUN sed -i -e"s/^#default_statistics_target = 100.*$/default_statistics_target = 100/" /var/lib/postgresql/data/postgresql.conf VOLUME ["/var/lib/postgresql/data", "/var/log/postgresql"] CMD ["postgres"]
使用此 Dockerfile,构建过程会显示错误:sed: can't read /var/lib/postgresql/data/postgresql.conf: No such file or directory
sed: can't read /var/lib/postgresql/data/postgresql.conf: No such file or directory
postgres:9.4您继承的图像在/var/lib/postgresql/data. 这实质上意味着您不能将任何文件复制到图像中的该路径;更改将被丢弃。
postgres:9.4
/var/lib/postgresql/data
你有几个选择:
您可以在运行时将自己的配置文件添加为卷,使用docker run -v postgresql.conf:/var/lib/postgresql/data/postgresql.conf .... 但是,我不确定这将如何与现有卷交互。
docker run -v postgresql.conf:/var/lib/postgresql/data/postgresql.conf ...
您可以在容器启动时复制文件。为此,请将您的文件复制到不在卷下方的位置的构建中,然后从entrypointor调用脚本,该脚本cmd会将文件复制到正确的位置并启动 Postgres。
entrypoint
cmd
克隆 Postgres 官方镜像后面的项目并编辑 Dockerfile 以在声明 VOLUME 之前添加您自己的配置文件(在运行时自动复制在 VOLUME 指令之前添加的任何内容)。
在 docker-compose 文件中传递命令选项中的所有配置更改
像这样:
services: postgres: ... command: - "postgres" - "-c" - "max_connections=1000" - "-c" - "shared_buffers=3GB" - "-c" ...