嗨,当我尝试在yml文件上运行docker-compose时,出现此错误。
这是我的docker-compose.yml文件
version: '3.6'
services:
phoenix:
# tell docker-compose which Dockerfile it needs to build
build:
context: .
dockerfile: Dockerfile.development
# map the port of phoenix to the local dev port
ports:
- 4000:4000
# mount the code folder inside the running container for easy development
volumes:
- . .
# make sure we start mongodb when we start this service
depends_on:
- db
db:
image: mongo:latest
volumes:
- ./data/db:/data/db
ports:
- 27017:27017
这是我的Dockerfile:
# base image elixer to start with
FROM elixir:1.6
# install hex package manager
RUN mix local.hex --force
RUN mix local.rebar --force
# install the latest phoenix
RUN mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez --force
# create app folder
COPY . .
WORKDIR ./
# install dependencies
RUN mix deps.get
# run phoenix in *dev* mode on port 4000
CMD mix phx.server
这是我的dev.exs设置存在问题吗,还是与docker和phoenix / docker和mongodb的兼容性有关?
最佳答案
https://docs.docker.com/compose/compose-file/#depends_on明确表示:
There are several things to be aware of when using depends_on:
- depends_on does not wait for db and redis to be “ready” before starting web - only until they have been started. If you need to wait for a service to be ready,
并建议您实现逻辑以等待mongodb启动,并准备自己接受连接:https://docs.docker.com/compose/startup-order/
在您的情况下,可能是这样的:
CMD wait-for-db.sh && mix phx.server
wait-for-db.sh
可以很简单#!/bin/bash
until nc -z localhost 27017; do echo "waiting for db"; sleep 1; done
为此,您需要在容器中安装nc
和wait-for-db.sh
。还有许多其他替代工具可以测试db容器是否正在目标端口上侦听。
更新:
容器之间的网络连接在https://docs.docker.com/compose/networking/中进行了描述:
当您运行docker-compose up时,会发生以下情况:
myapp_default
的网络,其中myapp
是docker-compose.yml
的存储目录的名称。 myapp_default
的名称加入网络phoenix
。 myapp_default
的名称加入网络db
。 现在,每个容器都可以查找主机名
phoenix
或db
,并获取适当的容器的IP地址。例如,凤凰的应用程序代码可以连接到URL mongodb://db:27017
并开始使用Mongodb数据库。
关于mongodb - docker-compose mongodb phoenix,[错误]无法连接:**(Mongo.Error)tcp连接:连接被拒绝-:econnrefused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51168224/