@nalan90
2017-03-13T13:45:20.000000Z
字数 12474
阅读 850
Docker学习
Docker组件
MAC OS X中安装Boot2Docker
1、安装虚拟机VirtualBox
2、http://boot2docker.io下载并安装boot2docker.pkg
Boot2Docker常用命令
zhangshangdeMBP:~ zhangshuang$ boot2docker help
Usage: boot2docker [<options>] <command> [<args>]
Boot2Docker management utility.
Commands:
init Create a new Boot2Docker VM.
up|start|boot Start VM from any states.
ssh [ssh-command] Login to VM via SSH.
save|suspend Suspend VM and save state to disk.
down|stop|halt Gracefully shutdown the VM.
restart Gracefully reboot the VM.
poweroff Forcefully power off the VM (may corrupt disk image).
reset Forcefully power cycle the VM (may corrupt disk image).
delete|destroy Delete Boot2Docker VM and its disk image.
config|cfg Show selected profile file settings.
info Display detailed information of VM.
ip Display the IP address of the VM's Host-only network.
shellinit Display the shell commands to set up the Docker client.
status Display current state of VM.
download Download Boot2Docker ISO image.
upgrade Upgrade the Boot2Docker ISO image (restart if running).
version Display version information.
Options:
--basevmdk="": Path to VMDK to use as base for persistent partition
--clobber=true: overwrite Docker client binary on boot2docker upgrade
-c, --cpus=4: number of CPUs for boot2docker.
--dhcp=true: enable VirtualBox host-only network DHCP.
--dhcpip=192.168.59.99: VirtualBox host-only network DHCP server address.
-s, --disksize=20000: boot2docker disk image size (in MB).
--dockerport=0: host Docker port (forward to port 2376 in VM). (deprecated - use with care)
--driver="virtualbox": hypervisor driver.
--force-upgrade-download=false: always download on boot2docker upgrade, never skip
--hostip=192.168.59.3: VirtualBox host-only network IP address.
--iso="/Users/zhangshuang/.boot2docker/boot2docker.iso": path to boot2docker ISO image.
--iso-url="https://api.github.com/repos/boot2docker/boot2docker/releases": source URL to provision the boot2docker ISO image.
--lowerip=192.168.59.103: VirtualBox host-only network DHCP lower bound.
-m, --memory=2048: virtual machine memory size (in MB).
--netmask=ffffff00: VirtualBox host-only network mask.
--no-dummy="": Example parameter for the dummy driver.
--retries=75: number of port knocking retries during 'start'
--serial=false: try serial console to get IP address (experimental)
--serialfile="": path to the serial socket/pipe.
--ssh="ssh": path to SSH client utility.
--ssh-keygen="ssh-keygen": path to ssh-keygen utility.
--sshkey="/Users/zhangshuang/.ssh/id_boot2docker": path to SSH key to use.
--sshport=2022: host SSH port (forward to port 22 in VM).
--upperip=192.168.59.254: VirtualBox host-only network DHCP upper bound.
--vbm="VBoxManage": path to VirtualBox management utility.
--vbox-share=[]: (defaults to '/Users=Users' if no shares are specified; use 'disable' to explicitly prevent any shares from being created) List of directories to share during 'up|start|boot' via VirtualBox Guest Additions, with optional labels
-v, --verbose=false: display verbose command invocations.
--vm="boot2docker-vm": virtual machine name.
--waittime=300: Time in milliseconds to wait between port knocking retries during 'start'
zhangshangdeMBP:~ zhangshuang$ boot2docker init
WARNING: The 'boot2docker' command line interface (not to be confused with
'boot2docker' the operating system) is officially deprecated.
Please switch to Docker Machine (https://docs.docker.com/machine/) ASAP.
Docker Toolbox (https://docker.com/toolbox) is the recommended install method.
Virtual machine boot2docker-vm already exists
本地若已经存在,会报以上信息
zhangshangdeMBP:~ zhangshuang$ boot2docker status
running
zhangshangdeMBP:~ zhangshuang$ boot2docker ssh
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.12.2, build HEAD : 9d8e41b - Tue Oct 11 23:40:08 UTC 2016
Docker version 1.12.2, build bb80604
只有进入此终端才能执行docker相关的操作
docker@boot2docker:~$ docker info
Docker常用命令
bash-3.2$ docker
Usage: docker [OPTIONS] COMMAND [arg...]
docker [ -h | --help | -v | --version ]
A self-sufficient runtime for containers.
Options:
--config=~/.docker Location of client config files
-D, --debug=false Enable debug mode
-H, --host=[] Daemon socket(s) to connect to
-h, --help=false Print usage
-l, --log-level=info Set the logging level
--tls=false Use TLS; implied by --tlsverify
--tlscacert=~/.boot2docker/certs/boot2docker-vm/ca.pem Trust certs signed only by this CA
--tlscert=~/.boot2docker/certs/boot2docker-vm/cert.pem Path to TLS certificate file
--tlskey=~/.boot2docker/certs/boot2docker-vm/key.pem Path to TLS key file
--tlsverify=true Use TLS and verify the remote
-v, --version=false Print version information and quit
Commands:
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders from a container to a HOSTDIR or to STDOUT
create Create a new container
diff Inspect changes on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on a container or image
kill Kill a running container
load Load an image from a tar archive or STDIN
login Register or log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within a container
port List port mappings or a specific mapping for the CONTAINER
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart a running container
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save an image(s) to a tar archive
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop a running container
tag Tag an image into a repository
top Display the running processes of a container
unpause Unpause all processes within a container
version Show the Docker version information
wait Block until a container stops, then print its exit code
Run 'docker COMMAND --help' for more information on a command.
查看docker信息
bash-3.2$ docker info
Containers: 0
Images: 1
Storage Driver: aufs
Root Dir: /mnt/sda1/var/lib/docker/aufs
Backing Filesystem: extfs
Dirs: 5
Dirperm1 Supported: true
Logging Driver: json-file
Kernel Version: 4.4.24-boot2docker
Operating System: Boot2Docker 1.12.2 (TCL 7.2); HEAD : 9d8e41b - Tue Oct 11 23:40:08 UTC 2016
CPUs: 4
Total Memory: 1.955 GiB
Name: boot2docker
ID: JKO2:2VVO:FWNH:XTFT:CUFM:CDPX:KJFM:RKU5:3GZH:5SDB:7L3Y:2U5W
Debug mode (server): true
File Descriptors: 14
Goroutines: 24
System Time: 2016-10-20T04:45:35.210310162Z
EventsListeners: 0
Init SHA1:
Init Path:
Docker Root Dir: /mnt/sda1/var/lib/docker
bash-3.2$ docker version
Client:
Version: 1.8.0
API version: 1.20
Go version: go1.4.2
Git commit: 0d03096
Built: Tue Aug 11 17:17:40 UTC 2015
OS/Arch: darwin/amd64
Server:
Version: 1.12.2
API version: 1.24
Go version: go1.6.3
Git commit: bb80604
Built: 2016-10-11T17:00:50.484464535+00:00
OS/Arch: linux/amd64
运行第一个容器
docker run命令提供了Docker容器的创建到启动的功能
创建一个新的交互式的容器
bash-3.2$ docker run -i -t ubuntu /bin/bash
root@3e4d73885a0d:/#
在容器中执行命令
root@3e4d73885a0d:/# hostname
3e4d73885a0d
root@3e4d73885a0d:/# 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.2 3e4d73885a0d
root@3e4d73885a0d:/# ip a
bash: ip: command not found
root@3e4d73885a0d:/# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 18240 3208 ? Ss 04:47 0:00 /bin/bash
root 14 0.0 0.1 34424 2708 ? R+ 04:53 0:00 ps -aux
root@3e4d73885a0d:/# apt-get update && apt-get install vim
退出容器(退出后容器也停止运行)
root@3e4d73885a0d:/# exit
参数说明:
查看容器状态
docker ps 可查看正在运行中的容器,若想查看所有的容器,请使用:docker ps -a
docker@boot2docker:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3e4d73885a0d ubuntu "/bin/bash" 10 minutes ago Up 10 minutes trusting_dijkstra
docker@boot2docker:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3e4d73885a0d ubuntu
有三种方式可以指定一个容器
容器命名
创建指定名称的容器,容器的名称必须唯一,若要创建一个名称已经存在的容器,请先行删除(docker rm )
docker@boot2docker:~$ docker run --name zhangshuang_ubuntu -i -t ubuntu /bin/bash
bash-3.2$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f2b0800c4a4 ubuntu "/bin/bash" 31 seconds ago Up 30 seconds zhangshuang_ubuntu
其他操作
docker@boot2docker:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f2b0800c4a4 ubuntu "/bin/bash" 8 minutes ago Exited (0) 5 minutes ago zhangshuang_ubuntu
3e4d73885a0d ubuntu "/bin/bash" 26 minutes ago Exited (0) 10 minutes ago trusting_dijkstra
启动一个关闭状态的docker容器
##1.以UUID方式启动
docker@boot2docker:~$ docker start 4f2b0800c4a4
4f2b0800c4a4
##2.以名称方式启动
docker@boot2docker:~$ docker start zhangshuang_ubuntu
zhangshuang_ubuntu
关闭一个运行状态的docker容器
##1.以UUID方式关闭
docker@boot2docker:~$ docker stop 4f2b0800c4a4
4f2b0800c4a4
##2.以名称方式关闭
docker@boot2docker:~$ docker stop zhangshuang_ubuntu
zhangshuang_ubuntu
进入一个运行状态中的容器
##1.以UUID方式进入
docker@boot2docker:~$ docker attach 4f2b0800c4a4
root@4f2b0800c4a4:/#
##2.以名称方式进入
docker@boot2docker:~$ docker attach zhangshuang_ubuntu
root@4f2b0800c4a4:/#
创建守护式容器
除了交互式的容器,我们也可以创建长期运行的守护式容器
bash-3.2$ docker run --name daemon_ubuntu -d ubuntu /bin/bash -c "while true;do echo hello world;sleep 1;done"
96c02f665916644ca0ad1f1042870a250704b0f678f0fe59176a62c657dafa17
ddocker@boot2docker:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96c02f665916 ubuntu "/bin/bash -c 'while " 44 seconds ago Up 43 seconds daemon_ubuntu
docker@boot2docker:~$
获取容器的日志
docker@boot2docker:~$ docker logs daemon_ubuntu
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
docker@boot2docker:~$ docker logs -ft daemon_ubuntu
2016-10-20T13:38:43.006288996Z hello world
2016-10-20T13:38:44.007539058Z hello world
2016-10-20T13:38:45.009713914Z hello world
2016-10-20T13:38:46.013797038Z hello world
2016-10-20T13:38:47.023904915Z hello world
2016-10-20T13:38:48.026268144Z hello world
2016-10-20T13:38:49.030625520Z hello world
2016-10-20T13:38:50.033924806Z hello world
查看容器内的进程
docker@boot2docker:~$ docker top daemon_ubuntu
UID PID PPID C STIME TTY TIME CMD
root 4858 4843 0 13:38 ? 00:00:00 /bin/bash -c while true;do echo hello world;sleep 1;done
root 5108 4858 0 13:41 ? 00:00:00 sleep 1
docker@boot2docker:~$
在容器内部运行进程(创建一个文件)
docker@boot2docker:~$ docker exec -d daemon_ubuntu touch /etc/new_config_file
以下方式进入容器退出后,容器仍然运行
docker@boot2docker:~$ docker exec -i -t daemon_ubuntu /bin/bash
root@e3ec89e5d9c5:/# exit
exit
docker@boot2docker:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3ec89e5d9c5 ubuntu "/bin/bas
自动重启容器
docker@boot2docker:~$ docker run --restart=always --name=daemon_restart_ubuntu -d ubuntu /bin/bash -c "while true;do echo hello world;sleep 2;done"
a7a19f1c8db4461397f849e1395866162d5a0689633d7333054a5a2f62c27a6f
--restart标志被 设置为always.无论容器的退出代码是什么,Docker都会自动重启该容器,除了always,我们还可以将这个标志设为on-failure,这样,只有当容器的退出代码为非0值的时候,才会自动重启。另外on-failure还接受一个可选的重启次数参数,如下
--restart=on-failure:5
深入容器
查看容器的详细信息
docker@boot2docker:~$ docker inspect daemon_ubuntu
[
{
"Id": "96c02f665916644ca0ad1f1042870a250704b0f678f0fe59176a62c657dafa17",
"Created": "2016-10-20T13:38:42.688894864Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true;do echo hello world;sleep 1;done"
],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 137,
"Error": "",
"StartedAt": "2016-10-20T13:38:43.006701815Z",
"FinishedAt": "2016-10-20T13:45:06.994070869Z"
},
"Image": "sha256:f753707788c5c100f194ce0a73058faae1a457774efcda6c1469544a114f8644",
"ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/96c02f665916644ca0ad1f1042870a250704b0f678f0fe59176a62c657dafa17/resolv.conf",
"HostnamePath": "/mnt/sda1/var/lib/docker/containers/96c02f665916644ca0ad1f1042870a250704b0f678f0fe59176a62c657dafa17/hostname",
"HostsPath": "/mnt/sda1/var/lib/docker/containers/96c02f665916644ca0ad1f1042870a250704b0f678f0fe59176a62c657dafa17/hosts",
"LogPath": "/mnt/sda1/var/lib/docker/containers/96c02f665916644ca0ad1f1042870a250704b0f678f0fe59176a62c657dafa17/96c02f665916644ca0ad1f1042870a250704b0f678f0fe59176a62c657dafa17-json.log",
"Name": "/daemon_ubuntu",
"RestartCount": 0,
"Driver": "aufs",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
.........
]
docker inspect 命令会对容器进行详细的检查,然后返回其配置信息,包括名称、命令、网络配置以及很多有用的数据。
我们也可以用-f或者--format标志来选定查看结果
docker@boot2docker:~$ docker inspect --format='{{.State.Running}}' daemon_ubuntu
false
docker@boot2docker:~$ docker inspect --format='{{.NetworkSettings.SandboxKey}}' daemon_ubuntu
/var/run/docker/netns/427e09c6bee8
docker@boot2docker:~$ docker inspect --format='{{.Name}} {{.NetworkSettings.SandboxKey}}' daemon_ubuntu zhangshuang_ubuntu
/daemon_ubuntu /var/run/docker/netns/427e09c6bee8
/zhangshuang_ubuntu /var/run/docker/netns/c62e4ae6384b
删除容器指定的容器
docker@boot2docker:~$ docker rm zhangshuang_ubuntu
zhangshuang_ubuntu
注意:只能删除停止运行的容器
删除所有的容器
docker@boot2docker:~$ docker rm `docker ps -a -q`
a7a19f1c8db4