Docker实操入门——容器管理

  • 运行容器

启动容器:docker run

[root@vm001 ~]# docker run ubuntu pwd
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
898c46f3b1a1: Pull complete 
63366dfa0a50: Pull complete 
041d4cd74a92: Pull complete 
6e1bee0f8701: Pull complete 
Digest: sha256:017eef0b616011647b269b5c65826e2e2ebddbe5d1f8c1e56b3599fb14fabec8
Status: Downloaded newer image for ubuntu:latest
/

1、Docker 客户端执行 docker run 命令。
2、Docker daemon 发现本地没有 ubuntu 镜像。
3、daemon 从 Docker Hub 下载镜像。
4、下载完成,镜像 ubuntu 被保存到本地。
5、Docker daemon 启动容器,并执行 pwd,返回的 / 是容器中的当前目录。

[root@vm001 ~]# docker run -d -p 80:80 --name web -h webserver httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
f7e2b70d04ae: Pull complete 
84006542c688: Pull complete 
dae6fe3c5e81: Pull complete 
33fc493aff90: Pull complete 
9a4113020573: Pull complete 
Digest: sha256:20ead958907f15b638177071afea60faa61d2b6747c216027b8679b5fa58794b
Status: Downloaded newer image for httpd:latest
184ac914f825c01540a12f391a38124b0491fd20306b75b762cd28e94843e2ef

启动一个http容器,-d 表示启动后在后台运行,-p 80:80 表示将容器的80端口映射到宿主机的80端口(可以通过ip:端口访问容器),–name 表示容器的名字,-h 表示容器的主机名。
常见的容器启动参数
在这里插入图片描述

  • 容器的资源限制
    cgroup 和 namespace 是最重要的两种技术。cgroup 实现资源限额,namespace 实现资源隔离。
    cgroup
    cgroup 全称 Control Group。Linux 操作系统通过 cgroup 可以设置进程使用 CPU、内存 和 IO 资源的限额。cgroup 到底长什么样子呢?我们可以在/sys/fs/cgroup 中找到它。

    [root@vm001 ~]# cd /sys/fs/cgroup/
    [root@vm001 cgroup]# ls
    blkio cpuacct cpuset freezer memory net_cls,net_prio perf_event systemd
    cpu cpu,cpuacct devices hugetlb net_cls net_prio pids
    namespace
    在每个容器中,我们都可以看到文件系统,网卡等资源,这些资源看上去是容器自己的。拿网卡来说,每个容器都会认为自己有一块独立的网卡,即使 host 上只有一块物理网卡。这种方式非常好,它使得容器更像一个独立的计算机。
    Linux 实现这种方式的技术是 namespace。namespace 管理着 host 中全局唯一的资源,并可以让每个容器都觉得只有自己在使用它。换句话说,namespace 实现了容器间资源的隔离。
    Linux 使用了六种 namespace,分别对应六种资源:Mount、UTS、IPC、PID、Network 和 User。
    以下是容器资源限制的常用参数
    在这里插入图片描述
    内存限额:允许容器最多使用500M内存和100M的Swap:

    [root@vm001 cgroup]# docker run -d --name nginx03 --memory=“500m” --memory-swap=“600m” nginx
    Unable to find image ‘nginx:latest’ locally
    latest: Pulling from library/nginx
    f7e2b70d04ae: Already exists
    08dd01e3f3ac: Pull complete
    d9ef3a1eb792: Pull complete
    Digest: sha256:98efe605f61725fd817ea69521b0eeb32bef007af0e3d0aeb6258c6e6fe7fc1a
    Status: Downloaded newer image for nginx:latest
    4699bae4066dc63ec54c3df08bb3eef301b3d4a1a4f065d3fd6b0af0741e7beb
    CPU限额:允许容器最多使用50%的CPU

    [root@vm001 cgroup]# docker run -d --name nginx04 --cpus=".5" nginx
    98d8b91c59084441ff4b15e0d0f4e540c8c374e5ee04526f30db136ef3b05923

  • 管理容器常用命令
    查看Docker Host上当前运行的容器(-a表示列示出所有容器,包括退出的容器):
    Docker容器的管理命令
    在这里插入图片描述

     [root@vm001 ~]# docker container ls -a
     CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                NAMES
     98d8b91c5908        nginx               "nginx -g 'daemon of…"   16 minutes ago      Up 16 minutes               80/tcp               nginx04
     4699bae4066d        nginx               "nginx -g 'daemon of…"   18 minutes ago      Up 18 minutes               80/tcp               nginx03
     184ac914f825        httpd               "httpd-foreground"       31 minutes ago      Up 31 minutes               0.0.0.0:80->80/tcp   web
     43477a8bae1a        ubuntu              "pwd"                    34 minutes ago      Exited (0) 34 minutes ago                        mystifying_bose
     [root@vm001 ~]# docker container exec -it 184ac914f825 /bin/bash
     root@webserver:/usr/local/apache2# exit
     exit
     [root@vm001 ~]# docker rm -f 184ac914f825
     184ac914f825
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值