@nalan90
2017-03-13T05:45:20.000000Z
字数 12474
阅读 1008
Docker学习
Docker组件
MAC OS X中安装Boot2Docker
1、安装虚拟机VirtualBox
2、http://boot2docker.io下载并安装boot2docker.pkg
Boot2Docker常用命令
zhangshangdeMBP:~ zhangshuang$ boot2docker helpUsage: 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 initWARNING: 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 statusrunning
zhangshangdeMBP:~ zhangshuang$ boot2docker ssh## .## ## ## ==## ## ## ## ## ===/"""""""""""""""""\___/ ===~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~\______ o __/\ \ __/\____\_______/_ _ ____ _ _| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ ||_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|Boot2Docker version 1.12.2, build HEAD : 9d8e41b - Tue Oct 11 23:40:08 UTC 2016Docker version 1.12.2, build bb80604只有进入此终端才能执行docker相关的操作docker@boot2docker:~$ docker info
Docker常用命令
bash-3.2$ dockerUsage: 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 quitCommands:attach Attach to a running containerbuild Build an image from a Dockerfilecommit Create a new image from a container's changescp Copy files/folders from a container to a HOSTDIR or to STDOUTcreate Create a new containerdiff Inspect changes on a container's filesystemevents Get real time events from the serverexec Run a command in a running containerexport Export a container's filesystem as a tar archivehistory Show the history of an imageimages List imagesimport Import the contents from a tarball to create a filesystem imageinfo Display system-wide informationinspect Return low-level information on a container or imagekill Kill a running containerload Load an image from a tar archive or STDINlogin Register or log in to a Docker registrylogout Log out from a Docker registrylogs Fetch the logs of a containerpause Pause all processes within a containerport List port mappings or a specific mapping for the CONTAINERps List containerspull Pull an image or a repository from a registrypush Push an image or a repository to a registryrename Rename a containerrestart Restart a running containerrm Remove one or more containersrmi Remove one or more imagesrun Run a command in a new containersave Save an image(s) to a tar archivesearch Search the Docker Hub for imagesstart Start one or more stopped containersstats Display a live stream of container(s) resource usage statisticsstop Stop a running containertag Tag an image into a repositorytop Display the running processes of a containerunpause Unpause all processes within a containerversion Show the Docker version informationwait Block until a container stops, then print its exit codeRun 'docker COMMAND --help' for more information on a command.
查看docker信息
bash-3.2$ docker infoContainers: 0Images: 1Storage Driver: aufsRoot Dir: /mnt/sda1/var/lib/docker/aufsBacking Filesystem: extfsDirs: 5Dirperm1 Supported: trueLogging Driver: json-fileKernel Version: 4.4.24-boot2dockerOperating System: Boot2Docker 1.12.2 (TCL 7.2); HEAD : 9d8e41b - Tue Oct 11 23:40:08 UTC 2016CPUs: 4Total Memory: 1.955 GiBName: boot2dockerID: JKO2:2VVO:FWNH:XTFT:CUFM:CDPX:KJFM:RKU5:3GZH:5SDB:7L3Y:2U5WDebug mode (server): trueFile Descriptors: 14Goroutines: 24System Time: 2016-10-20T04:45:35.210310162ZEventsListeners: 0Init SHA1:Init Path:Docker Root Dir: /mnt/sda1/var/lib/dockerbash-3.2$ docker versionClient:Version: 1.8.0API version: 1.20Go version: go1.4.2Git commit: 0d03096Built: Tue Aug 11 17:17:40 UTC 2015OS/Arch: darwin/amd64Server:Version: 1.12.2API version: 1.24Go version: go1.6.3Git commit: bb80604Built: 2016-10-11T17:00:50.484464535+00:00OS/Arch: linux/amd64
运行第一个容器
docker run命令提供了Docker容器的创建到启动的功能
创建一个新的交互式的容器bash-3.2$ docker run -i -t ubuntu /bin/bashroot@3e4d73885a0d:/#在容器中执行命令root@3e4d73885a0d:/# hostname3e4d73885a0droot@3e4d73885a0d:/# cat /etc/hosts127.0.0.1 localhost::1 localhost ip6-localhost ip6-loopbackfe00::0 ip6-localnetff00::0 ip6-mcastprefixff02::1 ip6-allnodesff02::2 ip6-allrouters172.17.0.2 3e4d73885a0droot@3e4d73885a0d:/# ip abash: ip: command not foundroot@3e4d73885a0d:/# ps -auxUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.1 18240 3208 ? Ss 04:47 0:00 /bin/bashroot 14 0.0 0.1 34424 2708 ? R+ 04:53 0:00 ps -auxroot@3e4d73885a0d:/# apt-get update && apt-get install vim退出容器(退出后容器也停止运行)root@3e4d73885a0d:/# exit
参数说明:
查看容器状态
docker ps 可查看正在运行中的容器,若想查看所有的容器,请使用:docker ps -a
docker@boot2docker:~$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3e4d73885a0d ubuntu "/bin/bash" 10 minutes ago Up 10 minutes trusting_dijkstradocker@boot2docker:~$ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3e4d73885a0d ubuntu
有三种方式可以指定一个容器
容器命名
创建指定名称的容器,容器的名称必须唯一,若要创建一个名称已经存在的容器,请先行删除(docker rm )
docker@boot2docker:~$ docker run --name zhangshuang_ubuntu -i -t ubuntu /bin/bashbash-3.2$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4f2b0800c4a4 ubuntu "/bin/bash" 31 seconds ago Up 30 seconds zhangshuang_ubuntu
其他操作
docker@boot2docker:~$ docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4f2b0800c4a4 ubuntu "/bin/bash" 8 minutes ago Exited (0) 5 minutes ago zhangshuang_ubuntu3e4d73885a0d ubuntu "/bin/bash" 26 minutes ago Exited (0) 10 minutes ago trusting_dijkstra启动一个关闭状态的docker容器##1.以UUID方式启动docker@boot2docker:~$ docker start 4f2b0800c4a44f2b0800c4a4##2.以名称方式启动docker@boot2docker:~$ docker start zhangshuang_ubuntuzhangshuang_ubuntu关闭一个运行状态的docker容器##1.以UUID方式关闭docker@boot2docker:~$ docker stop 4f2b0800c4a44f2b0800c4a4##2.以名称方式关闭docker@boot2docker:~$ docker stop zhangshuang_ubuntuzhangshuang_ubuntu进入一个运行状态中的容器##1.以UUID方式进入docker@boot2docker:~$ docker attach 4f2b0800c4a4root@4f2b0800c4a4:/###2.以名称方式进入docker@boot2docker:~$ docker attach zhangshuang_ubunturoot@4f2b0800c4a4:/#
创建守护式容器
除了交互式的容器,我们也可以创建长期运行的守护式容器
bash-3.2$ docker run --name daemon_ubuntu -d ubuntu /bin/bash -c "while true;do echo hello world;sleep 1;done"96c02f665916644ca0ad1f1042870a250704b0f678f0fe59176a62c657dafa17ddocker@boot2docker:~$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES96c02f665916 ubuntu "/bin/bash -c 'while " 44 seconds ago Up 43 seconds daemon_ubuntudocker@boot2docker:~$获取容器的日志docker@boot2docker:~$ docker logs daemon_ubuntuhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worlddocker@boot2docker:~$ docker logs -ft daemon_ubuntu2016-10-20T13:38:43.006288996Z hello world2016-10-20T13:38:44.007539058Z hello world2016-10-20T13:38:45.009713914Z hello world2016-10-20T13:38:46.013797038Z hello world2016-10-20T13:38:47.023904915Z hello world2016-10-20T13:38:48.026268144Z hello world2016-10-20T13:38:49.030625520Z hello world2016-10-20T13:38:50.033924806Z hello world查看容器内的进程docker@boot2docker:~$ docker top daemon_ubuntuUID PID PPID C STIME TTY TIME CMDroot 4858 4843 0 13:38 ? 00:00:00 /bin/bash -c while true;do echo hello world;sleep 1;doneroot 5108 4858 0 13:41 ? 00:00:00 sleep 1docker@boot2docker:~$在容器内部运行进程(创建一个文件)docker@boot2docker:~$ docker exec -d daemon_ubuntu touch /etc/new_config_file以下方式进入容器退出后,容器仍然运行docker@boot2docker:~$ docker exec -i -t daemon_ubuntu /bin/bashroot@e3ec89e5d9c5:/# exitexitdocker@boot2docker:~$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe3ec89e5d9c5 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_ubuntufalsedocker@boot2docker:~$ docker inspect --format='{{.NetworkSettings.SandboxKey}}' daemon_ubuntu/var/run/docker/netns/427e09c6bee8docker@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_ubuntuzhangshuang_ubuntu注意:只能删除停止运行的容器删除所有的容器docker@boot2docker:~$ docker rm `docker ps -a -q`a7a19f1c8db4