咱們知道容器是一個打包了應用和相關依賴的盒子,那怎麼去操控這個盒子呢? 這一篇我經過一個簡單的aspnetcore程序來加深對盒子的理解,使用以前先html
安裝一下Docker的環境。node
一:Docker的安裝linux
官方下載地址:https://docs.docker.com/install/linux/docker-ce/centos/ ,跟着文檔執行完下面2個大步驟便可。git
1. SET UP THE REPOSITORYweb
sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
2. INSTALL DOCKER CEdocker
sudo yum install docker-ce docker-ce-cli containerd.io
3. 啓動docker 服務shell
[root@localhost ~]# service docker start Redirecting to /bin/systemctl start docker.service
4. 查看docker版本json
[root@localhost ~]# docker -v Docker version 18.09.2, build 6247962
二:新建asp.net core 網站ubuntu
docker已經有了,接下來新建一個website,採用vs默認的mvc模板便可,有了網站以後新建一個Dockerfile文件,內容以下,目的就是爲了製做鏡像,centos
關於dockerfile的更多內容我會放到後面的文章中去說,本篇關注點在容器上。
FROM microsoft/dotnet:2.2-aspnetcore-runtime LABEL hxc@qq.com RUN mkdir /data COPY ./ /data WORKDIR /data EXPOSE 80 CMD [ "dotnet","WebNotebook.dll" ]
而後點擊vs的發佈功能,把發佈後的內容經過ftp push 到虛擬機中/data/publish文件夾下,具體內容以下:
[root@localhost publish]# ls appsettings.Development.json web.config WebNotebook.pdb WebNotebook.Views.pdb appsettings.json WebNotebook.deps.json WebNotebook.runtimeconfig.json wwwroot Dockerfile WebNotebook.dll WebNotebook.Views.dll [root@localhost publish]#
接下來經過 docker build 的方式構建出webnotebook的鏡像。
[root@localhost publish]# docker build -f ./Dockerfile -t huangxincheng/webnodebook:v1 . Sending build context to Docker daemon 4.201MB Step 1/7 : FROM microsoft/dotnet:2.2-aspnetcore-runtime 2.2-aspnetcore-runtime: Pulling from microsoft/dotnet 6ae821421a7d: Already exists 8a3a416e6dac: Already exists ed82f21723d8: Already exists 4f77997e649d: Already exists Digest: sha256:988829fbff8cde8b431d045d2dd2e3ea54d69abf5c61c69794c523535ce382e7 Status: Downloaded newer image for microsoft/dotnet:2.2-aspnetcore-runtime ---> dad26d192ae6 Step 2/7 : MAINTAINER hxc@qq.com ---> Running in 89b9a2c5bec6 Removing intermediate container 89b9a2c5bec6 ---> 2d6c5ecba6d2 Step 3/7 : RUN mkdir /data ---> Running in 08e8d316c3da Removing intermediate container 08e8d316c3da ---> 7d28be2a2bc1 Step 4/7 : COPY ./ /data ---> 9f603790a8a2 Step 5/7 : WORKDIR /data ---> Running in 85fbed40b4f7 Removing intermediate container 85fbed40b4f7 ---> 8c18eaadc85b Step 6/7 : EXPOSE 80 ---> Running in 0eaa5046ee1d Removing intermediate container 0eaa5046ee1d ---> 9b6ee99c1875 Step 7/7 : CMD [ "dotnet","WebNotebook.dll" ] ---> Running in 7c0d2fa1eb46 Removing intermediate container 7c0d2fa1eb46 ---> 513950255443 Successfully built 513950255443 Successfully tagged huangxincheng/webnodebook:v1
最後能夠經過 docker images 查看一下鏡像是否構建成功,從下圖中能夠看到,當前有一個個人應用鏡像,還有一個runtime鏡像。
[root@localhost publish]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE huangxincheng/webnodebook v1 513950255443 38 seconds ago 264MB microsoft/dotnet 2.2-aspnetcore-runtime dad26d192ae6 4 days ago 260MB
三:容器操做
1. 啓動容器
一般啓動容器的方式有兩種,第一種爲docker create, 還有一種是docker run,更多的狀況咱們使用docker run 方式,好比下面的命令。
[root@localhost publish]# docker run --name webnotebook -d huangxincheng/webnodebook:v1 51ded69fce15fb912ab167c4dea26535a17b0f2147a5571aaa411a974ab95b11
《1》 --name: 這個就是給你當前運行的容器取一個名字,若是不取的話就採用系統給你的默認名字。
《2》 -d: 表示脫離shell終端採用deamon形式的後臺運行,在生產環境中必需要這麼處理,沒毛病吧。
《3》 huangxincheng/webnodebook:v1 : huangxincheng表示倉庫名,固然你也能夠不須要這個。webnodebook 表示鏡像名, v1表示鏡像的版本。
容器運行成功後,會返回一個容器的標識碼,這裏要注意的是,若是鏡像在本地不存在,docker 默認會到官方的 hub.docker.com上進行拉取。
2. 查看容器
既然你說容器啓動了,那我怎麼判別是否真的啓動了呢? 這裏你就能夠用 docker ps命令鑑別。
[root@localhost publish]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 51ded69fce15 huangxincheng/webnodebook:v1 "dotnet WebNotebook.…" 6 minutes ago Up 6 minutes 80/tcp webnotebook
從上圖中能夠看到,容器的ID爲 51ded69fce15 ,而這個恰好是你docker run 返回的前12個字符,而後Status= Up 6 minutes ,說明容器是啓動的沒毛病。
3. 查看容器內應用程序是否啓動
容器啓動了不表明個人webnotebook沒問題,對吧,那接下來怎麼去鑑別容器內的程序是否正常啓動呢? 這裏就能夠用到下面兩種方式。
《1》 docker top xxx 查看容器運行的進程。
xxx 就是你的容器名或者容器ID,以下圖能夠看到確實個人WebNoteBook的進程號是5323,說明是啓動的。
[root@localhost publish]# docker top webnotebook UID PID PPID C STIME TTY TIME CMD root 5323 5306 0 00:09 ? 00:00:01 dotnet WebNotebook.dll
《2》 直接在宿主機上經過ps -ef 查看指定的程序。
從下圖中你也看到了,WebNotebook的PID果真是5323,說明啓動了無疑。
[root@localhost publish]# ps -ef | grep dotnet root 5323 5306 0 00:09 ? 00:00:01 dotnet WebNotebook.dll root 6235 3499 0 00:23 pts/1 00:00:00 grep --color=auto dotnet
4. 如何查看容器的ip和port
既然容器和容器內程序都是啓動的,那接下來我怎麼訪問這個webnotebook呢? 要想訪問,你確定是要知道這個容器的ip+port,否則怎麼訪問呢? 一般有
兩種作法能夠獲取到容器的ip和port端口。
《1》 使用docker exec 直接進入容器內
[root@localhost publish]# docker exec -it webnotebook /bin/bash root@51ded69fce15:/data# ifconfig bash: ifconfig: command not found root@51ded69fce15:/data#
-it 表示給當前的容器分配一個終端,因爲容器是ubuntu環境,爲了精簡併無把ifconfig打包進來,因此你須要使用apt-get 安裝一下net包便可。
root@51ded69fce15:/data# apt-get update && apt-get install net-tools -y Ign:1 http://cdn-fastly.deb.debian.org/debian stretch InRelease Get:3 http://cdn-fastly.deb.debian.org/debian stretch-updates InRelease [91.0 kB] Get:4 http://cdn-fastly.deb.debian.org/debian stretch Release [118 kB] Get:2 http://security-cdn.debian.org/debian-security stretch/updates InRelease [94.3 kB] Get:5 http://cdn-fastly.deb.debian.org/debian stretch-updates/main amd64 Packages [7748 B] Get:6 http://cdn-fastly.deb.debian.org/debian stretch Release.gpg [2434 B] Get:7 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 Packages [7084 kB] Get:8 http://security-cdn.debian.org/debian-security stretch/updates/main amd64 Packages [474 kB] Fetched 7871 kB in 1min 52s (69.8 kB/s) Reading package lists... Done Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: net-tools 0 upgraded, 1 newly installed, 0 to remove and 5 not upgraded. Need to get 248 kB of archives. After this operation, 963 kB of additional disk space will be used. Get:1 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 net-tools amd64 1.60+git20161116.90da8a0-1 [248 kB] Fetched 248 kB in 4s (56.4 kB/s) debconf: delaying package configuration, since apt-utils is not installed Selecting previously unselected package net-tools. (Reading database ... 6953 files and directories currently installed.) Preparing to unpack .../net-tools_1.60+git20161116.90da8a0-1_amd64.deb ... Unpacking net-tools (1.60+git20161116.90da8a0-1) ... Setting up net-tools (1.60+git20161116.90da8a0-1) ... root@51ded69fce15:/data# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 4133 bytes 8350979 (7.9 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3452 bytes 189039 (184.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 0 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 root@51ded69fce15:/data#
當你安裝完net包後,經過ifconfig你就能夠看到 172.17.0.2 這個ip地址了,而後經過netstat 來查看webnotebook的端口號。
root@51ded69fce15:/data# netstat -tlnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp6 0 0 :::80 :::* LISTEN 1/dotnet root@51ded69fce15:/data#
《2》 使用 docker inspect 查看容器各方面的底層信息。
[root@localhost ~]# docker inspect webnotebook [ ..."NetworkSettings": { "Bridge": "", "SandboxID": "cc8d9e678ef1137d992f3c8d019d81300c2b5f379bc8b746b08fe233d06ffb02", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "80/tcp": null }, "SandboxKey": "/var/run/docker/netns/cc8d9e678ef1", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "e37e565a26af790ce156dc957beab5182cd136215d61b655be26e5c31362b4ae", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "d234a2cc0ff52b802e846d12530e41d886ec780f2344cf601f022ff1c434afe4", "EndpointID": "e37e565a26af790ce156dc957beab5182cd136215d61b655be26e5c31362b4ae", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } ... ]
從NetworkSettings節點中,你能夠找到一個"IPAddress": "172.17.0.2" 的信息,這個ip就是你的容器IP,很簡單吧,並且從"Ports": { "80/tcp": null } 上
你也看到,當前暴露的是80端口。
5. 查看容器內程序日誌
容器和程序都啓動起來了,不表明程序運行就正常,爲了保險起見,仍是要看一下程序打印到終端的一些操做日誌,這樣我才放心一些不是嗎? 那如今
我能夠經過 docker logs xxx 來查看指定容器的日誌輸出,從下面能夠看到,一點問題都沒有。
[root@localhost ~]# docker logs webnotebook warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35] No XML encryptor configured. Key {8199b0ea-afae-4586-8e6d-aae75aa91db2} may be persisted to storage in unencrypted form. Hosting environment: Production Content root path: /data Now listening on: http://[::]:80 Application started. Press Ctrl+C to shut down.
6. 關閉和啓動容器
容器運行這一塊基本上就是這樣了,因爲項目不斷的迭代升級,你的舊版本程序的容器就該中止了,接下來你有 docker stop 和 docker kill 兩種方式,
二者的區別在於,前者是給你的程序 sigterm信號,後者是sigkill信號,若是不明白二者的區別,能夠看個人這篇博文:
http://www.javashuo.com/article/p-hpmdktog-k.html
[root@localhost ~]# docker stop webnotebook webnotebook [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 51ded69fce15 huangxincheng/webnodebook:v1 "dotnet WebNotebook.…" 35 minutes ago Exited (0) 2 seconds ago webnotebook [root@localhost ~]#
能夠看到,當我執行了stop以後再 ps -a 發現,當前的容器已經退出了,而後你能夠經過 docker start 重啓,再用 docker kill 關閉。
[root@localhost ~]# docker start webnotebook webnotebook [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 51ded69fce15 huangxincheng/webnodebook:v1 "dotnet WebNotebook.…" 37 minutes ago Up 2 seconds 80/tcp webnotebook [root@localhost ~]# docker kill webnotebook webnotebook [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 51ded69fce15 huangxincheng/webnodebook:v1 "dotnet WebNotebook.…" 37 minutes ago Exited (137) 1 second ago webnotebook [root@localhost ~]#
既然ip+port都知道了,接下來網站你就能夠訪問啦。訪問以前確保你的容器是start狀態哦( docker webnotebook start )。
好了,本篇就說到這裏,但願對你有幫助。