1、创建容器的基本格式
#方法一:
[root@localhost ~]# docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
#创建完成后直接运行
==============================================================================
#方法二:
[root@localhost ~]# docker create --help
Usage: docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Create a new container
#创建完成后并不运行
OPTIONS:
常用选项:
-a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项
-d: 后台运行容器,并返回容器ID
-i: 以交互模式运行容器,通常与 -t 同时使用
-P: 随机端口映射,容器内部端口随机映射到主机的高端口
-p: 指定端口映射,格式为:主机(宿主)端口:容器端口
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用
--name="test01": 为容器指定一个名称
--dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致
--dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致
-h "mars": 指定容器的hostname
-e username="ritchie": 设置环境变量
--env-file=[]: 从指定文件读入环境变量
--cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定CPU运行
-m :设置容器使用内存最大值
--net="bridge": 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型
--link=[]: 添加链接到另一个容器
--expose=[]: 开放一个端口或一组端口
--volume , -v: 绑定一个卷
实例:
[root@localhost ~]# docker run -itd --name xixi centos:latest
6cc0c7294fdf8426051f98fb72fb3b1666878ee5f0b96dedc728edf5331c0d27
#创建xixi容器并运行,以交互模式(-i)、分配为终端(-t)、打印id号并且后台运行(-d)
#镜像为centos最新版本
使用docker run命令来启动容器,docker在后台运行的标准操作包括
1.检查本地是否存在指定的镜像,不存在则从公有仓库下载
2.使用镜像创建并启动容器
3.分配一个文件系统,并在只读的镜像层外面挂载一层可读可写层
4.从宿主主机配置的网桥接口中桥接一个虚拟接口道容器中去
5.从地址池分配一个ip地址给容器
6.执行用户指定的应用程序
7.执行完毕之后容器被终止
2、容器管理
容器的基本操作:
ps、attach、rm、start、stop、kill、rename
查看容器:
[root@localhost ~]# docker ps --help
Usage: docker ps [OPTIONS]
List containers
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 numeric IDs
-s, --size Display total file sizes
#常用: -a全部容器进程、-q返回容器ID、-l最新运行的容器
=============================================================================================================================================================================
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6cc0c7294fdf centos:latest "/bin/bash" 5 minutes ago Up 5 minutes xixi
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6cc0c7294fdf centos:latest "/bin/bash" 11 minutes ago Up 11 minutes xixi
#容器ID、镜像、命令、创建时间、状态、暴漏端口、名字
容器标准输入:
[root@localhost ~]# docker attach --help
Usage: docker attach [OPTIONS] CONTAINER
Attach local standard input, output, and error streams to a running container
Options:
--detach-keys string Override the key sequence for detaching a container
--no-stdin Do not attach STDIN
--sig-proxy Proxy all received signals to the process (default true)
=======================================================================================
[root@localhost ~]# docker attach xixi
[root@6cc0c7294fdf /]#
#容器的标准输入
容器的删除:
[root@localhost ~]# docker rm --help
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
-l, --link Remove the specified link
-v, --volumes Remove the volumes associated with the container
=======================================================================================
[root@localhost ~]# docker rm xixi
xixi
#可以使用name或者ID删除
容器运行和关闭:
[root@localhost ~]# docker start --help
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
Start one or more stopped containers
Options:
-a, --attach Attach STDOUT/STDERR and forward signals
--detach-keys string Override the key sequence for detaching a container
-i, --interactive Attach container's STDIN
=======================================================================================
[root@localhost ~]# docker stop --help
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
Options:
-t, --time int Seconds to wait for stop before killing it (default 10)
=======================================================================================
[root@localhost ~]# docker run -itd --name hehe centos:latest
7c2fc69000d7112ce33732f1cd11a97f866c7c511f91c474672243b8898f28d1
[root@localhost ~]# docker stop hehe
hehe
[root@localhost ~]# docker start hehe
hehe
[root@localhost ~]# docker restart hehe
hehe
#容器的停止、开启和重启
kill容器:
[root@localhost ~]# docker kill --help
Usage: docker kill [OPTIONS] CONTAINER [CONTAINER...]
Kill one or more running containers
Options:
-s, --signal string Signal to send to the container (default "KILL")
=======================================================================================
[root@localhost ~]# docker kill hehe
hehe
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c2fc69000d7 centos:latest "/bin/bash" 3 minutes ago Exited (137) 10 seconds ago hehe
#将运行中的容器干掉
更改容器名:
[root@localhost ~]# docker rename --help
Usage: docker rename CONTAINER NEW_NAME
Rename a container
=======================================================================================
[root@localhost ~]# docker run -itd --name lala centos:latest
e1bf022e631d75b015d49b3941bcc8dbf59a35659cceacf8052c713bc28fe88a
[root@localhost ~]# docker rename lala heihei
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1bf022e631d centos:latest "/bin/bash" 18 seconds ago Up 16 seconds heihei