一、docker容器概念
docker 容器就是一个虚拟操作系统,只不过只运行在用户空间而已。
二、容器运行
2.1.docker run 运行参数说明
docker run :创建一个新的容器并运行一个命令
常用参数如下: -a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项; -d: 后台运行容器,并返回容器ID; -i: 以交互模式运行容器,通常与 -t 同时使用; -p: 端口映射,格式为:主机(宿主)端口:容器端口 -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用; --name="nginx-lb": 为容器指定一个名称; --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=[]: 开放一个端口或一组端口;
2.2.运行第一个container
2.2.1.交互式运行,并启动一个sh终端
# docker run -it busybox /bin/sh / # / # / # ls bin dev etc home proc root sys tmp usr var / # / # ip a 1: lo:mtu 65536 qdisc noqueue qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 6: eth0@if7: mtu 1500 qdisc noqueue link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever / # / # exit
2.3.容器进程
2.3.1.守护进程运行一个容器
# docker run -d --name="ckl-nginx" nginx Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx a5a6f2f73cd8: Pull complete 1ba02017c4b2: Pull complete 33b176c904de: Pull complete Digest: sha256:5d32f60db294b5deb55d078cd4feb410ad88e6fe77500c87d3970eca97f54dba Status: Downloaded newer image for nginx:latest 4dc24b453a4a60bd5978adee4495f3e98304dcc05ae8f605e6bd160848973c5d
运行过程说明:
检查本地是否存在指定的镜像,不存在就从公有仓库下载
利用镜像创建并启动一个容器
分配一个文件系统,并在只读的镜像层外面挂载一层可读写层
从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去
从地址池配置一个 ip 地址给容器
执行用户指定的应用程序
执行完毕后容器被终止
2.3.2.查看容器进程
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4dc24b453a4a nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 80/tcp ckl-nginx
容器已经运行,容器启动端口80,而非本地端口
2.3.3.查看容器详细信息获取IP
# docker inspect 4dc24b453a4a [ { .... "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02" .... } }
2.3.4.容器如何与本地主机如何通信
查看本地IP
# ifconfig docker0: flags=4163mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:58ff:fedb:c79b prefixlen 64 scopeid 0x20 ether 02:42:58:db:c7:9b txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8 bytes 656 (656.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
可以看到,有一个docker0的网络接口
访问nginx容器:
# curl http://172.17.0.2Welcome to nginx! Welcome to nginx!
2.3.5.查看所有容器进程
# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4dc24b453a4a nginx "nginx -g 'daemon of…" 30 minutes ago Up 30 minutes 80/tcp ckl-nginx 3f8af7094f31 busybox "/bin/sh" 34 minutes ago Exited (0) 34 minutes ago keen_leavitt
可以看出,之前的busybox容器是退出状态
2.3.6.停止容器
# docker stop 4dc24b453a4a
查看进程:
# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4dc24b453a4a nginx "nginx -g 'daemon of…" 35 minutes ago Exited (0) 1 second ago ckl-nginx 3f8af7094f31 busybox "/bin/sh" 39 minutes ago Exited (0) 39 minutes ago keen_leavitt
停止的容器状态为Exited
2.3.7.启动一个存在的容器
# docker start 3f8af7094f31
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3f8af7094f31 busybox "/bin/sh" 40 minutes ago Up 4 seconds keen_leavitt
2.3.8.重启容器
# docker restart 4dc24b453a4a
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4dc24b453a4a nginx "nginx -g 'daemon of…" 40 minutes ago Up 10 seconds 80/tcp ckl-nginx 3f8af7094f31 busybox "/bin/sh" 43 minutes ago Up 3 minutes keen_leavitt
三、docker进入容器
容器启动后,如何进入容器?
3.1.进入方法一
# docker attach 3f8af7094f31 / # / # / # ls bin dev etc home proc root sys tmp usr var / # exit
3.2.进入方法二
# docker exec -it 3f8af7094f31 /bin/sh / # / # / # ls bin dev etc home proc root sys tmp usr var / # / # exit
四、删除容器
4.1.删除运行的容器
# docker rm 4dc24b453a4a Error response from daemon: You cannot remove a running container 4dc24b453a4a60bd5978adee4495f3e98304dcc05ae8f605e6bd160848973c5d. Stop the container before attempting removal or force remove
容器在运行,无法删除,需要先停止
# docker stop 4dc24b453a4a # docker rm 4dc24b453a4a
4.2.删除所有退出状态的容器
docker rm `docker ps -a | grep Exited | awk '{print $1}'`
4.3.删除所有容器
docker rm $(docker ps -a -q)