1)docker container run,-i 交互式,-t 创建一个伪终端,-d 后台运行
[root@localhost ~]# docker container run -itd --name bs busybox
6cfa0a43ba1b393d2f5f086ad59f662e0b6bb293d80c3a3d6bdf8d1d2ff7255c
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6cfa0a43ba1b busybox "sh" 4 seconds ago Up 2 seconds bs
[root@localhost ~]#
2)docker container attach 进入容器再退出容器会停;按 Ctrl+p+q 退出则会继续运行。
使用 docker container exec -it <container id | container name> 进入容器终端再退出,不会停止运行。
[root@localhost ~]# docker container attach 6cfa0a43ba1b
/ # ls
bin dev etc home proc root sys tmp usr var
/ # exit
[root@localhost ~]# docker container attach bs
You cannot attach to a stopped container, start it first
[root@localhost ~]# docker container start bs
bs
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# docker container attach bs
/ # ls
bin dev etc home proc root sys tmp usr var
/ # read escape sequence
[root@localhost ~]# docker container exec -it bs sh
/ # exit
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6cfa0a43ba1b busybox "sh" About an hour ago Up About a minute bs
[root@localhost ~]#
3)容器和宿主机的一些联系
第一,关于IP地址
[root@localhost ~]# ip addr
...(skipped)...
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ba:15:f7:99 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:baff:fe15:f799/64 scope link
valid_lft forever preferred_lft forever
...(skipped)...
[root@localhost ~]#
[root@localhost ~]# docker container attach 6cfa0a43ba1b
/ # ls
bin dev etc home proc root sys tmp usr var
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:16 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1312 (1.2 KiB) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
第二 ,每次重启容器,会重新挂载以下三个文件
[root@localhost ~]# docker container attach bs
/ # mount
rootfs on / type rootfs (rw)
overlay on / type overlay
...(skipped)...
/dev/mapper/centos-root on /etc/resolv.conf type xfs (rw,seclabel,relatime,attr2,inode64,noquota)
/dev/mapper/centos-root on /etc/hostname type xfs (rw,seclabel,relatime,attr2,inode64,noquota)
/dev/mapper/centos-root on /etc/hosts type xfs (rw,seclabel,relatime,attr2,inode64,noquota)
...(skipped)...
/ # cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 114.114.114.114
/ # cat /etc/hostname
6cfa0a43ba1b
/ # cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 6cfa0a43ba1b
/ #
4)docker container run,-e 起容器的时候传入变量
[root@localhost ~]# docker container run -itd -e a=666 --name bs1 busybox
d8758dd0bd0d18047616680decedb25021037430ddb312e707e3bae5f7dad442
[root@localhost ~]# docker exec -it bs1 sh
/ # echo $a
666
5)docker container run,-p 起容器的时候做端口映射,例如将容器的80端口转到宿主机的8080端口
[root@localhost ~]# docker container run -itd -p 8080:80 --name nginx01 nginx
b83f2dc64a57b045ae8397bcafd5a9308f4e0c4d3a982e69b630b3d44f84743d
[root@localhost ~]# docker ps | egrep 'CONTAINER|nginx01'
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b83f2dc64a57 nginx "nginx -g 'daemon of…" 36 seconds ago Up 35 seconds 0.0.0.0:8080->80/tcp nginx01
[root@localhost ~]#
打开浏览器,url输入 <宿主机地址>:8080,会进入如下页面
6)docker logs <container name | container ID> 查看容器控制台输出;输出也会存储到宿主机的 /var/lib/docker/containers/<容器完整ID> 下的 json 文件。注:有时候空间不够可以看看这个json文件是不是太大可否清理
7)docker container run, --restart=always 服务化,容器停了会再给起来
注: --restart,容器退出时的重启策略,默认no
这里试试新起一个带--restart=always的容器nginx02,然后重启docker,比较一nginx01和nginx02,发现容器nginx02自动起了,而容器nginx01处于停止状态
8)容器资源限制(CPU、内存),可以使用 docker container stats 查看容器运行参数
--cpus decimal Number of CPUs 例如宿主机有多核CPU,限制容器只使用1核
--memory bytes Memory limit 例如限制容器使用内容上限512M,swap默认是内存的两倍
[root@localhost ~]# docker container run -itd --cpus 1 --name nginx03 nginx
a120b86d31618502a889532e46edf0515c00b6ca7873b9982515af5d91947620
[root@localhost ~]#
[root@localhost ~]# docker container run -itd --memory 512m --name nginx04 nginx
cc74fb0809723882b6765b10d1e4560f0326fc1390f15e1f4f6fda92e9acba85
[root@localhost ~]#
[root@localhost ~]# docker container stats nginx04
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
cc74fb080972 nginx04 0.00% 1.383MiB / 512MiB 0.27% 656B / 0B 0B / 0B 2
……
(End)
学习的大大教学视频传送门 -> https://ke.qq.com/course/366769?taid=2769871654197425