我认为没有解决此问题的标准方法,但这是一个已知问题,有些人有可接受的解决方法。
Docker 问题跟踪器上有一个关于在侦听暴露端口之前不考虑容器启动的建议。然而,由于它会在其他地方产生其他问题,它可能不会被接受。也有关于同一主题的无花果提案。
简单的解决方案是像@jcortejoso 所说的那样进行睡眠。来自http://blog.chmouel.com/2014/11/04/avoiding-race-conditions-between-containers-with-docker-and-fig/的示例:
function check_up() {
service=$1
host=$2
port=$3
max=13 # 1 minute
counter=1
while true;do
python -c "import socket;s = socket.socket(socket.AF_INET, socket.SOCK_STREAM);s.connect(('$host', $port))" \
>/dev/null 2>/dev/null && break || \
echo "Waiting that $service on ${host}:${port} is started (sleeping for 5)"
if [[ ${counter} == ${max} ]];then
echo "Could not connect to ${service} after some time"
echo "Investigate locally the logs with fig logs"
exit 1
fi
sleep 5
(( counter++ ))
done
}
然后check_up "DB Server" ${RABBITMQ_PORT_5672_TCP_ADDR} 5672
在启动您的应用服务器之前使用,如上面的链接中所述。
另一种选择是使用docker-wait。在你的fig.yml
.
rabbitmq:
image: dockerfile/rabbitmq:latest
mongodb:
image: mongo
rabbitmqready:
image: aanand/wait
links:
- rabbitmq
app:
build: .
command: python /code/app/main.py
links:
- rabbitmqready
- mongodb
volumes:
- .:/code