我正在尝试关注此tutorial并设置postgresql容器。
我有以下脚本:
#!/bin/bash
# wait-for-postgres.sh
set -e
host="$1"
shift
cmd="$@"
until psql -h "$host" -U "postgres" -c '\l'; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - executing command"
exec $cmd
以下docker-compose.yml
:
version: '2'
services:
server:
build: .
ports:
- 3030:3030
depends_on:
- database
command: ["./setup/wait-for-postgres.sh", "localhost:5432", "--", "node", "src"]
database:
image: postgres
environment:
- "POSTGRES_USER=postgres"
- "POSTGRES_PASSWORD=postgres"
- "POSTGRES_DB=tide_server"
ports:
- 5432:5432
问题是,当我运行docker-compose up
时,我收到以下错误:
server_1 | Postgres is unavailable - sleeping
server_1 | psql: could not translate host name "192.168.64.2:5432" to address: Name or servi
ce not known
server_1 | Postgres is unavailable - sleeping
server_1 | psql: could not translate host name "192.168.64.2:5432" to address: Name or servi
ce not known
server_1 | Postgres is unavailable - sleeping
server_1 | psql: could not translate host name "192.168.64.2:5432" to address: Name or servi
ce not known
现在我尝试将主机设置为database
,localhost
,0.0.0.0
,甚至是容器IP,但没有任何效果,我不知道它应该是什么或如何调试它,我不是100%确定docker-compose
如何链接容器。
答案 0 :(得分:1)
不要使用depends_on。尝试使用“链接”
version: '2'
services:
server:
build: .
ports:
- 3030:3030
links:
- database
#environment could be usefull too
environment:
DATABASE_HOST: database
command: ["./setup/wait-for-postgres.sh", "localhost:5432", "--", "node", "src"]
database:
image: postgres
environment:
- "POSTGRES_USER=postgres"
- "POSTGRES_PASSWORD=postgres"
- "POSTGRES_DB=tide_server"
ports:
- 5432:5432
答案 1 :(得分:1)
本教程跳过了一些内容,并且由于提到了wait-for-it.sh
脚本而令人困惑,但是随后显示了一个简化的版本,如果您将hostname:port
作为一个参数传递给它,则该版本将不起作用。它。
我很难做到这一点,为了将来我和其他人,我将在下面添加步骤。我在MacOS上进行了此操作,并安装了docker和docker-compose以及nodejs。
我没有方便使用您的节点应用程序,因此我使用了https://nodejs.org/de/docs/guides/nodejs-docker-webapp/
所述的应用程序我具有以下目录结构:
/src/package.json
/src/server.js
/.pgpass
/docker-compose.yml
/Dockerfile
/wait-for-postgres.sh
这些文件的内容在下面列出。
./src
目录运行$ npm install
(创建package-lock.json)$ chmod 600 .pgpass
修复pgpass权限$ chmod +x wait-for-postgres.sh
$ docker-compose up
它将拉出postgres图像并构建节点应用程序容器。 完成后,它将等待postgres,而当postgres启动时,您将看到它已经准备好。
src文件与上面的node js dockerize链接完全相同
/src/package.json
{
"name": "docker_web_app",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "First Last <first.last@example.com>",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.1"
}
}
/src/server.js
'use strict';
const express = require('express');
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello world\n');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
.pgpass
这使用用户名:密码postgres:postgres
,仅用于开发演示目的。在野外,您将使用其他秘密管理方法,并且永远不会将pgpass文件提交到版本控制中
#host:port:db:user:pass
db:5432:*:postgres:postgres
docker-compose.yml
wait-for-postgres.sh
脚本添加为托管卷,在最初的问题中,它是将其与怪异的应用程序src捆绑在一起。我还已经在根用户的主目录中装入了.pgpass
文件,psql将在其中查找该文件以自动完成密码。如果您没有提供此方法的方法,则会收到错误消息:
psql:fe_sendauth:未提供密码
请注意,server
容器的命令引用了database
,这是postgres容器的有效的docker-compose内部dns名称。
version: '2'
services:
server:
build: .
ports:
- 3030:3030
depends_on:
- database
volumes:
- ./wait-for-postgres.sh:/usr/app/setup/wait-for-postgres.sh
- ./.pgpass:/Users/root/.pgpass
command: ["/usr/app/setup/wait-for-postgres.sh", "database", "--", "node", "src"]
database:
image: postgres
environment:
- "POSTGRES_USER=postgres"
- "POSTGRES_PASSWORD=postgres"
- "POSTGRES_DB=tide_server"
ports:
- 5432:5432
Dockerfile
psql
。FROM node:10-buster
RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main" > /etc/apt/sources.list.d/pgdg.list && \
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
RUN apt-get -y update - && \
apt-get -y install libpq-dev && \
apt-get -y install postgresql-client-11
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
wait-for-postgres.sh
#!/bin/bash
# wait-for-postgres.sh
set -e
host="$1"
shift
cmd="$*"
export PGPASSFILE=./pgpass
until psql -h "$host" -U "postgres" -c '\l'; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - executing command"
exec "$cmd"
答案 2 :(得分:0)
这里的问题是主持人本身。
psql -h &#34; $ host&#34; -U&#34;&#34; -c&#39; \ l&#39;
您传递的是错误的HOSTNAME&#34; localhost:5432&#34; /&#34; 192.168.64.2:5432&#34;
我所做的是设置一个〜/ .pgpass 本地主机:5432:DB:USER:PASSWORD
而不是传递&#34; localhost:5432&#34;,省略端口。只需使用&#34; localhost&#34;
这对我有用......
答案 3 :(得分:0)
可能是一个旧线程要回答,但我一直在使用depends_on和以下docker-compose文件
version: '3.4'
volumes:
postgres_data:
driver: local
services:
postgres:
image: postgres
volumes:
- ./postgres_data:/var/lib/postgresql:rw
- ./deployments:/opt/jboss/wildfly/standalone/deployments:rw
environment:
POSTGRES_DB: keycloak
POSTGRES_USER: keycloak
POSTGRES_PASSWORD: password
ports:
- 5432:5432
keycloak:
image: jboss/keycloak
environment:
POSTGRES_ADDR: postgres
POSTGRES_DATABASE: keycloak
POSTGRES_USER: keycloak
POSTGRES_PASSWORD: password
KEYCLOAK_USER: admin
KEYCLOAK_PASSWORD: Pa55w0rd
ports:
- 8080:8080
- 9990:9990
depends_on:
- postgres