.net Core 2.2(API)+ react(ui)Docker文件

我对docker完全陌生,在我有一个包含7个项目的解决方案的情况下,试图弄清楚如何使用docker,在我的案例中是其API。 对于UI,我使用reactJS。

我尝试了这个Dockerfile。 Front正在3000端口上运行,并且可以正常工作,但是我没有连接到容器内部的API。

FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app

COPY /admin.api/DeployOutput/ ./
ENTRYPOINT ["dotnet","Admin.Api.dll"]

FROM node:12 as react-build
WORKDIR /app
COPY /admin.ui/ ./

RUN npm install
CMD [ "npm","start" ]

在package.json中,我输入URL以连接到API

  "proxy": "https://localhost:5001","eslintConfig": {
    "extends": "react-app"
  },

如果我在本地运行(不使用Docker),则该项目有效

API在端口5000和5001上运行。

我需要编写Dockerfile,它将使用UI + API创建映像

sdn199 回答:.net Core 2.2(API)+ react(ui)Docker文件

所以我推荐的方法是使用两个单独的容器,一个用于API,另一个用于前端。

#1 API

# Step 1 - build
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /build

COPY . . # copy everything from where the dockerfile is,to current workdir inside the container (sloppy but it works)
RUN dotnet publish -o out # compile the dll's and put them in an "out" directory

# Step 2 - runtime (compiled dll:s)
FROM microsoft/dotnet:2.2-aspnetcore-runtime
WORKDIR /app
COPY --from=build /build/out . # copy your compiled dll's from your BUILD CONTAINER and place them inside your runtime container

EXPOSE 5000/tcp # this is only to document what port is in use
ENTRYPOINT [ "dotnet","API.dll","--urls","http://*:5000"] # the name of your DLL to run

我认为对“调试” dockerfiles中的构建有用的一件事是添加RUN ls -al以输出当前目录,因此您可以看到生成了什么文件的位置。随时添加它们。

#2 Frontend - angular example,but is probably very simular to react

# build
FROM node:10.9.0-alpine as node
WORKDIR /app

COPY . .

# npm install
RUN npm i -g @angular/cli@8.3.2
RUN npm install
RUN ng build --prod

# runtime
FROM nginx:1.13.12-alpine
COPY --from=node /app/out /usr/share/nginx/html

# RUN rm -r /etc/nginx/conf.d/default.conf # replace with your own server config if you like
COPY --from=node /app/default.conf /etc/nginx/conf.d/

好吧,假设您现在已经构建了Docker容器,前端和API。仅启动单个容器,请使用docker run <image name>-但这尚未将任何端口映射到您的host machine。为此,请添加用于端口映射的参数-类似于docker run -p 3999:5000 <image name>。要验证其是否正在运行,请打开浏览器并导航至localhost:3999/your/api/endpoint

第一个参数-p 3999:5000将您的主机端口:3999映射到隔离的容器端口:5000,您可以随意交换它。

有关更多选项,请参见docker --help

另一个有用的技巧是从主机到正在运行的容器中输入命令shell,为此,请执行docker exec -it <container_id> bashdocker exec -it <container_id> sh。要列出正在运行的容器,请执行docker psdocker ps -a

如果您要我更新此帖子,请随时提问。干杯!

本文链接:https://www.f2er.com/3159432.html

大家都在问