默认情况下,所有的容器得到同等比例的 CPU 周期。在有多个容器竞争 CPU 时我们可以设置每个容器能使用的 CPU 时间比例。这个比例叫作共享权值,通过-c或–cpu-shares设置。Docker 默认每个容器的权值为 1024。不设置或将其设置为 0,都将使用这个默认值。系统会根据每个容器的共享权值和所有容器共享权值和比例来给容器分配 CPU 时间。
--cpu-period和--cpu-quataCPU资源的绝对限制 我们可以设置每个容器进程的调度周期,以及在这个周期内各个容器最多能使用多少 CPU 时间,使用--cpu-period即可设置调度周期,使用--cpu-quota即可设置在每个周期内容器能使用的 CPU 时间,两者配合使用。
CFS 周期的有效范围是 1ms~1s,对应的--cpu-period的数值范围是 1000~1000000。而容器的 CPU 配额必须不小于 1ms,即--cpu-quota的值必须 >= 1000。可以看出这两个选项的单位都是 us。
1 2
#将CFS调度的周期设为50000,将容器在每个周期内的CPU配额设置为25000,表示该容器每50ms可以得到50%的CPU运行时间。 docker run -it --cpu-period=50000 --cpu-quota=25000 ubuntu:16.04 /bin/bash
Options: -a, --all Show all containers (default shows just running) -f, --filter filter Filter output based on conditions provided --format string Pretty-print containers using a Go template -n, --last int Show n last created containers (includes all states) (default -1) -l, --latest Show the latest created container (includes all states) --no-trunc Don't truncate output -q, --quiet Only display container IDs -s, --size Display total file sizes
#显示所有运行容器 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e244a095f856 nginx "/docker-entrypoint.…" 41 seconds ago Up 40 seconds 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx_crun 45f8aca32e8a centos "/bin/bash" 2 minutes ago Up 2 minutes quirky_swirles
#显示所有容器,包括非运行中的容器 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e244a095f856 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx_crun a36ec2025146 centos "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago stupefied_archimedes 45f8aca32e8a centos "/bin/bash" 2 minutes ago Up 2 minutes quirky_swirles 546e34cebc66 centos "/bin/bash" 3 minutes ago Exited (0) 3 minutes ago friendly_ritchie
Options: -d, --detach Detached mode: run commandin the background --detach-keys string Override the key sequence for detaching a container -e, --env list Set environment variables --env-file list Read in a file of environment variables -i, --interactive Keep STDIN open even if not attached --privileged Give extended privileges to the command -t, --tty Allocate a pseudo-TTY -u, --user string Username or UID (format: <name|uid>[:<group|gid>]) -w, --workdir string Working directory inside the container
1 2 3 4 5
[root@localhost ~]# docker run -d --name nginx_cexec nginx 0c3cc43e2c77f90dfa00c3782a312057cef1e28bdc28a44b72d42040cfd2263a
Options: -f, --force Force the removal of a running container (uses SIGKILL) -l, --link Remove the specified link -v, --volumes Remove anonymous volumes associated with the container
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8cf831c8c789 nginx "/docker-entrypoint.…" 3 minutes ago Exited (137) About a minute ago nginx_cqt e244a095f856 nginx "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx_crun a36ec2025146 centos "/bin/bash" 10 minutes ago Exited (0) 9 minutes ago stupefied_archimedes 45f8aca32e8a centos "/bin/bash" 10 minutes ago Up 10 minutes quirky_swirles 546e34cebc66 centos "/bin/bash" 11 minutes ago Exited (0) 11 minutes ago friendly_ritchie
#无法直接删除运行容器 [root@localhost ~]# docker rm nginx_crun Error response from daemon: You cannot remove a running container e244a095f8569fcd00da63d939ed1cb382595195ed5da4c22dbb634f5f9197fc. Stop the container before attempting removal or force remove
Options: -f, --format string Format the output using the given Go template -s, --size Display total file sizes if the type is container --type string Return JSON for specified type
Options: --details Show extra details provided to logs -f, --follow Follow log output --since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes) -n, --tail string Number of lines to show from the end of the logs (default "all") -t, --timestamps Show timestamps --until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
Copy files/folders between a container and the local filesystem
Use '-' as the source to read a tar archive from stdin and extract it to a directory destination in a container. Use '-' as the destination to stream a tar archive of a container source to stdout.
Options: -a, --archive Archive mode (copy all uid/gid information) -L, --follow-link Always follow symbol linkin SRC_PATH
#宿主机文件拷贝到容器 [root@localhost ~]# docker cp /home/testfile/index.html nginx_ccp:/usr/share/nginx/html/test.html [root@localhost ~]# curl localhost:3344/test.html <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } 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>
[root@localhost ~]# docker diff nginx_ccp C /var C /var/cache C /var/cache/nginx A /var/cache/nginx/uwsgi_temp A /var/cache/nginx/client_temp A /var/cache/nginx/fastcgi_temp A /var/cache/nginx/proxy_temp A /var/cache/nginx/scgi_temp C /usr C /usr/share C /usr/share/nginx C /usr/share/nginx/html A /usr/share/nginx/html/test.html C /etc C /etc/nginx C /etc/nginx/conf.d C /etc/nginx/conf.d/default.conf C /run A /run/nginx.pid
Create a new image from a container's changes Options: -a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>") -c, --change list Apply Dockerfile instruction to the created image -m, --message string Commit message -p, --pause Pause container during commit (default true)
[root@localhost ~]# docker commit nginx_ccp mynginximage sha256:e902a486acf0bc36ddccec85d5372d96427b04eba12e687bf7d472f39a3193ef [root@localhost ~]# docker images mynginximage REPOSITORY TAG IMAGE ID CREATED SIZE mynginximage latest e902a486acf0 16 seconds ago 141MB
#运行新镜像的容器,访问成功 [root@localhost ~]# docker run -d --name nginx_ccommit -p 3355:80 mynginximage 23f76aae87471bd974f650eebb13ac4bb00bb6301ca265c30619770415c159a7 [root@localhost ~]# curl localhost:3355/test.html <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } 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>