我尝试使用Docker设置dotnet应用程序。
“后端”文件夹中的Dockerfile:
FROM microsoft/dotnet:1.1-sdk AS build-env
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
FROM microsoft/dotnet:1.1-runtime
WORKDIR /app
COPY --from=build-env /app/out ./
CMD ["dotnet", "backend.dll"]
“frontend”文件夹中的Dockerfile:
# build
FROM node AS build-env
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm install
COPY . /usr/src/app/
RUN npm run build
# run
FROM nginx
COPY --from=build-env /usr/src/app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
nginx.conf
events {}
http {
server {
listen 80;
location / {
root /usr/share/nginx/html;
include /etc/nginx/mime.types;
}
location /api {
rewrite ^/api/?(.*) /$1 break;
proxy_pass http://api:5000;
}
}
}
搬运工-compose.yml
version: "3"
services:
# redis:
# image: redis
# container_name: demoapp-redis
# db:
# image: postgres
# container_name: demoapp-database
api:
build:
context: ./backend
container_name: demoapp-backend
ports:
- 5000:5000
# depends_on:
# - db
web:
build:
context: ./frontend
container_name: demoapp-frontend
ports:
- 80:80
depends_on:
- api
当我使用dotnet run
启动我的应用程序时,一切正常
MacBook-Pro-Pavel:~ pavel$ curl http://localhost:5000/values
["value1","value2"]
但是当我跑docker-compose up
时,我收到了这条消息:
demoapp-backend | warn: Microsoft.AspNetCore.Server.Kestrel[0]
demoapp-backend | Unable to bind to http://localhost:5000 on the IPv6 loopback interface: (Error -99 EADDRNOTAVAIL address not available)
demoapp-backend | Hosting environment: Production
demoapp-backend | Content root path: /app
demoapp-backend | Now listening on: http://localhost:5000
demoapp-backend | Application started. Press Ctrl+C to shut down.
当我尝试提出请求时:
MacBook-Pro-Pavel:~ pavel$ curl http://localhost:5000/values
curl: (52) Empty reply from server
MacBook-Pro-Pavel:~ pavel$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2ae78ca79f56 demo_web "nginx -g 'daemon ..." 39 minutes ago Up About a minute 0.0.0.0:80->80/tcp demoapp-frontend
9220206e46fe demo_api "dotnet backend.dll" 7 hours ago Up About a minute 0.0.0.0:5000->5000/tcp demoapp-backend