登陸
1.通過xshell用root用戶登陸虛機機
安裝
2.執(zhí)行yum install docker 安裝docker
yum -y install docker 就可以不用中間的確認(rèn)交互了
啟動docker服務(wù)
[root@localhost?/]#?service?docker?start
Redirecting to /bin/systemctl start? docker.service
將docker加入開機啟動
[root@localhost?/]#?chkconfig?docker?on
Note: Forwarding request to 'systemctl enable docker.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
測試安裝成功docker images 查看是否有鏡像
[root@localhost ~]# docker images
REPOSITORY? ? ? ? ? TAG? ? ? ? ? ? ? ? IMAGE ID? ? ? ? ? ? CREATED? ? ? ? ? ? SIZE
下載運行鏡像
https://hub.docker.com/ 上查看需要的鏡像
可以直接用docker run 剔应,如果沒有該鏡像會主動去官網(wǎng)下載鏡像要出。也可以提前用
docker pull下載,若在鏡像后沒有加版本信息速种,默認(rèn)為latest
docker run -i -t -d docker.io/centos /bin/bash
c876b0f96b330642d51c929b215a7430ac44e14549661a1c91d9e54770b3195d
查看運行的容器?
查看剛才運行的容器
[root@localhost /]# docker ps
CONTAINER ID? ? ? ? IMAGE? ? ? ? ? ? ? COMMAND? ? ? ? ? ? CREATED? ? ? ? ? ? STATUS? ? ? ? ? ? ? PORTS? ? ? ? ? ? ? NAMES
c876b0f96b33? ? ? ? docker.io/centos? ? "/bin/bash"? ? ? ? 3 minutes ago? ? ? Up 3 minutes? ? ? ? ? ? ? ? ? ? ? ? ? ? gloomy_kirch
進(jìn)入和退出容器
進(jìn)入容器:命令 docker attach + CONTAINER ID
[root@localhost /]# docker attach c876b0f96b33
[root@c876b0f96b33 /]#
[root@c876b0f96b33 /]#exit
exit
[root@localhost /]#
如何退出容器而不停止容器?組合鍵:Ctrl+P+Q
給容器中的CentOS安裝ifconfig
[root@c876b0f96b33 /]# ifconfig
bash: ifconfig: command not found
因為默認(rèn)的CentOS中沒有安裝ifconfig,你無法知道該操作系統(tǒng)的IP,可以通過2中方式給鏡像安裝
ifconfig軟件陌粹。
1.在運行的容器里安裝軟件,然后commint成新的鏡像福压。
在容器里依次運行
yum search ifconfig
yum install -y net-tools.x86_64
測試ifconfig命令成功后掏秩,exit容器
執(zhí)行docker commit + CONTAINER ID + NEW IMAGENAME
然后images
[root@localhost mycentos]# docker images
REPOSITORY? ? ? ? ? TAG? ? ? ? ? ? ? ? IMAGE ID? ? ? ? ? ? CREATED? ? ? ? ? ? SIZE
my/mycentos? ? ? ? latest? ? ? ? ? ? ? 65f4293bb333? ? ? ? 3 days ago? ? ? ? ? 309.5 MB
2.通過dockerfile生產(chǎn)新的鏡像。
編寫一個Dockerfile文件
[root@localhost mycentos]# vi Dockerfile?
## MAINTAINER? ? ? ? hp.zheng
# DOCKER-VERSION? ? 1.0.0
#
# Dockerizing CentOS: Dockerfile for building CentOS images
#
FROM? ? ? centos:latest
MAINTAINER hp.zheng
RUN yum search ifconfig
RUN yum install -y net-tools.x86_64
RUN yum clean all