PostgreSQL docker图像构建错误

时间:2017-12-03 20:35:15

标签: postgresql docker dockerfile

我正在从源代码创建postgresql安装的docker镜像。这是我根据postgresql documentation创建的Dockerfile:

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 .时,我会收到错误:

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呢?我会非常感谢你的帮助!

2 个答案:

答案 0 :(得分:2)

问题是每个RUN都会创建自己的shell。这意味着用户名仅对该一个实例进行持久更改。这与任何将持续存在的文件系统更改不同。

尝试将命令链接在一起代替:

RUN cd /tmp/downloads/postgresql-9.6.6 &&\
    make configure &&\
    ./configure &&\
    make &&\
    su &&\
    make install &&\
    adduser postgres &&\
    mkdir /usr/local/pgsql/data &&\
    chown postgres /usr/local/pgsql/data

RUN cd /tmp/downloads/postgresql-9.6.6 &&\
    su postgres &&\
    /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data &&\
    /usr/local/pgsql/bin/createdb test &&\
    /usr/local/pgsql/bin/psql test

答案 1 :(得分:1)

正如@Harald Nordgren所说,如果可能的话,你需要在一个命令中总结所有RUN命令

除此之外还有导致失败的几件事

  

1)添加" postgres"用户:

<强>&#34; adduser的&#34;期望在添加用户时配置其他参数,正如您在评论中提到的那样,例如要求输入密码等等。因此,您需要修改命令以禁用参数以及密码,如下所示:

adduser postgres --gecos '' --disabled-login 
  

2)使用root用户执行postgress

当您执行命令&#34; su postgres &#34;时,它将以root用户身份执行。但是我们已经更改了上述命令中的权限&#34; chown postgres / usr / local / pgsql / data&#34;

为此你需要一个执行命令为&#34; postgres&#34;用户可以通过在dockerfile中添加USER来启用

最后你的Dockerfile看起来像这样:

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镜像并进行测试。添加可能有用的更多信息。

# 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镜像,但这样我就可以了。