PostgreSQL Docker映像构建错误



我正在为postgresql安装创建一个docker映像。这是我根据PostgreSQL文档创建的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

那么我该如何修复我的码头?我非常感谢一些帮助!

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

尝试将您的命令链接在一起:

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

如@harald Nordgren所说,如果可能的话,您需要将所有运行命令总结在一个命令中。

随之而来的是造成失败的几件事

1)添加" Postgres"用户:

" adduser"期望在添加用户时将配置附加参数,如您在评论中提到的,例如要求密码等。因此,您需要修改命令以禁用参数以及如下所示的密码:

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

2)使用root用户执行后加工

当您执行命令" su Postgres "时,它将使用root用户执行。但是,尽管我们更改了上述命令中的权限" chown postgres/usr/local/pgsql/data"

为此,您需要一个执行命令为" postgres"用户,可以通过在Dockerfile中添加用户

最后,您的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映像,但是我可以这样做。

最新更新