Table of contents
1. Create a NetCore API project
2.1 First log in to the cloud server to create a test directory
2.2 Create a new dockerfile file to build an image.
Table of contents
1. Create a NetCore API project
2.1 First log in to the cloud server to create a test directory
2.2 Create a new dockerfile file to build an image.
Because I have been engaged in the development of C# for five years, although I have not used C# very much recently, in order to be able to use it later, I briefly sorted out the use of dockerfile to deploy a very basic netcore api project from Microsoft. Interested friends can also learn about it.
1. Create a NetCore API project
First, create a netcore api project because it is relatively simple and will not be introduced. This can be obtained directly by leaving a message.
2. Cloud service deployment
2.1 First log in to the cloud server to create a test directory
mkdir /home/netCoreDemo/webapp
Use the Linux ftp tool to upload the deployment package released by Visualstudio to the /home/netCoreDemo/webapp directory of the cloud server.
2.2 Create a new dockerfile file to build an image.
The content is as follows :
# 添加基础镜像:docker会从网络下载这个镜像
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
ENV ASPNETCORE_URLS=http://+:28080 #解决下面报错的问题
#容器中系统的工作空间
WORKDIR /webapp
#拷贝当前文件夹下的文件到容器中系统的工作空间(注意:“.”后面有空格)
COPY . /webapp
#设置Docker容器对外暴露的端口
EXPOSE 28080
#容器中使用 ["dotnet","系统启动的dll"] 来运行应用程序
#使用ENTRYPOINT ["dotnet","系统启动的dll"]
#也可以使用 CMD ["dotnet","系统启动的dll"]推荐使用ENTRYPOINT
ENTRYPOINT ["dotnet", "test.dll"]
2.3 Build the image
Execute the build image command in the current dockerfile directory
build -f dockerfile -t xm/netcoredemo:1.0 .
2.4 Running the image
Start the running image, it is recommended to add the -d parameter to start the background
docker run --name netcoredemo -p 28080:28080 xm/netcoredemo:1.0
background start command
docker run -d --name netcoredemo -p 28080:28080 xm/netcoredemo:1.0
Server direct intranet test interface is normal
curl http://127.0.0.1:28080/weatherforecast
Preparation conditions for external network access test
Make sure your cloud server external network port is open
The firewall should also pay attention to opening port 28080 or closing the firewall
Browser access:
http://your public IP:28080/weatherforecast
To delete a container, you need to stop the container first, and then delete it
docker stop netcoredemo #先停止
docker rm netcoredemo #删除容器
delete mirror
docker rmi xm/netcoredemo:1.0
Deployment issues on the IPv6 loopback interface: 'Cannot assign requested address'
solution:
This line of configuration is added to the dockerfile, because this is only for testing purposes and does not consider using nginx as a proxy.
ENV ASPNETCORE_URLS=http://+:28080