一,如何启动一个已退出的容器?
[root@localhost ~]# docker start storage4
说明:架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,docker容器运行后状态是exited(0)?
run时加参数 -d -i -t
-
-i: 以交互模式运行容器,通常与 -t 同时使用;
-
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
例子:
[root@localhost ~]# docker run --name tracker1 --privileged -d -i -t 470671670cac
5a674df0c4bf9fa47d548e85b77166bc3b7ee4c7905a394ae8426e278cda5cce
查看状态,已经不是exit
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5a674df0c4bf 470671670cac "/bin/bash" 9 seconds ago Up 8 seconds tracker1
三,docker启动的centos容器中如何使systemctl可用?
[root@5a674df0c4bf /]# systemctl status System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: Host is down
[root@localhost ~]# docker run --name tracker1 --privileged -d -i -t 470671670cac /usr/sbin/init 0882a60ec13999b7fff728468fc1b9d89c47d05db49bac635d03b4a2e87c2a6d
[root@0882a60ec139 /]# systemctl start sshd Failed to start sshd.service: Unit sshd.service not found.
说明:不再报 Failed to connect to bus: Host is down
说明: /usr/sbin/init:初始容器里的CENTOS,用于启动dbus-daemon。
四,docker如何生成一个新的镜像
使用commit命令
在容器内做了修改之后,直接commit
[root@914e7dcb1298 fdfs]# exit exit [root@localhost liuhongdi]# docker commit 914e7dcb1298 fastdfs_storage_0.1
说明:
914e7dcb1298: 容器的id
fastdfs_storage_0.1 : 给imae起的名字
可以看到image的列表中已经生了我们生成的image
[root@localhost liuhongdi]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE fastdfs_storage_0.1 latest fc81a26e998e 8 seconds ago 533MB centos latest 470671670cac 6 weeks ago 237MB
五,docker如何挂载本地目录?
-v参数中,冒号":"前面的目录是宿主机目录,后面的目录是容器内目录
需要注意的地方:1,路径要使用绝对路径,避免因使用相对路径出错
2,如果在容器内修改从宿主机挂载进去的目录/文件的owner,
会影响到 宿主机上的目录/文件owner ,
因为linux下的文件是使用uid来对应,所以尽量不要做这种修改
例子:
[root@localhost liuhongdi]# docker run --name tracker1 --privileged -v /usr/local/source:/usr/local/source -d -i -t 470671670cac /usr/sbin/init 6351748e4916dc0300cdb663fe14187da78d7cd16d130c3f2b67a11c1f772aa9 [root@localhost liuhongdi]# docker exec -it tracker1 /bin/bash
[root@6351748e4916 /]# ls /usr/local/source/ V1.0.43.tar.gz apache-zookeeper-3.5.6-bin.tar.gz jmeter.log nginx-1.17.8
说明:进入容器后可以看到以前在宿主机中的文件
六,docker内无法解析dns,需要对宿主机的防火墙做设置:
生产环境下是绝对不可以禁用防火墙, 只需要开启伪装IP的功能即可以正常使用dns
[root@localhost liuhongdi]# firewall-cmd --zone=public --add-masquerade --permanent success [root@localhost liuhongdi]# firewall-cmd --reload success [root@localhost liuhongdi]# systemctl stop firewalld [root@localhost liuhongdi]# systemctl start firewalld [root@localhost liuhongdi]# systemctl stop docker [root@localhost liuhongdi]# systemctl start docker [root@localhost liuhongdi]# docker run --name tracker1 --privileged -v /usr/local/source:/usr/local/source -d -i -t 470671670cac /usr/sbin/init 914e7dcb12983cc27302a7becf05833abab07d8a765bf11326ca0c9ecfa12e6a [root@localhost liuhongdi]# docker exec -it tracker1 /bin/bash [root@914e7dcb1298 /]# ping www.sina.com.cn PING spool.grid.sinaedge.com (120.192.83.125) 56(84) bytes of data. 64 bytes from 120.192.83.125 (120.192.83.125): icmp_seq=1 ttl=51 time=34.6 ms ^C --- spool.grid.sinaedge.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 5ms rtt min/avg/max/mdev = 20.664/27.730/34.566/5.679 ms
七,查看本地centos的版本:
[root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core)