This is the second part of the three-part series that touches on deploying an ASP.NET Core Razor Pages web application to Ubuntu Server. In this series I will cover the various methods to make deploy and finally advancing to continuous integration and continuous deployment (CICD) practices.
In my previous post of this series I talked about using the file transfer protocol to deploy files to remote server. In this post I will talk about using Docker to package the web application, push it to the centralised repository from which I then pull to the remote server, and execute it within the remote server.
Pack Web Application Into a Docker Image
The files that were necessary to run my web application were transferred to remote server in the previous post. However, in this post I would like to pack them all into a single Docker image. First I created a
Dockerfile that had the following instructions:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 WORKDIR /app COPY bin/Release/netcoreapp3.1/publish/ . ENTRYPOINT ["dotnet", "<project name>.dll"]
The above instructions pulled and used the official ASP.NET Core 3.1 runtime, set the working directory of the docker image to be
/app, copied the above-mentioned files to this working directory, and finally set the entry point of docker image to be the command that runs the web application. From here I could foresee that when the docker container is created based on this docker image, the web application will be executed immediately in its own runtime that comes with it.
dockerfile was ready, it was time to pack the files. The following command executed the packing process
docker build -t <dockerhub repository name>/<image name> .
Run and Push the Docker Image
At the moment the docker image had been successfully created. I could test the docker image by using the
docker run command
docker run -d -p 5000:80 --name <container name> <dockerhub repository name>/<image name>
This created a docker container based one the docker image, exposed the port 80 of the docker container to port 5000 outside, and finally assigned a name for the container. Head to the browser and navigate to
localhost:5000 and I could see the running web application. At this point of time the application can be run simply out of docker image.
For the server to run this application via docker image, I had to store this image somewhere that was accessible by the remote server. Docker Hub solves this issue for us by providing a free repository to store docker images. To push the docker image to Docker Hub, I ran the command:
docker push <dockerhub repository name>/<image name>
I could then head to the remote server and run the web application through docker
docker pull <dockerhub repository name>/<image name> docker run -d -p 5000:80 --name <container name> <dockerhub repository name>/<image name>
What I Have Achieved so Far
At this point, I had successfully created a docker image out of the necessary files and run them via docker container. Compared to merely copying the files over to remote server, this method standardises the application deployment by ensuring same application instance is used across multiple servers, streamlines the deployment process, and the best part, comes with the necessary .NET Core runtime. Now I had the process to automatically prepare the deployment file (the docker image) and publish it to the central repository (Docker Hub), it was time to set up the CICD (continuous integration and continuous deployment) workflow which I will cover in the next post.