原文借鉴:http://www.runoob.com/docker/docker-container-usage.html
Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的、可移植的、自给自足的容器。开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机)、bare metal、OpenStack 集群和其他的基础应用平台。
web应用的自动化打包和发布;
自动化测试和持续集成、发布;
在服务型环境中部署和调整数据库或其他的后台应用;
从头编译或者扩展现有的OpenShift或Cloud Foundry平台来搭建自己的PaaS环境。
下载地址:http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/
安装成功后,桌边会出现三个图标,入下图所示:
进入安装目录执行start.sh会自动进行一些设置,需要点时间,全部完成后会出现如下的结果
初始化配置完成
Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序
docker run ubuntu:15.10 /bin/echo "Hello world"
控制台输出Hello world。
参数解析:
docker: Docker 的二进制执行文件。
run: 与前面的 docker 组合来运行一个容器。
ubuntu:15.10 指定要运行的镜像,Docker首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。
/bin/echo "Hello world": 在启动的容器里执行的命令
使用以下命令创建一个以进程方式运行的容器
docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
在输出中,我们没有看到期望的”hello world”,而是一串长字符
2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63
这个长字符串叫做容器ID,对每个容器来说都是唯一的,我们可以通过容器ID来查看对应的容器发生了什么。
首先,我们需要确认容器有在运行,可以通过 docker ps 来查看
docker stop e03d4ea0963a
在docker容器中运行一个 Python Flask 应用来运行一个web应用。
# 载入镜像 docker pull training/webapp
# 运行 docker run -d -P training/webapp python app.py
参数说明:
-d:让容器在后台运行。
-P:将容器内部使用的网络端口映射到我们使用的主机上。
Docker 开放了 5000 端口(默认 Python Flask 端口)映射到主机端口 32768 上。
通过浏览器访问WEB应用: http://192.168.99.100:32768(ip换成Docker 分配的ip)
也可以指定端口号
docker run -d -p 5000:5000 training/webapp python app.py
查看指定(ID或者名字)容器的端口映射到宿主机的端口号
docker port f30c34853235
查看WEB应用程序日志
docker logs -f f30c34853235
看容器内部运行的进程
docker top quizzical_mclean
重启WEB应用容器
docker start quizzical_mclean
移除WEB应用容器,删除容器时,容器必须是停止状态
docker rm quizzical_mclean
使用 docker inspect 来查看Docker的底层信息。它会返回一个 JSON 文件记录着 Docker 容器的配置和状态信息。
docker inspect quizzical_mclean
[
{
"Id": "f30c3485323567398aad728bafe4b26c2af86a6c07fd7b16e7ca154e01bec18e",
"Created": "2018-04-08T09:51:36.439715908Z",
"Path": "python",
"Args": [
"app.py"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 8598,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-04-08T09:51:36.626295826Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:6fae60ef344644649a39240b94d73b8ba9c67f898ede85cf8e947a887b3e6557",
"ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/f30c3485323567398aad728bafe4b26c2af86a6c07fd7b16e7ca154e01bec18e/resolv.conf",
"HostnamePath": "/mnt/sda1/var/lib/docker/containers/f30c3485323567398aad728bafe4b26c2af86a6c07fd7b16e7ca154e01bec18e/hostname",
"HostsPath": "/mnt/sda1/var/lib/docker/containers/f30c3485323567398aad728bafe4b26c2af86a6c07fd7b16e7ca154e01bec18e/hosts",
"LogPath": "/mnt/sda1/var/lib/docker/containers/f30c3485323567398aad728bafe4b26c2af86a6c07fd7b16e7ca154e01bec18e/f30c3485323567398aad728bafe4b26c2af86a6c07fd7b16e7ca154e01bec18e-json.log",
"Name": "/quizzical_mclean",
"RestartCount": 0,
"Driver": "aufs",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": true,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Data": null,
"Name": "aufs"
},
"Mounts": [],
"Config": {
"Hostname": "f30c34853235",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"5000/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"python",
"app.py"
],
"Image": "training/webapp",
"Volumes": null,
"WorkingDir": "/opt/webapp",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "183bb355f5d887b621f7769a1bf50c24c14a7d5bfb31247d58fc4ce76e0216e1",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"5000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "32768"
}
]
},
"SandboxKey": "/var/run/docker/netns/183bb355f5d8",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "05b20c083ad0d3dcbcf947354f3b36892d9c11b78e0477778a787e9bc355aec2",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "30f02b05fc2486b53a4b4e795b31dbab4a9a0ea498676c0ef05ad00d24c06325",
"EndpointID": "05b20c083ad0d3dcbcf947354f3b36892d9c11b78e0477778a787e9bc355aec2",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]