创建容器常用选项:
废话不多说,直接看帮助命令:
[root@localhost ~]# docker container --helpUsage: docker container COMMANDManage containersCommands: 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 codesRun '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 nginxd90c0947c0074f22a3226cc615a4584c6453c75813629b4285a77f5be353767a[root@localhost ~]# docker container ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd90c0947c007 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:8080Welcome to nginx! Welcome to nginx!
If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.
For online documentation and support please refer tonginx.org.
Commercial support is available atnginx.com.Thank you for using nginx.
[root@localhost ~]# docker logs nginx_web172.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 bashroot@nginxweb:/# hostnamenginxwebroot@nginxweb:/# echo $test123456root@nginxweb:/# exitexit[root@localhost ~]# docker container stop nginx_webnginx_web[root@localhost ~]# docker container ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESd90c0947c007 nginx "nginx -g 'daemon of…" About a minute ago Exited (0) 4 seconds ago nginx_web[root@localhost ~]# docker container rm d90c0947c007d90c0947c007[root@localhost ~]# docker container ps -lCONTAINER 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 ~]#
# memorydocker container run -d --name nginx01 --memory='500m' --memory-swap='600m' --oom-kill-disable nginx# CPUdocker run -d --name nginx02 --cpus='2' nginx# 查看状态:[root@localhost ~]# docker stats nginx01 --no-streamCONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS7e64017e7e4d nginx01 0.00% 3.941MiB / 500MiB 0.79% 1.31kB / 0B 7.71MB / 0B 2[root@localhost ~]# docker stats nginx02 --no-streamCONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDSe149218abf8f 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 nginx01172.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 NAMESa326ea3c85dd nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp nginx03e149218abf8f nginx "nginx -g 'daemon of…" 15 minutes ago Up 15 minutes 80/tcp nginx027e64017e7e4d 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 CMDroot 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 NAMESa326ea3c85dd nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp nginx03e149218abf8f 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 NAMESa326ea3c85dd nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp nginx03e149218abf8f nginx "nginx -g 'daemon of…" 16 minutes ago Up 16 minutes 80/tcp nginx027e64017e7e4d 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 NAMESa326ea3c85dd nginx "nginx -g 'daemon of…" 3 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp nginx03e149218abf8f nginx "nginx -g 'daemon of…" 16 minutes ago Up 16 minutes 80/tcp nginx027e64017e7e4d nginx "nginx -g 'daemon of…" 17 minutes ago Up 5 seconds 80/tcp nginx01[root@localhost ~]# docker stop nginx01nginx01[root@localhost ~]# docker rm nginx01 # 删除容器nginx01[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa326ea3c85dd nginx "nginx -g 'daemon of…" 3 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp nginx03e149218abf8f nginx "nginx -g 'daemon of…" 17 minutes ago Up 16 minutes 80/tcp nginx02[root@localhost ~]#