小試阿里雲容器服務 之后,接下來有一個擋不住的小試沖動——用docker容器運行程序。首先想到的程序是 ASP.NET 5示例程序,於是參考msdn博客中的這篇博文 Running ASP.NET 5 applications in Linux Containers with Docker 小試了一下。
首先連上阿里雲容器服務的群集,然后用 docker pull 命令從 hub.docker.com 上下載 ASP.NET 5的docker鏡像(我們選用基於coreclr的ASP.NET 5):
docker pull microsoft/aspnet:1.0.0-rc1-update1-coreclr
問題
基於阿里雲容器服務,下載docker鏡像時沒有下載進度指示,如果下載速度慢,干等的感覺讓人難受(這是docker smarm的問題,詳見Docker client via Swarm hangs for the first pull)。
c6bca914b855b44f2af39ca74ce4a6b7c-node1: Pulling microsoft/aspnet:1.0.0-rc1-update1-coreclr...
c6bca914b855b44f2af39ca74ce4a6b7c-node2: Pulling microsoft/aspnet:1.0.0-rc1-update1-coreclr...
如果不用阿里雲容器服務,基於自己運行的docker daemon,就會有下載進度指示。
9ee13ca3b908: Downloading 524.4 kB/51.35 MB
23cb15b0fcec: Download complete
ec73306ee200: Download complete
d376f9e966df: Download complete
7a4b50ae736b: Download complete
a3e8edf3e8fc: Downloading 1.08 MB/67.12 MB
e07cc3a60cb9: Download complete
52159185b7a9: Downloading 786.4 kB/18.2 MB
38903d7083ef: Downloading 401 kB/713.3 kB
4b6ac5688c98: Download complete
繼續
下載好ASP.NET 5鏡像之后,接着就用這個鏡像運行容器。
docker run -it microsoft/aspnet:1.0.0-rc1-update1-coreclr
如果出現下面的命令提示符,說明容器已經成功啟動了。
root@4bc82a74681c:/#
然后運行dnvm命令驗證一下:
root@4bc82a74681c:/# dnvm list
Active Version Runtime Architecture OperatingSystem Alias
------ ------- ------- ------------ --------------- -----
1.0.0-rc1-update1 coreclr x64 linux default
接下來,我們要基於這個容器創建包含ASP.NET 5示例程序的容器。
先用exit命令退出容器,接着從github下載ASP.NET 5示例程序。
git clone git@github.com:aspnet/Home.git
cd samples/1.0.0-rc1-update1/HelloWeb/
然后修改一下當前文件夾中的Dockerfile文件,將 1.0.0-rc1-update1 改為 1.0.0-rc1-update1-coreclr:
FROM microsoft/aspnet:1.0.0-rc1-update1-coreclr
COPY . /app
WORKDIR /app
RUN ["dnu", "restore"]
EXPOSE 5004
ENTRYPOINT ["dnx", "-p", "project.json", "web"]
緊接着我們基於這個Dockerfile用docker build命令生成一個新的容器鏡像:
docker build -t aspnet5-hello-web /git/Home/samples/1.0.0-rc1-update1/HelloWeb
生成過程中的輸出如下:
Sending build context to Docker daemon 321 kB
Step 1 : FROM microsoft/aspnet:1.0.0-rc1-update1-coreclr
---> 4b6ac5688c98
Step 2 : COPY . /app
---> 5eb606a7926f
Removing intermediate container c8f1d23fc130
Step 3 : WORKDIR /app
---> Running in 0b2a32a9a251
---> b5f1d718f699
Removing intermediate container 0b2a32a9a251
Step 4 : RUN dnu restore
---> Running in 7d1f6154e72b
Microsoft .NET Development Utility CoreClr-x64-1.0.0-rc1-16231
....
Restore complete, 83316ms elapsed
...
Installed:
120 package(s) to /root/.dnx/packages
---> d469c112c0a0
Removing intermediate container 7d1f6154e72b
Step 5 : EXPOSE 5004
---> Running in 6b5760820818
---> 7aa563e208ee
Removing intermediate container 6b5760820818
Step 6 : ENTRYPOINT dnx -p project.json web
---> Running in a513a06fd393
---> 4cb553854bff
Removing intermediate container a513a06fd393
Successfully built 4cb553854bff
docker build成功之后,通過docker images可以看到我們創建的aspnet5-hello-web鏡像。緊接着我們用這個新的鏡像運行容器:
docker run -t -d -p 8080:5004 --name aspnet5-hello-web aspnet5-hello-web
成功運行之后,我們進入阿里雲容器服務控制台看一下(訪問路徑:集群->點擊集群名稱->點擊節點IP->容器列表),在容器列表中就會看到我們剛剛運行的容器:
最后,通過瀏覽器訪問http://節點IP:8080
,就能訪問托管於阿里雲容器服務運行於docker容器中的ASP.NET 5示例站點:
搞定!