PostgreSQL docker镜像构建错误 [英] PostgreSQL docker image build errors
问题描述
我正在从源代码创建用于Postgresql安装的docker映像。这是我根据 postgresql文档创建的Dockerfile。 :
I am creating a docker image for postgresql installation from sources. Here is my Dockerfile which I created according to the postgresql documentation:
FROM ubuntu
RUN apt-get update && apt-get install gcc zlib1g-dev libreadline6-dev apt-utils make -y
RUN mkdir -p /tmp/downloads
ADD https://ftp.postgresql.org/pub/source/v9.6.6/postgresql-9.6.6.tar.gz /tmp/downloads
RUN cd /tmp/downloads && tar -zxf postgresql-9.6.6.tar.gz
RUN cd /tmp/downloads/postgresql-9.6.6 && make configure
RUN cd /tmp/downloads/postgresql-9.6.6 && ./configure
RUN cd /tmp/downloads/postgresql-9.6.6 && make
RUN cd /tmp/downloads/postgresql-9.6.6 && su
RUN cd /tmp/downloads/postgresql-9.6.6 && make install
RUN cd /tmp/downloads/postgresql-9.6.6 && adduser postgres
RUN cd /tmp/downloads/postgresql-9.6.6 && mkdir /usr/local/pgsql/data
RUN cd /tmp/downloads/postgresql-9.6.6 && chown postgres /usr/local/pgsql/data
RUN cd /tmp/downloads/postgresql-9.6.6 && su postgres
RUN cd /tmp/downloads/postgresql-9.6.6 && /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
RUN cd /tmp/downloads/postgresql-9.6.6 && /usr/local/pgsql/bin/createdb test
RUN cd /tmp/downloads/postgresql-9.6.6 && /usr/local/pgsql/bin/psql test
所以当我运行 docker build -t roksolanad / psql:latest。
我收到错误:
So when I run docker build -t roksolanad/psql:latest .
I get errors:
initdb: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.
The command '/bin/sh -c cd /tmp/downloads/postgresql-9.6.6 && /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data' returned a non-zero code: 1
那么我该如何修复Dockerfile?我将非常感谢您的帮助!
So how can I fix my Dockerfile? I would be very grateful for some help!
推荐答案
正如@Harald Nordgren所说,您需要总结所有如果可能,在一个命令中使用RUN命令。
As said by @Harald Nordgren, you need sum up all the RUN commands in one command, if possible.
此外,还有几件事导致失败
1)添加 postgres用户:
1) adding "postgres" user:
adduser期望附加参数在添加用户时进行配置,如您在注释中所提到的,例如询问密码等。因此,您需要修改命令以禁用参数和密码,如下所示:
"adduser" expects the additional parameters to be configured when you add a user, as you have mentioned in the comments like asking for password and so. So you need to modify the command to disable the arguments as well as the password like below:
adduser postgres --gecos '' --disabled-login
2)使用root用户执行postgress
2) executing postgress using root user
执行命令 su postgres 时,它将以root用户身份执行。但是,尽管我们已更改了上面命令 chown postgres / usr / local / pgsql / data
When you execute the command "su postgres", it executes with root user. But whereas we have changed the permissions in the above command "chown postgres /usr/local/pgsql/data"
的权限,为此,您需要以 postgres用户身份执行命令,可以通过在dockerfile中添加USER来启用该命令。
For this you need a execute the command as "postgres" user which can be enabled by adding USER in dockerfile.
最后,您的Dockerfile如下所示:
Finally your Dockerfile looks something like this:
FROM ubuntu
RUN apt-get update && apt-get install gcc zlib1g-dev libreadline6-dev apt-utils make -y
RUN mkdir -p /tmp/downloads
ADD https://ftp.postgresql.org/pub/source/v9.6.6/postgresql-9.6.6.tar.gz /tmp/downloads
RUN cd /tmp/downloads && tar -zxf postgresql-9.6.6.tar.gz
RUN cd /tmp/downloads/postgresql-9.6.6 &&\
make configure &&\
./configure &&\
make &&\
su &&\
make install &&\
adduser postgres --gecos '' --disabled-login &&\
mkdir /usr/local/pgsql/data &&\
chown postgres /usr/local/pgsql/data
USER postgres
#use below command only if it is necessary, it is similar to "cd" linux command
WORKDIR /tmp/downloads/postgresql-9.6.6
RUN /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
有了这个,我能够创建Docker映像并进行测试。添加更多可能有用的信息。
With this I am able to create a Docker image and tested as well. Adding more information which may be useful.
# docker run -it postgres:2.0 /bin/bash
postgres@8354d83023f9:/tmp/downloads/postgresql-9.6.6$ ps -eaf
UID PID PPID C STIME TTY TIME CMD
postgres 1 0 0 10:58 ? 00:00:00 /bin/bash
postgres 9 1 0 10:59 ? 00:00:00 ps -eaf
postgres@8354d83023f9:/tmp/downloads/postgresql-9.6.6$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
server starting
postgres@8354d83023f9:/tmp/downloads/postgresql-9.6.6$ ps -eaf
UID PID PPID C STIME TTY TIME CMD
postgres 1 0 0 10:58 ? 00:00:00 /bin/bash
postgres 13 1 0 10:59 ? 00:00:00 /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
postgres 15 13 0 10:59 ? 00:00:00 postgres: checkpointer process
postgres 16 13 0 10:59 ? 00:00:00 postgres: writer process
postgres 17 13 0 10:59 ? 00:00:00 postgres: wal writer process
postgres 18 13 0 10:59 ? 00:00:00 postgres: autovacuum launcher process
postgres 19 13 0 10:59 ? 00:00:00 postgres: stats collector process
postgres 20 1 0 10:59 ? 00:00:00 ps -eaf
postgres@8354d83023f9:/tmp/downloads/postgresql-9.6.6$ /usr/local/pgsql/bin/createdb test
postgres@8354d83023f9:/tmp/downloads/postgresql-9.6.6$ /usr/local/pgsql/bin/psql test
psql (9.6.6)
Type "help" for help.
test=#
test=#
还有其他种方法来构建此docker映像,但我可以这样做。
There are other ways to build this docker image but this way I am able to.
这篇关于PostgreSQL docker镜像构建错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!