[关闭]
@nalan90 2017-03-13T13:45:20.000000Z 字数 12474 阅读 850

Docker入门

Docker学习


Docker组件


MAC OS X中安装Boot2Docker

1、安装虚拟机VirtualBox
2、http://boot2docker.io下载并安装boot2docker.pkg


Boot2Docker常用命令

  1. zhangshangdeMBP:~ zhangshuang$ boot2docker help
  2. Usage: boot2docker [<options>] <command> [<args>]
  3. Boot2Docker management utility.
  4. Commands:
  5. init Create a new Boot2Docker VM.
  6. up|start|boot Start VM from any states.
  7. ssh [ssh-command] Login to VM via SSH.
  8. save|suspend Suspend VM and save state to disk.
  9. down|stop|halt Gracefully shutdown the VM.
  10. restart Gracefully reboot the VM.
  11. poweroff Forcefully power off the VM (may corrupt disk image).
  12. reset Forcefully power cycle the VM (may corrupt disk image).
  13. delete|destroy Delete Boot2Docker VM and its disk image.
  14. config|cfg Show selected profile file settings.
  15. info Display detailed information of VM.
  16. ip Display the IP address of the VM's Host-only network.
  17. shellinit Display the shell commands to set up the Docker client.
  18. status Display current state of VM.
  19. download Download Boot2Docker ISO image.
  20. upgrade Upgrade the Boot2Docker ISO image (restart if running).
  21. version Display version information.
  22. Options:
  23. --basevmdk="": Path to VMDK to use as base for persistent partition
  24. --clobber=true: overwrite Docker client binary on boot2docker upgrade
  25. -c, --cpus=4: number of CPUs for boot2docker.
  26. --dhcp=true: enable VirtualBox host-only network DHCP.
  27. --dhcpip=192.168.59.99: VirtualBox host-only network DHCP server address.
  28. -s, --disksize=20000: boot2docker disk image size (in MB).
  29. --dockerport=0: host Docker port (forward to port 2376 in VM). (deprecated - use with care)
  30. --driver="virtualbox": hypervisor driver.
  31. --force-upgrade-download=false: always download on boot2docker upgrade, never skip
  32. --hostip=192.168.59.3: VirtualBox host-only network IP address.
  33. --iso="/Users/zhangshuang/.boot2docker/boot2docker.iso": path to boot2docker ISO image.
  34. --iso-url="https://api.github.com/repos/boot2docker/boot2docker/releases": source URL to provision the boot2docker ISO image.
  35. --lowerip=192.168.59.103: VirtualBox host-only network DHCP lower bound.
  36. -m, --memory=2048: virtual machine memory size (in MB).
  37. --netmask=ffffff00: VirtualBox host-only network mask.
  38. --no-dummy="": Example parameter for the dummy driver.
  39. --retries=75: number of port knocking retries during 'start'
  40. --serial=false: try serial console to get IP address (experimental)
  41. --serialfile="": path to the serial socket/pipe.
  42. --ssh="ssh": path to SSH client utility.
  43. --ssh-keygen="ssh-keygen": path to ssh-keygen utility.
  44. --sshkey="/Users/zhangshuang/.ssh/id_boot2docker": path to SSH key to use.
  45. --sshport=2022: host SSH port (forward to port 22 in VM).
  46. --upperip=192.168.59.254: VirtualBox host-only network DHCP upper bound.
  47. --vbm="VBoxManage": path to VirtualBox management utility.
  48. --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
  49. -v, --verbose=false: display verbose command invocations.
  50. --vm="boot2docker-vm": virtual machine name.
  51. --waittime=300: Time in milliseconds to wait between port knocking retries during 'start'
  1. zhangshangdeMBP:~ zhangshuang$ boot2docker init
  2. WARNING: The 'boot2docker' command line interface (not to be confused with
  3. 'boot2docker' the operating system) is officially deprecated.
  4. Please switch to Docker Machine (https://docs.docker.com/machine/) ASAP.
  5. Docker Toolbox (https://docker.com/toolbox) is the recommended install method.
  6. Virtual machine boot2docker-vm already exists
  7. 本地若已经存在,会报以上信息
  1. zhangshangdeMBP:~ zhangshuang$ boot2docker status
  2. running
  1. zhangshangdeMBP:~ zhangshuang$ boot2docker ssh
  2. ## .
  3. ## ## ## ==
  4. ## ## ## ## ## ===
  5. /"""""""""""""""""\___/ ===
  6. ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
  7. \______ o __/
  8. \ \ __/
  9. \____\_______/
  10. _ _ ____ _ _
  11. | |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
  12. | '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
  13. | |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
  14. |_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
  15. Boot2Docker version 1.12.2, build HEAD : 9d8e41b - Tue Oct 11 23:40:08 UTC 2016
  16. Docker version 1.12.2, build bb80604
  17. 只有进入此终端才能执行docker相关的操作
  18. docker@boot2docker:~$ docker info

Docker常用命令

  1. bash-3.2$ docker
  2. Usage: docker [OPTIONS] COMMAND [arg...]
  3. docker [ -h | --help | -v | --version ]
  4. A self-sufficient runtime for containers.
  5. Options:
  6. --config=~/.docker Location of client config files
  7. -D, --debug=false Enable debug mode
  8. -H, --host=[] Daemon socket(s) to connect to
  9. -h, --help=false Print usage
  10. -l, --log-level=info Set the logging level
  11. --tls=false Use TLS; implied by --tlsverify
  12. --tlscacert=~/.boot2docker/certs/boot2docker-vm/ca.pem Trust certs signed only by this CA
  13. --tlscert=~/.boot2docker/certs/boot2docker-vm/cert.pem Path to TLS certificate file
  14. --tlskey=~/.boot2docker/certs/boot2docker-vm/key.pem Path to TLS key file
  15. --tlsverify=true Use TLS and verify the remote
  16. -v, --version=false Print version information and quit
  17. Commands:
  18. attach Attach to a running container
  19. build Build an image from a Dockerfile
  20. commit Create a new image from a container's changes
  21. cp Copy files/folders from a container to a HOSTDIR or to STDOUT
  22. create Create a new container
  23. diff Inspect changes on a container's filesystem
  24. events Get real time events from the server
  25. exec Run a command in a running container
  26. export Export a container's filesystem as a tar archive
  27. history Show the history of an image
  28. images List images
  29. import Import the contents from a tarball to create a filesystem image
  30. info Display system-wide information
  31. inspect Return low-level information on a container or image
  32. kill Kill a running container
  33. load Load an image from a tar archive or STDIN
  34. login Register or log in to a Docker registry
  35. logout Log out from a Docker registry
  36. logs Fetch the logs of a container
  37. pause Pause all processes within a container
  38. port List port mappings or a specific mapping for the CONTAINER
  39. ps List containers
  40. pull Pull an image or a repository from a registry
  41. push Push an image or a repository to a registry
  42. rename Rename a container
  43. restart Restart a running container
  44. rm Remove one or more containers
  45. rmi Remove one or more images
  46. run Run a command in a new container
  47. save Save an image(s) to a tar archive
  48. search Search the Docker Hub for images
  49. start Start one or more stopped containers
  50. stats Display a live stream of container(s) resource usage statistics
  51. stop Stop a running container
  52. tag Tag an image into a repository
  53. top Display the running processes of a container
  54. unpause Unpause all processes within a container
  55. version Show the Docker version information
  56. wait Block until a container stops, then print its exit code
  57. Run 'docker COMMAND --help' for more information on a command.

查看docker信息

  1. bash-3.2$ docker info
  2. Containers: 0
  3. Images: 1
  4. Storage Driver: aufs
  5. Root Dir: /mnt/sda1/var/lib/docker/aufs
  6. Backing Filesystem: extfs
  7. Dirs: 5
  8. Dirperm1 Supported: true
  9. Logging Driver: json-file
  10. Kernel Version: 4.4.24-boot2docker
  11. Operating System: Boot2Docker 1.12.2 (TCL 7.2); HEAD : 9d8e41b - Tue Oct 11 23:40:08 UTC 2016
  12. CPUs: 4
  13. Total Memory: 1.955 GiB
  14. Name: boot2docker
  15. ID: JKO2:2VVO:FWNH:XTFT:CUFM:CDPX:KJFM:RKU5:3GZH:5SDB:7L3Y:2U5W
  16. Debug mode (server): true
  17. File Descriptors: 14
  18. Goroutines: 24
  19. System Time: 2016-10-20T04:45:35.210310162Z
  20. EventsListeners: 0
  21. Init SHA1:
  22. Init Path:
  23. Docker Root Dir: /mnt/sda1/var/lib/docker
  24. bash-3.2$ docker version
  25. Client:
  26. Version: 1.8.0
  27. API version: 1.20
  28. Go version: go1.4.2
  29. Git commit: 0d03096
  30. Built: Tue Aug 11 17:17:40 UTC 2015
  31. OS/Arch: darwin/amd64
  32. Server:
  33. Version: 1.12.2
  34. API version: 1.24
  35. Go version: go1.6.3
  36. Git commit: bb80604
  37. Built: 2016-10-11T17:00:50.484464535+00:00
  38. OS/Arch: linux/amd64

运行第一个容器

docker run命令提供了Docker容器的创建到启动的功能

  1. 创建一个新的交互式的容器
  2. bash-3.2$ docker run -i -t ubuntu /bin/bash
  3. root@3e4d73885a0d:/#
  4. 在容器中执行命令
  5. root@3e4d73885a0d:/# hostname
  6. 3e4d73885a0d
  7. root@3e4d73885a0d:/# cat /etc/hosts
  8. 127.0.0.1 localhost
  9. ::1 localhost ip6-localhost ip6-loopback
  10. fe00::0 ip6-localnet
  11. ff00::0 ip6-mcastprefix
  12. ff02::1 ip6-allnodes
  13. ff02::2 ip6-allrouters
  14. 172.17.0.2 3e4d73885a0d
  15. root@3e4d73885a0d:/# ip a
  16. bash: ip: command not found
  17. root@3e4d73885a0d:/# ps -aux
  18. USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
  19. root 1 0.0 0.1 18240 3208 ? Ss 04:47 0:00 /bin/bash
  20. root 14 0.0 0.1 34424 2708 ? R+ 04:53 0:00 ps -aux
  21. root@3e4d73885a0d:/# apt-get update && apt-get install vim
  22. 退出容器(退出后容器也停止运行)
  23. root@3e4d73885a0d:/# exit

参数说明:


查看容器状态

docker ps 可查看正在运行中的容器,若想查看所有的容器,请使用:docker ps -a

  1. docker@boot2docker:~$ docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 3e4d73885a0d ubuntu "/bin/bash" 10 minutes ago Up 10 minutes trusting_dijkstra
  4. docker@boot2docker:~$ docker ps -a
  5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  6. 3e4d73885a0d ubuntu
注意:

有三种方式可以指定一个容器


容器命名

创建指定名称的容器,容器的名称必须唯一,若要创建一个名称已经存在的容器,请先行删除(docker rm )

  1. docker@boot2docker:~$ docker run --name zhangshuang_ubuntu -i -t ubuntu /bin/bash
  2. bash-3.2$ docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 4f2b0800c4a4 ubuntu "/bin/bash" 31 seconds ago Up 30 seconds zhangshuang_ubuntu

其他操作

  1. docker@boot2docker:~$ docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 4f2b0800c4a4 ubuntu "/bin/bash" 8 minutes ago Exited (0) 5 minutes ago zhangshuang_ubuntu
  4. 3e4d73885a0d ubuntu "/bin/bash" 26 minutes ago Exited (0) 10 minutes ago trusting_dijkstra
  5. 启动一个关闭状态的docker容器
  6. ##1.以UUID方式启动
  7. docker@boot2docker:~$ docker start 4f2b0800c4a4
  8. 4f2b0800c4a4
  9. ##2.以名称方式启动
  10. docker@boot2docker:~$ docker start zhangshuang_ubuntu
  11. zhangshuang_ubuntu
  12. 关闭一个运行状态的docker容器
  13. ##1.以UUID方式关闭
  14. docker@boot2docker:~$ docker stop 4f2b0800c4a4
  15. 4f2b0800c4a4
  16. ##2.以名称方式关闭
  17. docker@boot2docker:~$ docker stop zhangshuang_ubuntu
  18. zhangshuang_ubuntu
  19. 进入一个运行状态中的容器
  20. ##1.以UUID方式进入
  21. docker@boot2docker:~$ docker attach 4f2b0800c4a4
  22. root@4f2b0800c4a4:/#
  23. ##2.以名称方式进入
  24. docker@boot2docker:~$ docker attach zhangshuang_ubuntu
  25. root@4f2b0800c4a4:/#

创建守护式容器

除了交互式的容器,我们也可以创建长期运行的守护式容器

  1. bash-3.2$ docker run --name daemon_ubuntu -d ubuntu /bin/bash -c "while true;do echo hello world;sleep 1;done"
  2. 96c02f665916644ca0ad1f1042870a250704b0f678f0fe59176a62c657dafa17
  3. ddocker@boot2docker:~$ docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 96c02f665916 ubuntu "/bin/bash -c 'while " 44 seconds ago Up 43 seconds daemon_ubuntu
  6. docker@boot2docker:~$
  7. 获取容器的日志
  8. docker@boot2docker:~$ docker logs daemon_ubuntu
  9. hello world
  10. hello world
  11. hello world
  12. hello world
  13. hello world
  14. hello world
  15. hello world
  16. hello world
  17. hello world
  18. hello world
  19. docker@boot2docker:~$ docker logs -ft daemon_ubuntu
  20. 2016-10-20T13:38:43.006288996Z hello world
  21. 2016-10-20T13:38:44.007539058Z hello world
  22. 2016-10-20T13:38:45.009713914Z hello world
  23. 2016-10-20T13:38:46.013797038Z hello world
  24. 2016-10-20T13:38:47.023904915Z hello world
  25. 2016-10-20T13:38:48.026268144Z hello world
  26. 2016-10-20T13:38:49.030625520Z hello world
  27. 2016-10-20T13:38:50.033924806Z hello world
  28. 查看容器内的进程
  29. docker@boot2docker:~$ docker top daemon_ubuntu
  30. UID PID PPID C STIME TTY TIME CMD
  31. root 4858 4843 0 13:38 ? 00:00:00 /bin/bash -c while true;do echo hello world;sleep 1;done
  32. root 5108 4858 0 13:41 ? 00:00:00 sleep 1
  33. docker@boot2docker:~$
  34. 在容器内部运行进程(创建一个文件)
  35. docker@boot2docker:~$ docker exec -d daemon_ubuntu touch /etc/new_config_file
  36. 以下方式进入容器退出后,容器仍然运行
  37. docker@boot2docker:~$ docker exec -i -t daemon_ubuntu /bin/bash
  38. root@e3ec89e5d9c5:/# exit
  39. exit
  40. docker@boot2docker:~$ docker ps
  41. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  42. e3ec89e5d9c5 ubuntu "/bin/bas
  43. 自动重启容器
  44. docker@boot2docker:~$ docker run --restart=always --name=daemon_restart_ubuntu -d ubuntu /bin/bash -c "while true;do echo hello world;sleep 2;done"
  45. a7a19f1c8db4461397f849e1395866162d5a0689633d7333054a5a2f62c27a6f
  46. --restart标志被 设置为always.无论容器的退出代码是什么,Docker都会自动重启该容器,除了always,我们还可以将这个标志设为on-failure,这样,只有当容器的退出代码为非0值的时候,才会自动重启。另外on-failure还接受一个可选的重启次数参数,如下
  47. --restart=on-failure:5

深入容器

查看容器的详细信息

  1. docker@boot2docker:~$ docker inspect daemon_ubuntu
  2. [
  3. {
  4. "Id": "96c02f665916644ca0ad1f1042870a250704b0f678f0fe59176a62c657dafa17",
  5. "Created": "2016-10-20T13:38:42.688894864Z",
  6. "Path": "/bin/bash",
  7. "Args": [
  8. "-c",
  9. "while true;do echo hello world;sleep 1;done"
  10. ],
  11. "State": {
  12. "Status": "exited",
  13. "Running": false,
  14. "Paused": false,
  15. "Restarting": false,
  16. "OOMKilled": false,
  17. "Dead": false,
  18. "Pid": 0,
  19. "ExitCode": 137,
  20. "Error": "",
  21. "StartedAt": "2016-10-20T13:38:43.006701815Z",
  22. "FinishedAt": "2016-10-20T13:45:06.994070869Z"
  23. },
  24. "Image": "sha256:f753707788c5c100f194ce0a73058faae1a457774efcda6c1469544a114f8644",
  25. "ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/96c02f665916644ca0ad1f1042870a250704b0f678f0fe59176a62c657dafa17/resolv.conf",
  26. "HostnamePath": "/mnt/sda1/var/lib/docker/containers/96c02f665916644ca0ad1f1042870a250704b0f678f0fe59176a62c657dafa17/hostname",
  27. "HostsPath": "/mnt/sda1/var/lib/docker/containers/96c02f665916644ca0ad1f1042870a250704b0f678f0fe59176a62c657dafa17/hosts",
  28. "LogPath": "/mnt/sda1/var/lib/docker/containers/96c02f665916644ca0ad1f1042870a250704b0f678f0fe59176a62c657dafa17/96c02f665916644ca0ad1f1042870a250704b0f678f0fe59176a62c657dafa17-json.log",
  29. "Name": "/daemon_ubuntu",
  30. "RestartCount": 0,
  31. "Driver": "aufs",
  32. "MountLabel": "",
  33. "ProcessLabel": "",
  34. "AppArmorProfile": "",
  35. .........
  36. ]
  37. docker inspect 命令会对容器进行详细的检查,然后返回其配置信息,包括名称、命令、网络配置以及很多有用的数据。
  38. 我们也可以用-f或者--format标志来选定查看结果
  39. docker@boot2docker:~$ docker inspect --format='{{.State.Running}}' daemon_ubuntu
  40. false
  41. docker@boot2docker:~$ docker inspect --format='{{.NetworkSettings.SandboxKey}}' daemon_ubuntu
  42. /var/run/docker/netns/427e09c6bee8
  43. docker@boot2docker:~$ docker inspect --format='{{.Name}} {{.NetworkSettings.SandboxKey}}' daemon_ubuntu zhangshuang_ubuntu
  44. /daemon_ubuntu /var/run/docker/netns/427e09c6bee8
  45. /zhangshuang_ubuntu /var/run/docker/netns/c62e4ae6384b
  46. 删除容器指定的容器
  47. docker@boot2docker:~$ docker rm zhangshuang_ubuntu
  48. zhangshuang_ubuntu
  49. 注意:只能删除停止运行的容器
  50. 删除所有的容器
  51. docker@boot2docker:~$ docker rm `docker ps -a -q`
  52. a7a19f1c8db4
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注