通过第一部分中的介绍,我们知道使用一个 Dockerfile 模板文件,可以让用户很方便的定义一个单独的应用容器。然而,在日常工作中,经常会碰到需要多个容器相互配合来完成某项任务的情况。例如要实现一个 Web 项目,除了 Web 服务容器本身,往往还需要再加上后端的数据库服务容器,甚至还包括负载均衡容器等。
ubuntu@ip-172-31-16-20:~/dotnet$ docker-compose up -d --build Creating network "dotnet_default" with the default driver Building dockerwebapp Step 1/7 : FROM microsoft/dotnet:latest ---> 2ac9a416f201 Step 2/7 : WORKDIR /app ---> Using cache ---> 9449738bc5a1 Step 3/7 : COPY . /app ---> 63468e7420e1 Step 4/7 : RUN dotnet restore ---> Running in 24ecd554e076 Restore completed in 75.73 ms for /app/docker.web.csproj. Restoring packages for /app/docker.web.csproj... Restore completed in 464.93 ms for /app/docker.web.csproj. Removing intermediate container 24ecd554e076 ---> cdc5f195aa36 Step 5/7 : EXPOSE 5000 ---> Running in af5717cc39b8 Removing intermediate container af5717cc39b8 ---> afe466d24f6d Step 6/7 : ENV ASPNETCORE_URLS http://*:5000 ---> Running in d151cad8058d Removing intermediate container d151cad8058d ---> d4e8dadfe89c Step 7/7 : ENTRYPOINT ["dotnet","run"] ---> Running in de41a97d97a8 Removing intermediate container de41a97d97a8 ---> 05c1f1c48653 Successfully built 05c1f1c48653 Successfully tagged dotnet_dockerwebapp:latest Building reverse-proxy Step 1/2 : FROM nginx ---> ae513a47849c Step 2/2 : COPY nginx.conf /etc/nginx/nginx.conf ---> 458d9128600f Successfully built 458d9128600f Successfully tagged dotnet_reverse-proxy:latest Creating reverse-proxy-nginx ... done Creating dockerwebapp ... done
验证启动的服务:
1
sudo docker-compose ps
显示结果:
1 2 3 4 5 6
ubuntu@ip-172-31-16-20:~/dotnet$ docker-compose ps Name Command State Ports -------------------------------------------------------------------------------- dockerwebapp dotnet run Up 5000/tcp reverse-proxy-nginx nginx -g daemon off; Up 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
ubuntu@ip-172-31-16-20:~/dotnet$ sudo docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq) Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.37/containers/json?all=1: dial unix /var/run/docker.sock: connect: permission denied "docker inspect" requires at least 1 argument. See 'docker inspect --help'.
ubuntu@ip-172-31-16-20:~/dotnet/conf$ curl http://127.0.0.1:8080 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p>
<p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p> </body> </html> ubuntu@ip-172-31-16-20:~/dotnet/conf$
ubuntu@ip-172-31-16-20:~$ sudo docker run --rm -it microsoft/dotnet root@b2ce07e9f5e5:/# dotnet new mvc -n docker.web The template "ASP.NET Core Web App (Model-View-Controller)" was created successfully. This template contains technologies from parties other than Microsoft, see https://aka.ms/template-3pn for details.
Processing post-creation actions... Running 'dotnet restore' on docker.web/docker.web.csproj... Restoring packages for /docker.web/docker.web.csproj... Generating MSBuild file /docker.web/obj/docker.web.csproj.nuget.g.props. Generating MSBuild file /docker.web/obj/docker.web.csproj.nuget.g.targets. Restore completed in 1.69 sec for /docker.web/docker.web.csproj. Restoring packages for /docker.web/docker.web.csproj... Restore completed in 338.5 ms for /docker.web/docker.web.csproj.
Restore succeeded.
root@b2ce07e9f5e5:/# cd docker.web root@b2ce07e9f5e5:/docker.web# dotnet run warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35] No XML encryptor configured. Key {392f49bb-6372-47a0-a34b-94b82a079812} may be persisted to storage in unencrypted form. warn: Microsoft.AspNetCore.Server.Kestrel[0] Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Error -99 EADDRNOTAVAIL address not available'. Hosting environment: Production Content root path: /docker.web Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down.
ubuntu@ip-172-31-16-20:~$ sudo docker run --rm -it \ > -v $HOME/dotnet/docker.web:/app \ > microsoft/dotnet:latest root@4e0ae54991c4:/# ls app boot etc lib media opt root sbin sys usr bin dev home lib64 mnt proc run srv tmp var root@4e0ae54991c4:/# cd app root@4e0ae54991c4:/app# ls Controllers Startup.cs appsettings.json docker.web.csproj Models Views bin obj Program.cs appsettings.Development.json bundleconfig.json wwwroot root@4e0ae54991c4:/app# dotnet run warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35] No XML encryptor configured. Key {3600e0ea-f9cc-4c43-b75f-58665338c060} may be persisted to storage in unencrypted form. warn: Microsoft.AspNetCore.Server.Kestrel[0] Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Error -99 EADDRNOTAVAIL address not available'. Hosting environment: Production Content root path: /app Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down.
parallelism:一次更新容器的数量。 delay:更新一组容器之间的等待时间。 failure_action:更新失败时该怎么做。 继续,回滚或暂停之一(默认:暂停)。 monitor:每次任务更新后监视失败的时间(ns | us | ms | s | m | h)(默认为0)。 max_failure_ratio:在更新期间容忍的失败率。 order:更新期间的操作顺序。 停止优先(旧任务在开始新任务之前停止)或者先启动(首先启动新任务,并且正在运行的任务短暂重叠)(默认停止优先)注意:只支持v3.4及更高版本。
# Hit the local web app test: ["CMD", "curl", "-f", "http://localhost"]
# As above, but wrapped in /bin/sh. Both forms below are equivalent. test: ["CMD-SHELL", "curl -f http://localhost || exit 1"] test: curl -f https://localhost || exit 1
ps 为我们提供了进程的一次性的查看,它所提供的查看结果并不动态连续的;如果想对进程时间监控,应该用 top 工具。
kill 命令用于杀死进程。
linux上进程有5种状态
运行(正在运行或在运行队列中等待)
中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号)
不可中断(收到信号不唤醒和不可运行, 进程必须等待直到有中断发生)
僵死(进程已终止, 但进程描述符存在, 直到父进程调用wait4()系统调用后释放)
停止(进程收到SIGSTOP, SIGSTP, SIGTIN, SIGTOU信号后停止运行运行)
ps工具标识进程的5种状态码
D 不可中断 uninterruptible sleep (usually IO) R 运行 runnable (on run queue) S 中断 sleeping T 停止 traced or stopped Z 僵死 a defunct (”zombie”) process
ps命令
** 命令格式:** ps [参数]
** 命令功能:** 用来显示当前进程的状态
** 命令参数:**
a 显示所有进程 -a 显示同一终端下的所有程序 -A 显示所有进程 c 显示进程的真实名称 -N 反向选择 -e 等于“-A” e 显示环境变量 f 显示程序间的关系 -H 显示树状结构 r 显示当前终端的进程 T 显示当前终端的所有程序 u 指定用户的所有进程 -au 显示较详细的资讯 -aux 显示所有包含其他使用者的行程 -C<命令> 列出指定命令的状况 –lines<行数> 每页显示的行数 –width<字符数> 每页显示的字符数 –help 显示帮助信息 –version 显示版本显示
** 使用实例 **
ps -A:显示所有进程信息
ps -u root:显示指定用户信息
ps -ef|grep ssh:ps 与grep 常用组合用法,查找特定进程
ps -l:将目前属于您自己这次登入的 PID 与相关信息列示出来
1 2 3 4 5
[root@localhost Desktop]# ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 2774 2722 0 80 0 - 47498 do_wai pts/0 00:00:00 su 4 S 0 2780 2774 0 80 0 - 29075 do_wai pts/0 00:00:00 bash 0 R 0 23118 2780 0 80 0 - 37235 - pts/0 00:00:00 ps
** 说明:**
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
F 代表这个程序的旗标 (flag), 4 代表使用者为 super user S 代表这个程序的状态 (STAT),关于各 STAT 的意义将在内文介绍 UID 程序被该 UID 所拥有 PID 就是这个程序的 ID ! PPID 则是其上级父程序的ID C CPU 使用的资源百分比 PRI 这个是 Priority (优先执行序) 的缩写,详细后面介绍 NI 这个是 Nice 值,在下一小节我们会持续介绍 ADDR 这个是 kernel function,指出该程序在内存的那个部分。如果是个 running的程序,一般就是 "-" SZ 使用掉的内存大小 WCHAN 目前这个程序是否正在运作当中,若为 - 表示正在运作 TTY 登入者的终端机位置 TIME 使用掉的 CPU 时间。 CMD 所下达的指令为何 在预设的情况下, ps 仅会列出与目前所在的 bash shell 有关的 PID 而已,所以, 当我使用 ps -l 的时候,只有三个 PID。
USER //用户名 %CPU //进程占用的CPU百分比 %MEM //占用内存的百分比 VSZ //该进程使用的虚拟內存量(KB) RSS //该进程占用的固定內存量(KB)(驻留中页的数量) STAT //进程的状态 START //该进程被触发启动时间 TIME //该进程实际使用CPU运行的时间
其中STAT状态位常见的状态字符有
1 2 3 4 5 6 7 8 9 10 11 12 13
D //无法中断的休眠状态(通常 IO 的进程); R //正在运行可中在队列中可过行的; S //处于休眠状态; T //停止或被追踪; W //进入内存交换 (从内核2.6开始无效); X //死掉的进程 (基本很少见); Z //僵尸进程; < //优先级高的进程 N //优先级较低的进程 L //有些页被锁进内存; s //进程的领导者(在它之下有子进程); l //多线程,克隆线程(使用 CLONE_THREAD, 类似 NPTL pthreads); + //位于后台的进程组;
[36mci-build_1 |[0m Generating MSBuild file /src/DotNetCoreApp/obj/DotNetCoreApp.csproj.nuget.g.targets. [36mci-build_1 |[0m Restore completed in 30.06 sec for /src/DotNetCoreApp/DotNetCoreApp.csproj. [36mci-build_1 |[0m Restoring packages for /src/DotNetCoreApp/DotNetCoreApp.csproj...
[36mci-build_1 |[0m Restore completed in 2.64 sec for /src/DotNetCoreApp/DotNetCoreApp.csproj.
[36mci-build_1 |[0m Microsoft (R) Build Engine version 15.6.84.34536 for .NET Core [36mci-build_1 |[0m Copyright (C) Microsoft Corporation. All rights reserved. [36mci-build_1 |[0m
[36mci-build_1 |[0m Restore completed in 97.25 ms for /src/DotNetCoreApp/DotNetCoreApp.csproj. [36mci-build_1 |[0m Restore completed in 24.37 ms for /src/DotNetCoreApp/DotNetCoreApp.csproj.