postgresql - Docker + Postgres 无法使配置工作
问题描述
我刚刚开始使用 docker,但无法让我的 API 连接到我的数据库。我得到一个connection refused
.
我在日志中得到了这个,这似乎意味着环境变量没有被传入:The files belonging to this database system will be owned by user "postgres".
docker exec -ti [container_id] psql -U postgres
但是,也有效docker exec -ti [container_id] psql -U docker
->role "docker" does not exist
我也尝试COPY
在数据库中创建一个 init.sql 文件Dockerfile
,但它似乎没有任何效果。
这是我的docker-compose.yaml
。有什么明显的错误吗?
version: '2'
services:
db:
build: ./db
volumes:
- ./db/pgdata:/pgdata
ports:
- "5432:5432"
environment:
- POSTGRES_USER=docker
- POSTGRES_PASSWORD=docker
- POSTGRES_DB=xx
- PGDATA=/pgdata
api:
build:
context: .
args:
app_env: ${APP_ENV}
volumes:
- .:/go/src/github.com/x/xx
ports:
- "5000:8080"
links:
- db
这是我的 Dockerfile
FROM golang
ARG app_env
ENV APP_ENV $app_env
# Copy the local package files to the container's workspace
COPY . /go/src/github.com/x/xx
WORKDIR /go/src/github.com/x/xx
# added vendor services will need to be included here
RUN go build
# if dev setting will use pilu/fresh for code reloading via docker-compose volume sharing with local machine
# if production setting will build binary
CMD if [ ${APP_ENV} = production ]; \
then \
api; \
else \
go get github.com/pilu/fresh && \
fresh -c recompile.conf; \
fi
# Document that the container uses port 8080
EXPOSE 8080
这是我在 ./db 下的 Dockerfile
FROM postgres:latest
EXPOSE 5432
解决方案
据我所知,这似乎是一个网络问题。没有完整的范围很难调试你的文件,所以这里是关于内部网络以及如何允许容器之间连接的文档。
此外,这是一个完整docker-compose.yml
的 Postgres 数据库和一个桥接网络:
version: '3.1'
services:
postgres:
container_name: postgres
image: postgres:11-alpine
network_mode: bridge
volumes:
- ./data:/var/lib/postgresql/data
ports:
- 5432:5432
environment:
- "POSTGRES_PASSWORD=whatever"
clientApp:
container_name: cinema
image: your/image:latest
network_mode: bridge
external_links:
- postgres
推荐阅读
- c# - 在 Visual Studio 中根据解决方案配置使用不同的 appsetting.json 文件
- r - 如何在 bookdown/knitr/Rmarkdown 中添加交叉引用补充数字
- sql - 使用 regexp_substr 的 Sql 查询很慢,一旦在表中插入更多记录就会超时
- jestjs - 如何在 Jest 测试框架中创建 mockObject 而无需担心对象的属性
- php - 每次运行更新时,Composer 交替删除/重新安装相同的包
- wpf - 如何从模板外部但在同一个窗口中的资源字典绑定到控件模板内的元素?
- css - 是否可以仅使用 CSS 更改修改后的输入字段的样式?
- maven - 万无一失的测试输出丢失
- sybase - Sybase proc 抛出错误 - 语法不正确,“,”
- arm - 交叉编译 c++-File:文件格式无法识别