Use Docker to publish C# .NETCore WebApi container to the server

Table of contents

1. Enable Docker support

2. Publish application program

3. The server pulls the image and runs the WebApi container


To use Docker to release programs, both the server and the local area need to have the docker program installed. For the download and installation of docker, please refer to the official website: Docker: Accelerated, Containerized Application Development

1. Enable Docker support

If we use vs2022 to create a new WebApi project, we need to check to enable Docker:

If you don't check it or use the project created by VS019, you can add docker support to the right button of the project

2. Publish application program

Next, start publishing the program, right click on the project to publish 

Prompt where to publish content, select the docker container registry: 

 Select docker hub to publish the application as a docker image to docker hub

 Enter the corresponding docker account password. You need to register an account with docker hub. For docker hub, please refer to the official website: Docker Hub

 After completion, you can see your own docker hub repository address, click publish, you must run the local docker first when publishing, otherwise an error will be reported

 Prompt has been published successfully:

3. The server pulls the image and runs the WebApi container

Log in to the server, open a command prompt, and see if you can search for your own image on docker hub:

docker search lwpoor/mywebapi

# lwpoor/mywebapi is your own image name

 You can see that the mirror image already exists

Pull the image through pull:

docker pull lwpoor/mywebapi

 View the pulled image:

docker images

Next, start running the WebApi container, enter the following code to run the container:

docker run -d -p 8888:5050 --name webapi --restart=always lwpoor/mywebapi

# -d means running in the background

# -p 8888:5050 indicates that port 5050 of the container is mapped to port 8888 of the server

# --name webapi Give the container a name webapi

# --restart=always container starts with system boot

 The startup is successful, and the browser opens the connection: http://localhost:8888/api/admin/login?pwd=123456

The WebApi is successfully published to the server!

Guess you like

Origin blog.csdn.net/lwpoor123/article/details/127900973