Objective
Recently took the very fire Docker to see, therefore rubs the fist to wipe the palm to practice. So take the previous Aps.net core project (has been stopped) to practiced hand. The project was previously guaranteed to work on ubuntu14.04, so Docker should not have too many problems. I searched for the official Docker image of ASP, but in order to learn Docker I decided to start making a Docker image from the base image of Linux, and in order to avoid detours, I decided to start with the ubuntu14.04 that I could run.
Prepare the Environment
Os:ubuntu 14.04
. NET Core sdk:2.0
Db:mysql
Compiling the publishing program
sudo dotnet publish -o ..\publish -c Release -r ubuntu.14.04-x64
Compile the program into a binary file of the Ubuntu platform.
Building a database with Docker containers
Now that the application is Docker, the database must also be Docker, where the database I use MySQL, directly using the official image of the mysql:5.7
sudo docker run --name demo-mysql -v /home/yotsuki/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql:5.7
This is used to map the local path/home/yotsuki/datadir to the database directory and the 3306 port of the database.
Modify the application configuration file
Since the IP address of the Docker container is not fixed, the application container later intends to connect to the database using the--link method, so the database connection in the application configuration file needs to be changed from IP to hostname, which is defined as DEMODB.
//"ConnectionString":"server=127.0.0.1;userid=root;persistsecurityinfo=True;database=pmis;password=123456;port=3306;", "ConnectionString":"server=demodb;userid=root;persistsecurityinfo=True;database=pmis;password=123456;port=3306;",
Writing dockerfile files
# Version:0.0.1FROM ubuntu:14.04MAINTAINER yotsukiRUN cp /etc/apt/sources.list /etc/apt/sources.list_backupCOPY ./sources.list /etc/apt/sources.listRUN apt-get install libunwind8 liblttng-ust0 libcurl3 libssl1.0.0 libuuid1 libkrb5-dev zlib1g libicu52 -yRUN apt-get updateRUN mkdir /usr/bin/pmis_webCOPY ./publish /usr/bin/pmis_webWORKDIR /usr/bin/pmis_webENTRYPOINT ./PMIS.WebAPIsEXPOSE 80
I used to start building containers from the official ubuntu14.04 image.
Sources.list is a domestic visit to the official mirror too slow, I made a 163 image file directly into the copy (I know this is not good, but this is not the point)
According to the official Microsoft documentation, you need to install the following packages to run the. NET core program, so you first use Apt-get to install the required packages
apt-get install libunwind8 liblttng-ust0 libcurl3 libssl1.0.0 libuuid1 libkrb5-dev zlib1g libicu52 -yapt-get update
Then create the directory and copy the program files in
RUN mkdir /usr/bin/pmis_webCOPY ./publish /usr/bin/pmis_webWORKDIR /usr/bin/pmis_web
Finally set the start command and expose the port
So the Dockerfile is written and done.
sudo docker build -t demo-web .
Run mirror
sudo docker run -d --name demowebapp --link demo-mysql:demodb -p 80:80 demo-web
Open our browser and enter 127.0.0.1 to see that the program is running.
This is where a simple docker ASP. NET core application is deployed.
Summarize
Because the cross-platform nature of. NET core guarantees that. NET can embrace Docker, this is a great thing.
In learning Docker, I sensed that the containerized. NET program was different from what we had developed before. For example, the database link in the configuration file, although I am the IP demodb, and use the--link link, but this is I pre-compiled Docker image before the fixed name. I think it might be better to pass in the Docker Run command or specify the local configuration file with the-v command. The latter does not have to change the program, the former still need to use the environment variable (my previous program basically without environment variables).
Hopefully, you'll have the opportunity to practice the. NET core program in Docker in the future.
Deploying ASP. NET core application practices with Docker