docker的使用。
搜索一个centos的镜像。
# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 3335 [OK]
jdeathe/centos-ssh CentOS-6 6.8 x86_64 / CentOS-7 7.3.1611 x8... 67 [OK]
nimmis/java-centos This is docker images of CentOS 7 with dif... 25 [OK]
gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glu... 19 [OK]
egyptianbman/docker-centos-nginx-php A simple and highly configurable docker co... 9 [OK]
torusware/speedus-centos Always updated official CentOS docker imag... 8 [OK]
nathonfowlie/centos-jre Latest CentOS image with the JRE pre-insta... 5 [OK]
centos/mariadb55-centos7 5 [OK]
feduxorg/centos-postgresql Centos Image with postgres 4 [OK]
# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
343b09361036: Downloading [======================> ] 31.44MB/70.52MB
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python 2.7-slim 1c7128a655f6 6 days ago 183MB
centos latest 8140d0c64310 6 days ago 193MB
hello-world latest 48b5124b2768 4 months ago 1.84kB
运行第一个实例
# docker run centos /bin/echo 'Hello world'
Hello world
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f47579ffc543 centos "/bin/echo 'Hello ..." 4 seconds ago Exited (0) 3 seconds ago compassionate_edison
# docker run --name mydocker -it centos /bin/bash
[root@72a66110134b /]#
-it i是让容器的输入终端保持打开状态,t是给一个tty终端
现在就进入一个容器中了。
退出的话直接输入exit就可以了。
再次进入的话,就需要先启动起来。
]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72a66110134b centos "/bin/bash" 18 minutes ago Exited (0) 10 minutes ago mydocker
# docker start 72
72
# docker attach 72
[root@72a66110134b /]# exit
exit
再使用docker attach 进入容器
这里有个小技巧,使用Ctrl+p+q 可以直接退出终端,而不使终端关闭。
启动一个nginx。
# docker run -d --name mynginx nginx
97f3f1aa7d135b284ed92b10835ca250dd351648d0c8c556a56b92663c06190a
首先获取nginx的pid
# docker inspect --format "{{.State.Pid}}" mynginx
10196
再进入
# nsenter --target 10196 --mount --uts --ipc --net --pid
mesg: ttyname failed: No such file or directory
root@97f3f1aa7d13:/#
# brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.0242a993190c no veth86b8828
virbr0 8000.525400b9ff6a yes virbr0-nic
# ifconfig
docker0: flags=4163 mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:a9ff:fe93:190c prefixlen 64 scopeid 0x20
ether 02:42:a9:93:19:0c txqueuelen 0 (Ethernet)
RX packets 12623 bytes 547444 (534.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 15111 bytes 84894210 (80.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
还实现了IP地址的映射。
# iptables -t nat -nL
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
创建一个docker实例。采用-P随机映射。
# docker run -d -P --name mynginx1 nginx
97e0d6e3bbbdad775f40da530588ff57fcf25c49681b222e12c085c50863e55e
查看状态
# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
97e0d6e3bbbd nginx "nginx -g 'daemon ..." 6 seconds ago Up 4 seconds 0.0.0.0:32768->80/tcp mynginx1
采用-p实现特定的映射。(指定映射)
# docker run -d -p 8000:80 --name mynginx2 nginx
d3dab021326f3340cd4e92b93a4e02050e7ee9e83c37db6dd119a6685e5e532c
# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d3dab021326f nginx "nginx -g 'daemon ..." 13 seconds ago Up 13 seconds 0.0.0.0:8000->80/tcp mynginx2