03:创建容器常用选项

创建容器常用选项:

废话不多说,直接看帮助命令:

[root@localhost ~]# docker container --help

Usage:    docker container COMMAND

Manage containers

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  ls          List containers
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune       Remove all stopped containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker container COMMAND --help' for more information on a command.
[root@localhost ~]#

常用的做个截图解释:

来个实例:

# 创建一个后台运行nginxweb的容器,主机名为nginxweb,容器名为nginx_web,主机端口8080映射到容器的80端口,设置环境变量test为123456
[root@localhost ~]# docker container run -itd --name nginx_web -e test=123456 -p 8080:80 -h nginxweb nginx d90c0947c0074f22a3226cc615a4584c6453c75813629b4285a77f5be353767a [root@localhost ~]# docker container ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d90c0947c007 nginx "nginx -g 'daemon of…" 7 seconds ago Up 6 seconds 0.0.0.0:8080->80/tcp nginx_web [root@localhost ~]# docker logs nginx_web [root@localhost ~]# curl 127.0.0.1:8080 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { 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 logs nginx_web 172.17.0.1 - - [13/Jan/2019:14:07:27 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" [root@localhost ~]# docker exec -it nginx_web "docker exec" requires at least 2 arguments. See 'docker exec --help'. Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in a running container [root@localhost ~]# docker exec -it nginx_web bash root@nginxweb:/# hostname nginxweb root@nginxweb:/# echo $test 123456 root@nginxweb:/# exit exit [root@localhost ~]# docker container stop nginx_web nginx_web [root@localhost ~]# docker container ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d90c0947c007 nginx "nginx -g 'daemon of…" About a minute ago Exited (0) 4 seconds ago nginx_web [root@localhost ~]# docker container rm d90c0947c007 d90c0947c007 [root@localhost ~]# docker container ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@localhost ~]#

 容器资源限制:

[root@localhost ~]# docker container run --help |grep cpu
      --cpu-period int                 Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int                  Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int              Limit CPU real-time period in microseconds
      --cpu-rt-runtime int             Limit CPU real-time runtime in microseconds
  -c, --cpu-shares int                 CPU shares (relative weight)
      --cpus decimal                   Number of CPUs
      --cpuset-cpus string             CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string             MEMs in which to allow execution (0-3, 0,1)
[root@localhost ~]# docker container run --help |grep memory
      --kernel-memory bytes            Kernel memory limit
  -m, --memory bytes                   Memory limit
      --memory-reservation bytes       Memory soft limit
      --memory-swap bytes              Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --memory-swappiness int          Tune container memory swappiness (0 to 100) (default -1)
[root@localhost ~]# 
# memory
docker container run -d --name nginx01 --memory='500m' --memory-swap='600m' --oom-kill-disable nginx

# CPU
docker run -d --name nginx02 --cpus='2' nginx

# 查看状态:
[root@localhost ~]# docker stats nginx01 --no-stream
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
7e64017e7e4d        nginx01             0.00%               3.941MiB / 500MiB   0.79%               1.31kB / 0B         7.71MB / 0B         2
[root@localhost ~]# docker stats nginx02 --no-stream
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
e149218abf8f        nginx02             0.00%               1.375MiB / 1.934GiB   0.07%               656B / 0B           0B / 0B             2
[root@localhost ~]# 

管理容器常用命令:

  

[root@localhost ~]# docker container run -d --name nginx03 -p 8080:80 nginx       # 端口映射
[root@localhost ~]# docker container cp anaconda-ks.cfg nginx01:/home/            # cp文件到docker里面
[root@localhost ~]# docker container logs nginx01                    #  查看容器日志
[root@localhost ~]# docker container logs nginx01
172.17.0.1 - - [15/Jan/2019:12:42:58 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
[root@localhost ~]#
[root@localhost ~]# docker ps         # 查看活跃中的容器
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
a326ea3c85dd        nginx               "nginx -g 'daemon of…"   2 minutes ago       Up 2 minutes        0.0.0.0:8080->80/tcp   nginx03
e149218abf8f        nginx               "nginx -g 'daemon of…"   15 minutes ago      Up 15 minutes       80/tcp                 nginx02
7e64017e7e4d        nginx               "nginx -g 'daemon of…"   16 minutes ago      Up 16 minutes       80/tcp                 nginx01
[root@localhost ~]# docker top nginx03   # 查看容器的进程
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                7422                7402                0                   20:44               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 7458                7422                0                   20:44               ?                   00:00:00            nginx: worker process
[root@localhost ~]# docker stop nginx01   # 停止容器
nginx01
[root@localhost ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
a326ea3c85dd        nginx               "nginx -g 'daemon of…"   2 minutes ago       Up 2 minutes        0.0.0.0:8080->80/tcp   nginx03
e149218abf8f        nginx               "nginx -g 'daemon of…"   16 minutes ago      Up 16 minutes       80/tcp                 nginx02
[root@localhost ~]# docker ps -a    # 查看所有容器
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                  NAMES
a326ea3c85dd        nginx               "nginx -g 'daemon of…"   2 minutes ago       Up 2 minutes                0.0.0.0:8080->80/tcp   nginx03
e149218abf8f        nginx               "nginx -g 'daemon of…"   16 minutes ago      Up 16 minutes               80/tcp                 nginx02
7e64017e7e4d        nginx               "nginx -g 'daemon of…"   17 minutes ago      Exited (0) 10 seconds ago                          nginx01
[root@localhost ~]# docker start nginx01    # 启动容器
nginx01
[root@localhost ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
a326ea3c85dd        nginx               "nginx -g 'daemon of…"   3 minutes ago       Up 3 minutes        0.0.0.0:8080->80/tcp   nginx03
e149218abf8f        nginx               "nginx -g 'daemon of…"   16 minutes ago      Up 16 minutes       80/tcp                 nginx02
7e64017e7e4d        nginx               "nginx -g 'daemon of…"   17 minutes ago      Up 5 seconds        80/tcp                 nginx01
[root@localhost ~]# docker stop nginx01
nginx01
[root@localhost ~]# docker rm nginx01  # 删除容器
nginx01
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
a326ea3c85dd        nginx               "nginx -g 'daemon of…"   3 minutes ago       Up 3 minutes        0.0.0.0:8080->80/tcp   nginx03
e149218abf8f        nginx               "nginx -g 'daemon of…"   17 minutes ago      Up 16 minutes       80/tcp                 nginx02
[root@localhost ~]# 
原文地址:https://www.cnblogs.com/zheng-weimin/p/10264283.html