我使用Gitpod通过Docker生成容器(因为我使用Gitpod,所以无法访问Docker命令行)。我的目标是安装Python和PostgreSQL。这是我当前的Dockerfile:
# Base image is one of Python official distributions.
FROM python:3.8.13-slim-buster
# Update libraries and install sudo.
RUN apt update
RUN apt -y install sudo
# Install curl.
RUN sudo apt install -y curl
# Install git.
RUN sudo apt install install-info
RUN sudo apt install -y git-all
# Install nodejs.
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
RUN sudo apt install -y nodejs
# Download Google Cloud CLI installation script.
RUN mkdir -p /tmp/google-cloud-download
RUN curl -sSL https://sdk.cloud.google.com > /tmp/google-cloud-download/install.sh
# Install Google Cloud CLI.
RUN mkdir -p /gcloud
RUN bash /tmp/google-cloud-download/install.sh --install-dir=/gcloud --disable-prompts
# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./
# Install production dependencies.
RUN pip install --no-cache-dir -r requirements.txt
# Set some variables and create gitpod user.
ENV PGWORKSPACE="/workspace/.pgsql"
ENV PGDATA="$PGWORKSPACE/data"
RUN sudo mkdir -p $PGDATA
RUN useradd -l -u 33333 -G sudo -md /home/gitpod -s /bin/bash -p gitpod gitpod
RUN sudo chown gitpod $PGWORKSPACE -R
# Declare Django env variables.
ENV DJANGO_DEBUG=True
ENV DJANGO_DB_ENGINE=django.db.backends.postgresql_psycopg2
# Declare Postgres env variables. Note that these variables
# cannot be renamed since they are used by Postgres.
# https://www.postgresql.org/docs/current/libpq-envars.html
ENV PGDATABASE=postgres
ENV PGUSER=gitpod
ENV PGPASSWORD=gitpod
ENV PGHOST=localhost
ENV PGPORT=5432
# Install PostgreSQL 14. Note that this block needs to be located
# after the env variables are specified, since it uses POSTGRES_DB,
# POSTGRES_USER and POSTGRES_PASSWORD to create the first user.
RUN curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/postgresql.gpg
RUN sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
RUN sudo apt -y update
RUN sudo apt -y install postgresql-14
USER gitpod
# Set some more variables and init the db.
ENV PATH="/usr/lib/postgresql/14/bin:$PATH"
RUN mkdir -p ~/.pg_ctl/bin ~/.pg_ctl/sockets
RUN initdb -D $PGDATA
RUN printf '#!/bin/bash\npg_ctl -D $PGDATA -l ~/.pg_ctl/log -o "-k ~/.pg_ctl/sockets" start\n' > ~/.pg_ctl/bin/pg_start
RUN printf '#!/bin/bash\npg_ctl -D $PGDATA -l ~/.pg_ctl/log -o "-k ~/.pg_ctl/sockets" stop\n' > ~/.pg_ctl/bin/pg_stop
RUN chmod +x ~/.pg_ctl/bin/*
ENV PATH="$HOME/.pg_ctl/bin:$PATH"
ENV DATABASE_URL="postgresql://gitpod@localhost"
ENV PGHOSTADDR="127.0.0.1"此时,我希望有一个名为postgres的数据库和一个名为gitpod的默认用户,这也是bash中默认用户的名称。但是,当我尝试使用psql -h localhost时,我会收到一个错误消息:
psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: password authentication failed for user "gitpod"
connection to server at "127.0.0.1", port 5432 failed: FATAL: password authentication failed for user "gitpod"尽管有这样的消息,我相信并没有真正创建任何用户。如果我尝试以用户名的形式使用随机字符串登录,我会收到相同的消息。
我也试过:
echo "host all all 127.0.0.1/32 trust" | sudo tee -a /etc/postgresql/14/main/pg_hba.conf
这不会改变任何事情。
echo "host all all localhost trust" | sudo tee -a /etc/postgresql/14/main/pg_hba.conf
这不会改变任何事情。
sudo -u gitpod psql postgres
它返回错误:psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "gitpod" does not exist
发布于 2022-09-14 13:03:40
如果我使用您的Dockerfile创建一个Gitpod工作区来构建自定义映像(从https://gitpod.io#https://github.com/larsks/so-example-73710360-gitpod-test/tree/main开始),我在打开终端时发现,gitpod将您的工作区挂载在/workspace上,所以您放置在Dockerfile中的任何东西(例如postgres数据目录)在运行时都不可用。
如果我在终点站运行:
$ mkdir /workspace/data
$ initdb
$ ~/.pg_ctl/bin/pg_start然后postgres正确运行,我可以使用psql毫无问题地连接到它:
$ psql
psql (14.5 (Debian 14.5-1.pgdg100+1))
Type "help" for help.
postgres=#我们可以通过在/workspace之外安装数据库来修复Dockerfile中的这个问题。例如,我们可以:
/data/pgdata/data/sockets/usr/local/bin用于pg_start/pg_stop脚本我已经对这些更改进行了做了一个Dockerfile处理(以及其他一些更改);您可以在https://gitpod.io#https://github.com/larsks/so-example-73710360-gitpod-test/tree/fixed上试用它。
通过这些更改,pg_start没有问题地运行,psql成功连接:
gitpod@larsks-soexample7371036-htkkrp9bsl7:/workspace/so-example-73710360-gitpod-test$ pg_start
waiting for server to start.... done
server started
gitpod@larsks-soexample7371036-htkkrp9bsl7:/workspace/so-example-73710360-gitpod-test$ psql
psql (14.5 (Debian 14.5-1.pgdg100+1))
Type "help" for help.
postgres=#我对Dockerfile做了一些其他的修改,因为我无法控制自己。特别是:
sudo使用apt install传递多个包名来加快构建过程,而不是单独安装每个包名。COPY . ./时,对工作目录中的文件的任何更改都将使缓存失效,因此在此之后的任何构建步骤都需要重新执行。通过在COPY语句之前尽可能多地进行操作,我们可以显著减少本地重建映像所需的时间。https://stackoverflow.com/questions/73710360
复制相似问题