运行第一个容器
[root@localhost ~]# docker run -i -t centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
8ba884070f61: Pull complete
Digest: sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861
Status: Downloaded newer image for centos:latest
[root@dd77efe3aa68 /]#
查看当前系统中容器的列表
[root@localhost ~]# docker ps -a #查看当前中容器的列表
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dd77efe3aa68 centos "/bin/bash" 2 minutes ago Exited (0) About a minute ago determined_lehmann
88b7cea2ef07 nginx "/bin/bash" 2 weeks ago Exited (130) 2 weeks ago objective_gagarin
780086dba00d httpd "httpd-foreground" 2 weeks ago Exited (0) 2 weeks ago zen_fermi
7500dd5b88c9 nginx "nginx -s reload" 2 weeks ago Exited (1) 2 weeks ago nostalgic_keller
31a038906506 nginx "nginx -g 'daemon of…" 2 weeks ago Exited (0) 2 weeks ago flamboyant_lamarr
44316cfd52e9 ubuntu:15.10 "/bin/sh -c 'while t…" 2 weeks ago Exited (137) 2 weeks ago adoring_chatterjee
5da8d136bf33 ubuntu:15.10 "bin/sh 'while true;…" 2 weeks ago Exited (127) 2 weeks ago compassionate_shirley
86347adc606e ubuntu:15.10 "bin/sh 'while true;…" 2 weeks ago Exited (127) 2 weeks ago infallible_dhawan
9835c743b019 ubuntu:15.10 "/bin/bash" 2 weeks ago Exited (0) 2 weeks ago gracious_wescoff
4345d801c414 ubuntu:15.10 "/bin/echo hello" 2 weeks ago Exited (0) 2 weeks ago modest_meitner
387c0af7b665 ubuntu:15.10 "/bin/echo hello" 2 weeks ago Exited (0) 2 weeks ago stupefied_archimedes
b7b7d8f0a788 nginx "nginx -g 'daemon of…" 2 weeks ago Exited (137) 11 days ago rik_nginx
[root@localhost ~]# docker ps #当前正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
给容器命名
[root@localhost ~]# docker run --name fleming_test -i -t centos /bin/bash
[root@569134b8fedd /]#
启动停止的容器
[root@localhost ~]# docker start fleming_test #启动停止的容器
fleming_test
附着到正在运行的容器
[root@localhost ~]# docker attach fleming_test
[root@569134b8fedd /]#
列出镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 8dce45f00be0 2 weeks ago 88.9MB
nginx latest bb776ce48575 3 weeks ago 109MB
httpd latest d4a07e6ce470 4 weeks ago 132MB
centos latest 9f38484d220f 7 weeks ago 202MB
ubuntu latest 94e814e2efa8 7 weeks ago 88.9MB
hello-world latest fce289e99eb9 4 months ago 1.84kB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
training/webapp latest 6fae60ef3446 3 years ago 349MB
创建测试docker镜像
创建一个要修改的定制容器
[root@localhost docker]# docker run -i -t centos /bin/bash #创建一个容器
[root@localhost docker]# docker ps -l -q #得到刚创建容器的id
e3b2a7c3b01f
用本地系统做成的镜像新建测试容器
[root@localhost ~]# docker run -d -p 231:22 --name jason centos7 /usr/sbin/sshd -D //启动容器,并绑定容器端口22到宿主主机, 可以有多个-p 绑定多个端口,例如22端口让远程客户端连接,8080 可以访问容器内tomcat
[root@localhost ~]# ssh root@192.168.0.252 -p 220
The authenticity of host '[192.168.0.252]:220 ([192.168.0.252]:220)' can't be established.
ECDSA key fingerprint is SHA256:8lY/a8uaXKoaGTVyun4LSXzClraZ5AiodB1kKarSNIE.
ECDSA key fingerprint is MD5:74:fa:33:b5:06:e9:0c:c1:dd:80:84:1b:2e:38:33:6b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.0.252]:220' (ECDSA) to the list of known hosts.
root@192.168.0.252's password:
Last login: Mon May 6 10:45:39 2019
[root@faff97aef1d1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
通过xshell登录到自己的容器即可。在容器中操作和在centos7中操作是一样的。Xshell登录容器方式如下