vim /home/console/Dockerfile
# ------
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /app
COPY . /app
RUN dotnet run
构建镜像
docker build -t wyt/console:dev .
运行容器
dotnet run --name console-dev wyt/console
基于dotnet Runtime
新建控制台程序
mkdir /home/console
cd /home/console
dotnet new console
dotnet restore
using System;
using System.Threading;
namespace console
{
class Program
{
staticvoid Main(string[] args)
{
Console.WriteLine("Hello World from docker!");
Thread.Sleep(Timeout.Infinite);
}
}
}
View Code
创建 Dockerfile 文件
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /code
COPY *.csproj /code
RUN dotnet restore
COPY . /code
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
WORKDIR /app
COPY --from=build /code/out /app
ENTRYPOINT ["dotnet", "console.dll"]
构建镜像
docker build -t wyt/console:prod .
运行容器
docker run --name=console-prod wyt/console:prod
镜像大小对比
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wyt/console prod d2c683338197 3 minutes ago 260MB
wyt/console dev 93b346366bc5 20 minutes ago 1.74GB
mcr.microsoft.com/dotnet/core/sdk 2.2 155911c343f3 11 days ago 1.74GB
mcr.microsoft.com/dotnet/core/aspnet 2.2 c56aab97bc42 11 days ago 260MB
Mysql EF Core 快速构建 web api
新建文件夹beta,创建WebAPI项目User.API,添加EFCore引用
Install-Package MySql.Data.EntityFrameworkCore
新建文件夹Data、Models 新建 AppUser.cs
namespace User.API.Models
{
publicclass AppUser
{
publicint Id { get; set; }
publicstring Name { get; set; }
publicstring Company { get; set; }
publicstring Title { get; set; }
}
}
namespace User.API
{
publicclass Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.publicvoid ConfigureServices(IServiceCollection services)
{
services.AddDbContext<UserContext>(options =>
{
options.UseMySQL(Configuration.GetConnectionString("MysqlUser"));
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.publicvoid Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
InitDatabase(app);
}
publicvoid InitDatabase(IApplicationBuilder app)
{
using (var scope=app.ApplicationServices.CreateScope())
{
var userContext = scope.ServiceProvider.GetRequiredService<UserContext>();
//userContext.Database.Migrate();if (userContext.Users.Count()==0)
{
userContext.Users.Add(new AppUser()
{
Name = "wyt"
});
userContext.SaveChanges();
}
}
}
}
}
# 初始化脚本mysql-init/init.sql
use mysql;
ALTER USER 'jeese'@'%' IDENTIFIED WITH mysql_native_password BY 'pwd123456';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'pwd123456';
FLUSH PRIVILEGES;
docker-compose构建
docker-compose build
[root@localhost User.API]# docker-compose build
db uses an image, skipping
Building web
Step 1/11 : FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
---> 155911c343f3
Step 2/11 : WORKDIR /code
---> Using cache
---> 7525de38c042
Step 3/11 : COPY *.csproj ./
---> Using cache
---> 397affedf1a6
Step 4/11 : RUN dotnet restore
---> Using cache
---> 964ce7a0de36
Step 5/11 : COPY . ./
---> Using cache
---> 5d18774ff1df
Step 6/11 : RUN dotnet publish -c Release -o out
---> Using cache
---> 3353849a8dd8
Step 7/11 : FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
---> c56aab97bc42
Step 8/11 : WORKDIR /app
---> Using cache
---> 12d1df98dc50
Step 9/11 : COPY --from=build /code/out ./
---> Using cache
---> 4e6819b010fe
Step 10/11 : EXPOSE 80
---> Using cache
---> 2ee374887860
Step 11/11 : ENTRYPOINT ["dotnet", "User.API.dll"]
---> Using cache
---> 2b06acc1b707
Successfully built 2b06acc1b707
Successfully tagged userapi_web:latest
View Code
docker-compose up
[root@localhost User.API]# docker-compose up
Creating network "userapi_default" with the default driver
Creating db ... done
Creating aspnetcore ... done
Attaching to db, aspnetcore
db | [Entrypoint] MySQL Docker Image 8.0.16-1.1.11
db | [Entrypoint] Initializing database
aspnetcore | warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
aspnetcore | No XML encryptor configured. Key {67f35cc4-a4c3-4224-ba07-7a0753ed0d09} may be persisted to storage in unencrypted form.
aspnetcore | Hosting environment: Production
aspnetcore | Content root path: /app
aspnetcore | Now listening on: http://[::]:80
aspnetcore | Application started. Press Ctrl+C to shut down.
db | 2019-07-05T09:27:43.358708Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.16) initializing of server in progress as process 20
db | 2019-07-05T09:27:43.360043Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8'is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
db | 2019-07-05T09:27:43.360052Z 0 [Warning] [MY-013244] [Server] --collation-server: 'utf8_general_ci'is a collation of the deprecated character set UTF8MB3. Please consider using UTF8MB4 with an appropriate collation instead.
db | 2019-07-05T09:27:46.943704Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
db | 2019-07-05T09:27:48.131450Z 0 [System] [MY-013170] [Server] /usr/sbin/mysqld (mysqld 8.0.16) initializing of server has completed
db | [Entrypoint] Database initialized
db | 2019-07-05T09:27:49.902213Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.16) starting as process 66
db | 2019-07-05T09:27:49.903376Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8'is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
db | 2019-07-05T09:27:49.903389Z 0 [Warning] [MY-013244] [Server] --collation-server: 'utf8_general_ci'is a collation of the deprecated character set UTF8MB3. Please consider using UTF8MB4 with an appropriate collation instead.
db | 2019-07-05T09:27:50.456492Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db | 2019-07-05T09:27:50.477501Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.16' socket: '/var/lib/mysql/mysql.sock' port: 0 MySQL Community Server - GPL.
db | 2019-07-05T09:27:50.558190Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock'
db | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab'as time zone. Skipping it.
db | Warning: Unable to load '/usr/share/zoneinfo/leapseconds'as time zone. Skipping it.
db | Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi'as time zone. Skipping it.
db | Warning: Unable to load '/usr/share/zoneinfo/zone.tab'as time zone. Skipping it.
db | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab'as time zone. Skipping it.
db |
db | [Entrypoint] running /docker-entrypoint-initdb.d/init.sql
db |
db |
db | 2019-07-05T09:27:52.092496Z 12 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.16).
db | 2019-07-05T09:27:54.191280Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.16) MySQL Community Server - GPL.
db | [Entrypoint] Server shut down
db |
db | [Entrypoint] MySQL init process done. Ready for start up.
db |
db | [Entrypoint] Starting MySQL 8.0.16-1.1.11
db | 2019-07-05T09:27:55.417600Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.16) starting as process 1
db | 2019-07-05T09:27:55.419490Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8'is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
db | 2019-07-05T09:27:55.419504Z 0 [Warning] [MY-013244] [Server] --collation-server: 'utf8_general_ci'is a collation of the deprecated character set UTF8MB3. Please consider using UTF8MB4 with an appropriate collation instead.
db | 2019-07-05T09:27:55.858661Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
db | 2019-07-05T09:27:55.880107Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.16' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server - GPL.
db | 2019-07-05T09:27:56.066024Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
aspnetcore | Application is shutting down...
aspnetcore exited with code 0
aspnetcore | Hosting environment: Production
aspnetcore | Content root path: /app
aspnetcore | Now listening on: http://[::]:80
aspnetcore | Application started. Press Ctrl+C to shut down.
请发表评论