說在前面
在閱讀本文之前,您必須對 Docker 的中涉及的基本概念以及常見命令有一定了解,本文側重實戰,不會對相關概念詳述。
同時請確保您本地開發機器已完成如下安裝:
- Docker 18.06 或更高版本的 Docker 客戶端
- .NET Core SDK 2.2 或更高版本
- Visual Studio Code 代碼編輯器,以及 C# 語法插件 1.17.1 或更高版本
注:本文實驗環境是 Ubuntu 18.04 LTS。如果您的機器是 Window,也可以把 Docker 裝在虛擬機或服務器上。
創建演示項目
開始之前要先准備一個需要 Docker 容器化的 ASP.NET Core 應用程序,用於下面的操作演示。這里我用 .NET Core CLI 快速搭建一個全新的 Web API 項目。
啟動 VS Code,打開集成終端,輸入如下命令:
dotnet new webapi -o TodoApi
code -r TodoApi
以上便創建了一個名為TodoApi
的 Web API 樣板項目。
打開集成終端,輸入dotnet run
命令編譯運行程序,然后打開瀏覽器跳轉到 URL http://localhost:5000/api/values
,如正常返回如下 JSON 數據,說明應用程序本地成功運行。
["value1","value2"]
現在讓我們更進一步,在 Docker 中構建並運行該應用程序。
創建 Dockerfile 文件
Dockerfile 是一個文本文件,其用來定義單個容器的內容和啟動行為,按順序包含構建鏡像所需的所有指令。Docker 會通過讀取 Dockerfile 中的指令自動構建鏡像。
在項目TodoApi
根目錄中,創建一個名為Dockerfile
的文件,並粘貼以下內容:
FROM microsoft/dotnet:2.2-sdk AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM microsoft/dotnet:2.2-aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "TodoApi.dll"]
FROM
指令必須放在第一位,用於初始化鏡像,為后面的指令設置基礎鏡像。WORKDIR
指令為其他指令設置工作目錄,如果不存在,則會創建該目錄。COPY
指令會從源路徑復制新文件或目錄,並將它們添加到路徑目標容器的文件系統中。RUN
指令可以在當前鏡像之上的新 層 中執行任何命令並提交結果,生成的已提交鏡像將用於 Dockerfile 中的下一步。ENTRYPOINT
指令支持以可執行文件的形式運行容器。
有關 Dockerfile 中指令用法的更多信息請參閱 Dockerfile reference。
同時,為了避免構建項目中的一些調試生成文件,可以在項目文件夾中新增.dockerignore
文件,並粘貼如下內容:
bin\
obj\
構建應用容器鏡像
在項目TodoApi
根目錄中,打開集成終端,執行如下命令構建容器鏡像:
docker build -t todoapi .
-t
參數用來指定鏡像的名字及標簽,通常是name:tag
或者name
格式。本例todoapi
便是我們給鏡像起的名字,沒有設置標簽即使用默認標簽latest
。
如命令執行成功,終端會有類似如下輸出:
$ docker build -t todoapi .
Sending build context to Docker daemon 1.137MB
Step 1/10 : FROM microsoft/dotnet:2.2-sdk AS build-env
2.2-sdk: Pulling from microsoft/dotnet
e79bb959ec00: Pull complete
d4b7902036fe: Pull complete
1b2a72d4e030: Pull complete
d54db43011fd: Pull complete
b3ae1535ac68: Pull complete
f04cf82b07ad: Pull complete
6f91a9d92092: Pull complete
Digest: sha256:c443ff79311dde76cb1acf625ae47581da45aad4fd66f84ab6ebf418016cc008
Status: Downloaded newer image for microsoft/dotnet:2.2-sdk
---> e268893be733
Step 2/10 : WORKDIR /app
---> Running in c7f62130f331
Removing intermediate container c7f62130f331
---> e8b6a73d3d84
Step 3/10 : COPY *.csproj ./
---> cfa03afa6003
Step 4/10 : RUN dotnet restore
---> Running in d96a9b89e4a9
Restore completed in 924.67 ms for /app/TodoApi.csproj.
Removing intermediate container d96a9b89e4a9
---> 14d5d32d40b6
Step 5/10 : COPY . ./
---> b1242ea0b0b8
Step 6/10 : RUN dotnet publish -c Release -o out
---> Running in 37c8eb07c86e
Microsoft (R) Build Engine version 16.0.450+ga8dc7f1d34 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 663.74 ms for /app/TodoApi.csproj.
TodoApi -> /app/bin/Release/netcoreapp2.2/TodoApi.dll
TodoApi -> /app/out/
Removing intermediate container 37c8eb07c86e
---> 6238f4c1cf07
Step 7/10 : FROM microsoft/dotnet:2.2-aspnetcore-runtime
2.2-aspnetcore-runtime: Pulling from microsoft/dotnet
27833a3ba0a5: Pull complete
25dbf7dc93e5: Pull complete
0ed9cb15d3b8: Pull complete
874ea13b7488: Pull complete
Digest: sha256:ffd756d34bb0f976ba5586f6c88597765405af8014ae51b34811992b46ba40e8
Status: Downloaded newer image for microsoft/dotnet:2.2-aspnetcore-runtime
---> cb2dd04458bc
Step 8/10 : WORKDIR /app
---> Running in b0a3826d346b
Removing intermediate container b0a3826d346b
---> 4218db4cc2f5
Step 9/10 : COPY --from=build-env /app/out .
---> 765168aa2c7a
Step 10/10 : ENTRYPOINT ["dotnet", "TodoApi.dll"]
---> Running in f93bcaf5591f
Removing intermediate container f93bcaf5591f
---> 046226f5e9cb
Successfully built 046226f5e9cb
Successfully tagged todoapi:latest
如果您的機器是第一次構建,速度可能會有些慢,因為要從 Docker Hub 上拉取應用依賴的
dotnet-sdk
和aspnetcore-runtime
基礎鏡像。
構建完成后,我們可以通過docker images
命令確認本地鏡像倉庫是否存在我們構建的鏡像todoapi
。
REPOSITORY TAG IMAGE ID CREATED SIZE
todoapi latest c92a82f0efaa 19 hours ago 260MB
microsoft/dotnet 2.2-sdk 5e09f77009fa 26 hours ago 1.74GB
microsoft/dotnet 2.2-aspnetcore-runtime 08ed21b5758c 26 hours ago 260MB
...
運行應用容器
容器鏡像構建完成后,就可以使用docker run
命令運行容器了,有關該命令參數的更多信息請參閱 Reference - docker run 。
開發環境下,通常會通過docker run --rm -it
命令運行應用容器,具體命令如下:
docker run --rm -it -p 5000:80 todoapi
-it
參數表示以交互模式運行容器並為容器重新分配一個偽輸入終端,方便查看輸出調試程序。--rm
參數表示將會在容器退出后自動刪除當前容器,開發模式下常用參數。-p
參數表示會將本地計算機上的5000
端口映射到容器中的默認80
端口,端口映射的關系為host:container
。todoapi
便是我們要啟動的本地鏡像名稱。
如命令執行成功,終端會有類似如下輸出:
$ docker run -it --rm -p 5000:80 todoapi
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
No XML encryptor configured. Key {1a78d899-738b-4aea-a7d6-777302933f38} may be persisted to storage in unencrypted form.
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.
生產環境下,通常會通過docker run -d
命令運行應用容器,具體命令如下:
docker run -d --restart=always --name myapp -p 5000:80 todoapi
-d
參數表示會將容器作為服務啟動,不需要終端交互。--name
參數用來指定容器名稱,本例指定容器名稱為myapp
。--restart
是一個面向生產環境的參數,用來指定容器非正常退出時的重啟策略,本例always
表示始終重新啟動容器,其他可選策略請參考 Restart policies (--restart)。
如命令執行成功,終端會有類似如下輸出:
$ docker run -d --restart=always --name myapp -p 5000:80 todoapi
e3d747d9d2b4cd14b2acb24f81bea9312f89c4eb689dba5f6559950c91db1600
容器啟動后,在 Web 瀏覽器中再次訪問http://localhost:5000/api/values
,應該會和本地測試一樣返回如下 JSON 數據:
["value1","value2"]
至此,我們的 ASP.NET Core 應用就成功運行在 Docker 容器中了。
多容器應用部署
目前我們創建的演示項目TodoApi
過於簡單,真實的生產項目肯定會涉及更多其他的依賴。例如:關系數據庫 Mysql、文檔數據庫 MongoDB、分布式緩存 Redis、消息隊列 RabbitMQ 等各種服務。
還有就是,生產環境我們一般不會將 ASP.NET Core 應用程序的宿主服務器 Kestrel 直接暴露給用戶,通常是在前面加一個反向代理服務 Nginx。
這些依賴服務還要像傳統部署方式那樣,一個一個單獨配置部署嗎?不用的,因為它們本身也是可以被容器化的,所以我們只要考慮如何把各個相互依賴的容器聯系到一起,這就涉及到容器編排,而 Docker Compose 正是用來解決這一問題的,最終可以實現多容器應用的一鍵部署。
Docker Compose 是一個用於定義和運行多容器的 Docker 工具。其使用
YAML
文件來配置應用程序的服務,最終您只要使用一個命令就可以從配置中創建並啟動所有服務。
安裝 Docker Compose
Linux 系統下的安裝過程大致分為以下幾步:
Step1:運行如下命令下載 Compose 最新穩定版本,截止發稿前最新版本為1.24.0
。
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Step2:對下載完成的二進制程序添加可執行權限。
sudo chmod +x /usr/local/bin/docker-compose
Step3:測試安裝是否成功。
$ docker-compose --version
docker-compose version 1.24.0, build 0aa59064
若您在安裝過程中遇到問題,或是其他系統安裝請參閱 Install Docker Compose。
改造演示項目
現在來改造一下我們的演示項目TodoApi
,添加 Redis 分布式緩存、使用 Nginx 做反向代理,准備構建一個具如下圖所示架構的多容器應用。
在TodoApi
項目根目錄下,打開集成終端,輸入如下命令新增 Redis 依賴包。
dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis --version 2.2.0
修改應用啟動配置文件Startup.cs
中的ConfigureServices
方法:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = Configuration.GetConnectionString("Redis");
});
services.AddHttpContextAccessor();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
在TodoApi
項目Controllers
目錄下新建控制器HelloController
,具體代碼如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
namespace TodoApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class HelloController : ControllerBase
{
private readonly IDistributedCache _distributedCache;
private readonly IHttpContextAccessor _httpContextAccessor;
public HelloController(
IDistributedCache distributedCache,
IHttpContextAccessor httpContextAccessor)
{
_distributedCache = distributedCache;
_httpContextAccessor = httpContextAccessor;
}
[HttpGet]
public ActionResult<string> Get()
{
var connection = _httpContextAccessor.HttpContext.Connection;
var ipv4 = connection.LocalIpAddress.MapToIPv4().ToString();
var message = $"Hello from Docker Container:{ipv4}";
return message;
}
[HttpGet("{name}")]
public ActionResult<string> Get(string name)
{
var defaultKey = $"hello:{name}";
_distributedCache.SetString(defaultKey, $"Hello {name} form Redis");
var message = _distributedCache.GetString(defaultKey);
return message;
}
}
}
以上控制器,提供了兩個接口/api/hello
和/api/hello/{name}
,分別用來測試 Nginx 負載均衡和 Redis 的聯通性。
創建 docker-compose.yml
准備工作就緒,下面我們就可以使用 Docker Compose 來編排容器。
同樣是在TodoApi
項目根目錄中,創建一個名為docker-compose.yml
的文件,並粘貼以下內容:
version: "3.7"
services:
myproject-todoapi-1:
container_name: my-todoapi-1
build:
context: .
dockerfile: Dockerfile
restart: always
ports:
- "5001:80"
volumes:
- ./appsettings.json:/app/appsettings.json
myproject-todoapi-2:
container_name: my-todoapi-2
build:
context: .
dockerfile: Dockerfile
restart: always
ports:
- "5002:80"
volumes:
- ./appsettings.json:/app/appsettings.json
myproject-todoapi-3:
container_name: my-todoapi-3
build:
context: .
dockerfile: Dockerfile
restart: always
ports:
- "5003:80"
volumes:
- ./appsettings.json:/app/appsettings.json
myproject-nginx:
container_name: my-nginx
image: nginx
restart: always
ports:
- "80:80"
volumes:
- ./conf/nginx.conf:/etc/nginx/conf.d/default.conf
myproject-redis:
container_name: my-redis
image: redis
restart: always
ports:
- "6379:80"
volumes:
- ./conf/redis.conf:/etc/redis/redis.conf
其中version
用來指定 Compose 文件版本號,3.7
是目前最新版本,具體哪些版本對應哪些特定的 Docker 引擎版本請參閱 Compose file versions and upgrading。
Compose 中強化了服務的概念,簡單地理解就是, 服務是一種用於生產環境的容器。一個多容器 Docker 應用由若干個服務組成,如上文件即定義了 5 個服務:
- 3 個應用服務
myproject-todoapi-1
、myproject-todoapi-2
和myproject-todoapi-3
- 1 個 Nginx 服務
myproject-reverse-proxy
- 1 個 Redis 服務
myproject-redis
以上 5 個服務的配置參數相差無幾、也很簡單,我就不展開敘述,不清楚的可以參閱 Compose file reference。
這里只講一個配置參數volumes
:
我們知道,容器中的文件在宿主機上存在形式復雜,修改文件需要先通過如下命令進入容器后操作。
docker exec -it <CONTAINER ID/NAMES> /bin/bash
容器一旦刪除,其內部配置以及產生的數據也會丟失。
為了解決這些問題,Docker 引入了數據卷 volumes 機制。即 Compose 中 volumes 參數用來將宿主機的某個目錄或文件映射掛載到 Docker 容器內部的對應的目錄或文件,通常被用來靈活掛載配置文件或持久化容器產生的數據。
PS:自己動手編寫
docker-compose.yml
的時候,可以嘗試實驗更多場景。比如:新增一個 MySQL 依賴服務、把容器內產生的數據持久化到宿主機等等。
創建相關配置文件
接下來,需要根據如上docker-compose.yml
文件中涉及的volumes
配置創建三個配置文件。要知道,它們最終是需要被注入到 Docker 容器中的。
首先,在TodoApi
項目根目錄中,創建三個應用服務myproject-todoapi-*
需要的程序配置文件appsettings.json
,具體內容如下:
"ConnectionStrings": {
"Redis": "myproject-redis:6379,password=todoapi@2019"
},
以上配置,指定了 Redis 服務myproject-redis
的連接字符串,其中myproject-redis
可以看到是 Redis 服務的服務名稱,當該配置文件注入到 Docker 容器中后,會自動解析為容器內部 IP,同時考慮到 Redis 服務的安全性,為其指定了密碼,即password=todoapi@2019
。
然后,在TodoApi
項目根目錄中創建一個子目錄conf
,用來存放 Nginx 和 Redis 的配置文件。
mkdir conf && cd conf
先來創建 Redis 服務myproject-redis
的配置文件。
可以通過如下命令,下載一個 Redis 官方提供的標准配置文件redis.conf
:
wget http://download.redis.io/redis-stable/redis.conf
然后打開下載后的redis.conf
文件,找到SECURITY
節點,根據如上應用服務的 Redis 連接字符串信息,啟用並改下密碼:
requirepass todoapi@2019
再來創建 Nginx 服務myproject-nginx
的配置文件。
在conf
目錄中,創建一個名為nginx.conf
的配置文件,並粘貼如下內容:
upstream todoapi {
server myproject-todoapi-1:80;
server myproject-todoapi-2:80;
server myproject-todoapi-3:80;
}
server {
listen 80;
location / {
proxy_pass http://todoapi;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
以上配置,是一個 Nginx 中具備負載均衡的代理配置,其默認采用輪循策略將請求轉發給 Docker 服務myproject-todoapi-1
、myproject-todoapi-2
和myproject-todoapi-3
。
運行並測試多容器應用
經過以上幾個小節,容器編排的過程就完成了,接下來就可以直接定義並啟動我們創建的多容器應用實例了。
切換到docker-compose.yml
文件所在的目錄,也就是TodoApi
項目的根目錄,執行如下命令:
docker-compose up -d
如命令執行成功,終端最后會有類似如下輸出:
......
Creating my-todoapi-1 ... done
Creating my-redis ... done
Creating my-todoapi-3 ... done
Creating my-nginx ... done
Creating my-todoapi-2 ... done
至此,我們的多容器應用就已經在運行了,可以通過docker-compose ps
命令來確認下。
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------
my-nginx nginx -g daemon off; Up 0.0.0.0:80->80/tcp
my-redis docker-entrypoint.sh redis ... Up 6379/tcp, 0.0.0.0:6379->80/tcp
my-todoapi-1 dotnet TodoApi.dll Up 0.0.0.0:5001->80/tcp
my-todoapi-2 dotnet TodoApi.dll Up 0.0.0.0:5002->80/tcp
my-todoapi-3 dotnet TodoApi.dll Up 0.0.0.0:5003->80/tcp
可以通過連續三次請求/api/hello
接口測試應用的負載均衡。
curl http://localhost/api/hello
curl http://localhost/api/hello
curl http://localhost/api/hello
// Output:
Hello from Docker Container:172.30.0.2
Hello from Docker Container:172.30.0.4
Hello from Docker Container:172.30.0.5
三個應用服務分別部署在不同容器中,所以理論上來講,他們的容器內部 IP 也是不同的,所以/api/hello
接口每次輸出信息不會相同。
請求/api/hello/{name}
接口測試 Redis 服務連通性。
curl http://localhost/api/hello/esofar
// Output:
Hello esofar form Redis
小結
本文從零構建了一個 ASP.NET Core 應用,並通過 Docker 部署,然后由淺入深,引入 Docker Compose 演示了多容器應用的部署過程。通過本文的實戰您可以更深入地了解 Docker。本文涉及的代碼已托管到以下地址,您在實驗過程中遇到問題可以參考。
https://github.com/esofar/dockerize-aspnetcore-samples
Docker 命令附錄
$ docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
engine Manage the docker engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Docker Compose 命令附錄
$ docker-compose --help
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file
(default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name
(default: directory name)
--verbose Show more output
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi Do not print ANSI control characters
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by --tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don't check the daemon's hostname against the
name specified in the client certificate
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
--compatibility If set, Compose will attempt to convert keys
in v3 files to their non-Swarm equivalent
Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information