我有一个非常简单的asp.net应用
,当我构建docker镜像并尝试在本地PC上运行它时: 在我的镜像名为test,我执行
docker run test
此时我看到
"Content root path: /app Now listening on: http://[::]:80"
然后我运行docker ps
我可以看到进程正在运行,但当我尝试导航到localhost:80
时,我得到的是漫长的等待,然后“无法访问此站点,localhost拒绝连接”
我输入
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ec158cc3b344
如果我带着端口80访问,报“This site can’t be reached”,或者如果我尝试直接访问IP,则得到 “Your connection was interrupted”。
我还尝试不用docker,直接将镜像部署到Kubernetes上,但是当我尝试访问服务External-IP(localhost)
时,还是 "This page isn’t working, localhost didn’t send any data。”
我也尝试使用
kubectl get pods -o wide
然后访问pod的ip,报 “This 10.1.0.32 page can’t be found”
这是我的dockerfile
和kubernetes的deployment.yml
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["Test/Test.csproj", "Test/"]
RUN dotnet restore "Test/Test.csproj"
COPY . .
WORKDIR "/src/Test"
RUN dotnet build "Test.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Test.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Test.dll"]
apiVersion: apps/v1
kind: Deployment
metadata:
name: test
spec:
replicas: 3
selector:
matchLabels:
app: test
template:
metadata:
labels:
app: test
spec:
containers:
- name: test
image: <DockerEndpoint>.io/test:v5 #Sorry, can't include the real endpoint!
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: test
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
selector:
app: test
请大神指导一下。
PS:即使我创建了一个具有docker支持的全新.net程序
,也不行。
你把8080作为端口转发的目标端口了,但是你的应用程序正在侦听的是
80
。将targetport设置更改为80,就可以了另外,你还可能还需要调整
containerPort
设。好了,可以访问了!我把目标端口设置成80就好了,感谢!
你的答案