英文:
No connections to mssql in docker compose
问题
我有两个通过docker-compose运行的容器,我的docker-compose文件如下所示:
version: "3.7"
services:
mssql:
build: ./Db
ports:
- 1433:1433
planning-poker:
build: .
restart: always
env_file:
- env.list
ports:
- 80:8080
depends_on:
- mssql
go-app的Dockerfile如下:
FROM golang:latest
RUN apt-get -y update && \
apt-get install -y net-tools && \
apt-get install -y iputils-ping && \
apt-get install -y telnet
ADD . /go/src/planning-poker
WORKDIR /go/src/planning-poker
RUN go build -o main .
ENTRYPOINT ["./main"]
mssql的Dockerfile如下:
FROM mcr.microsoft.com/mssql/server
ENV ACCEPT_EULA=Y
ENV MSSQL_SA_PASSWORD=Yukon_900
EXPOSE 1433
USER root
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN chmod +x ./run-initialization.sh
USER mssql
CMD /bin/bash ./entrypoint.sh
我正在使用数据库初始化脚本:
for i in {1..50};
do
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Yukon_900 -d master -i SQL_PlanningPoker.sql
if [ $? -eq 0 ]
then
echo "SQL_PlanningPoker.sql completed"
break
else
echo "not ready yet..."
sleep 1
fi
done
我的entrypoint如下:
./run-initialization.sh & /opt/mssql/bin/sqlservr
问题是我无论如何都无法从包含golang应用程序的容器连接到mssql,连接是通过主机进行的,我尝试从go-app容器上的localhost 1433、127.0.0.1 1433、0.0.0.0 1433使用telnet连接到mssql,但总是出现连接被重置或telnet无法解析地址的错误。
我的项目链接:https://github.com/philomela/PlanningPoker/tree/master - master分支。
我做错了什么?提前谢谢!
英文:
I have two containers that run via docker-compose, my docker-compose looks like this
version: "3.7"
services:
mssql:
build: ./Db
ports:
- 1433:1433
planning-poker:
build: .
restart: always
env_file:
- env.list
ports:
- 80:8080
depends_on:
- mssql
Dockerfile go-app:
FROM golang:latest
RUN apt-get -y update && \
apt-get install -y net-tools && \
apt-get install -y iputils-ping && \
apt-get install -y telnet
ADD . /go/src/planning-poker
WORKDIR /go/src/planning-poker
RUN go build -o main .
ENTRYPOINT ["./main"]
Dockerfile mssql:
FROM mcr.microsoft.com/mssql/server
ENV ACCEPT_EULA=Y
ENV MSSQL_SA_PASSWORD=Yukon_900
EXPOSE 1433
USER root
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN chmod +x ./run-initialization.sh
USER mssql
CMD /bin/bash ./entrypoint.sh
I am using database initialization scripts:
for i in {1..50};
do
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Yukon_900 -d master -i SQL_PlanningPoker.sql
if [ $? -eq 0 ]
then
echo "SQL_PlanningPoker.sql completed"
break
else
echo "not ready yet..."
sleep 1
fi
done
So is my entrypoint:
./run-initialization.sh & /opt/mssql/bin/sqlservr
The problem is that I can’t connect to mssql from the container with the golang application in any way, the connection passes from the host, I tried to connect via telnet to mssql from the go-app container on localhost 1433, 127.0.0.1 1433, 0.0.0.0 1433 but always I get an error that the connection is either reset or telnet cannot resolve the addresses.
MyProject: https://github.com/philomela/PlanningPoker/tree/master - master branch.
What am I doing wrong? thank you in advance!
答案1
得分: 1
尝试添加网络并确保所有服务在同一网络中运行
networks:
my-network:
services:
mssql:
build: ./Db
ports:
- 1433:1433
networks:
- my-network
planning-poker:
build: .
restart: always
env_file:
- env.list
ports:
- 80:8080
depends_on:
- mssql
networks:
- my-network
此外,可以使用一些健康检查来检查服务是否健康,而不仅仅是使用 depends_on
,因为Docker可能已经启动,但SQL服务器可能需要一些时间才能启动和运行。
参考链接:https://docs.docker.com/engine/reference/builder/#healthcheck
英文:
try adding network and kepp all services run in same network
networks:
my-network:
services:
mssql:
build: ./Db
ports:
- 1433:1433
networks:
- my-network
planning-poker:
build: .
restart: always
env_file:
- env.list
ports:
- 80:8080
depends_on:
- mssql
networks:
- my-network
Also there is a possibility to check service is healthy with some heath check rather than just depends_on
because docker may be up but SQL server would take some time to be up and running
https://docs.docker.com/engine/reference/builder/#healthcheck
答案2
得分: 0
问题已解决,我遭受了几天的困扰,但没有注意到我的应用程序中的端口转发,现在一切都正常工作!
英文:
The issue is resolved, I suffered for several days, but did not pay attention to port forwarding in my application, now everything is working stably!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论