所以我推荐的方法是使用两个单独的容器,一个用于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> bash
或docker exec -it <container_id> sh
。要列出正在运行的容器,请执行docker ps
或docker ps -a
。
如果您要我更新此帖子,请随时提问。干杯!
本文链接:https://www.f2er.com/3159432.html