【玩转Docker系列3】Docker入门操作
1 Docker入门操作
1.1 查看Docker状态
查看Docker程序是否正常工作
UbuntuTest:~ # docker info
Containers: 20
Running: 0
Paused: 0
Stopped: 20
Images: 44
Server Version: 1.12.1
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs
Backing Filesystem: extfs
Dirs: 84
Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge null host overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: apparmor
Kernel Version: 3.19.0-25-generic
Operating System: Ubuntu 14.04.5 LTS
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 15.67 GiB
Name: UbuntuTest
ID: AF2O:OFS5:7QCH:HWXH:LYFN:I4UG:YCI5:NT57:3BXJ:VR4R:A6M6:NBGN
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: No swap limit support
Insecure Registries:
127.0.0.0/8
1.2 运行、查看、使用容器
1.2.1 运行第一个容器
容器运行前必须先下载镜像,镜像下载方法请参看本文4.1章节。
系统已下载了python镜像。
UbuntuTest:~ # docker run -i -t python /bin/bash
root@0183b71000ba:/#
1.2.2 使用第一个容器
root@0183b71000ba:/# hostname
0183b71000ba
root@0183b71000ba:/# 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.1 0183b71000ba
root@0183b71000ba:/# ip a
1: lo:
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
5: eth0:
link/ether 02:42:ac:11:00:01 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe11:1/64 scope link
valid_lft forever preferred_lft forever
root@0183b71000ba:/# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 21944 3532 ? Ss 10:36 0:00 /bin/bash
root 10 0.0 0.0 19188 2380 ? R+ 10:39 0:00 ps –aux
root@0183b71000ba:/# ls -l
total 448
drwxr-xr-x 2 root root 4096 Jun 9 21:38 bin
drwxr-xr-x 2 root root 4096 May 30 04:18 boot
-rw------- 1 root root 393216 Jul 19 21:11 core
drwxr-xr-x 5 root root 380 Sep 7 10:36 dev
drwxr-xr-x 63 root root 4096 Sep 7 10:36 etc
drwxr-xr-x 2 root root 4096 May 30 04:18 home
drwxr-xr-x 13 root root 4096 Jul 19 21:11 lib
drwxr-xr-x 2 root root 4096 Jun 8 16:28 lib64
drwxr-xr-x 2 root root 4096 Jun 8 16:28 media
drwxr-xr-x 2 root root 4096 Jun 8 16:28 mnt
drwxr-xr-x 2 root root 4096 Jun 8 16:28 opt
dr-xr-xr-x 125 root root 0 Sep 7 10:36 proc
drwx------ 2 root root 4096 Jul 19 21:11 root
drwxr-xr-x 3 root root 4096 Jun 8 16:28 run
drwxr-xr-x 2 root root 4096 Jun 8 16:29 sbin
drwxr-xr-x 2 root root 4096 Jun 8 16:28 srv
dr-xr-xr-x 13 root root 0 Sep 7 10:39 sys
drwxrwxrwt 2 root root 4096 Jul 19 21:10 tmp
drwxr-xr-x 39 root root 4096 Jul 19 21:11 usr
drwxr-xr-x 25 root root 4096 Jul 19 21:08 var
1.2.3 拷贝安装包到容器并安装
UbuntuTest:~ # ip a show docker0
3: docker0:
link/ether 02:42:fc:ea:e5:44 brd ff:ff:ff:ff:ff:ff
inet 172.17.42.1/16 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:fcff:feea:e544/64 scope link
valid_lft forever preferred_lft forever
root@0183b71000ba:/# scp 172.17.42.1:/root/CloudServiceTestTools.tgz /root
The authenticity of host '172.17.42.1 (172.17.42.1)' can't be established.
ECDSA key fingerprint is 80:d7:04:0f:c2:7d:5c:84:0a:93:d5:77:59:89:e7:c6.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.42.1' (ECDSA) to the list of known hosts.
root@172.17.42.1's password:
CloudServiceTestTools.tgz 100% 145KB 145.4KB/s 00:00
root@0183b71000ba:/# ls -l /root
total 148
-rw-r--r-- 1 root root 148899 Sep 7 10:43 CloudServiceTestTools.tgz
root@0183b71000ba:/# cd /root
root@0183b71000ba:~# tar xzvf CloudServiceTestTools.tgz
install.sh
tools/
tools/rzsz-0.12.20-934.22.x86_64.rpm
tools/find_unused_ip
tools/port_map
tools/tree
root@0183b71000ba:~# ls -l
total 156
-rw-r--r-- 1 root root 148899 Sep 7 10:43 CloudServiceTestTools.tgz
-rwxr--r-- 1 root root 1017 Apr 21 06:51 install.sh
drwxr-xr-x 2 root root 4096 Apr 21 10:43 tools
root@0183b71000ba:~# ./install.sh
============ Begin to install cloud service tools. ============
============ Success to install cloud service tools. ============
root@0183b71000ba:~# tree
.
├── CloudServiceTestTools.tgz
├── install.sh
└── tools
├── find_unused_ip
├── port_map
├── rzsz-0.12.20-934.22.x86_64.rpm
└── tree
1 directory, 6 files
1.3 容器命名
1.3.1 查看当前系统容器列表
运行容器时给容器命名
UbuntuTest:~ # docker run --name container_rename_test -i -t python /bin/bash
root@b6ec7001afb4:/#
查看容器内进程
UbuntuTest:~ # docker attach container_test
root@3ce3a19e44d4:/#
root@3ce3a19e44d4:/# sleep 10000 &
[1] 7
root@3ce3a19e44d4:/# sleep 10000 &
[2] 8
root@3ce3a19e44d4:/# sleep 10000 &
[3] 9
root@3ce3a19e44d4:/# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 21948 3612 ? Ss 11:36 0:00 /bin/bash
root 7 0.0 0.0 5924 680 ? S 11:36 0:00 sleep 10000
root 8 0.0 0.0 5924 664 ? S 11:36 0:00 sleep 10000
root 9 0.0 0.0 5924 716 ? S 11:36 0:00 sleep 10000
root 11 0.0 0.0 19188 2244 ? R+ 11:37 0:00 ps -aux
查看容器详情
UbuntuTest:~ # docker inspect container_test
[
{
"Id": "3ce3a19e44d4283a4e251e1fa4a007739d3765d07addf74b94d02dd057e3d747",
"Created": "2016-09-07T11:32:35.704742277Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 130,
"Error": "",
"StartedAt": "2016-09-07T11:36:37.921683425Z",
"FinishedAt": "2016-09-07T11:41:46.184321085Z"
},
1.8 删除容器
UbuntuTest:~ # docker rm container_test
container_test
UbuntuTest:~ # docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1.9 导出、导入容器
1.9.1 导出容器到本地
UbuntuTest:~ # docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75217df5b798 ubuntu "/bin/bash" 21 hours ago Exited (0) 21 hours ago mad_bhabha
UbuntuTest:~ # docker export 75217df5b798 > /root/ubuntu_container.tar
UbuntuTest:~ # ls -l /root/ubuntu_container.tar
-rw-r--r-- 1 root root 116007424 Sep 19 09:05 /root/ubuntu_container.tar
1.9.2 导入容器
UbuntuTest:~ # cat /root/ubuntu_container.tar | docker import - ubuntu:latest
sha256:eb4ea3f0d161f5db4ff282cab9431b09dd9bfcb45c00fd24ed83ba41781d3dcf
UbuntuTest:~ # docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest eb4ea3f0d161 7 seconds ago 110.4 MB
jenkins latest 19b1ef41dab5 5 days ago 714.5 MB
registry 2.1.1 52bb991b482e 11 months ago 220.1 MB
1.10Docker统计信息
- 点赞
- 收藏
- 关注作者
评论(0)