相关概念
image: 用于创建容器的模板
container:独立运行的一个或一组应用(你创建的独立的工作环境)
client:通过命令行与docker的守护进程通信
容器相关命名:
run : 启动一个容器(以镜像为模板启动一个新的容器),一个镜像使用run命令启动容器需要注意
malijun@MaLiJun-Ubuntu:~/workdir/dockerpro/bitmexpro$ docker run -d -P training/webapp python app.py 229bdf48dc14ad5daa23e0cad83175cde4833f911a294b67587489589e48affc malijun@MaLiJun-Ubuntu:~/workdir/dockerpro/bitmexpro$ docker run -d -P training/webapp python app.py b33338a9942e776156e3f273f966f60c6be5cb96ed4e7e17c35d1da3eda3ff84 malijun@MaLiJun-Ubuntu:~/workdir/dockerpro/bitmexpro$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b33338a9942e training/webapp "python app.py" 3 seconds ago Up 2 seconds 0.0.0.0:32769->5000/tcp angry_goldberg 229bdf48dc14 training/webapp "python app.py" 2 minutes ago Up 2 minutes 0.0.0.0:32768->5000/tcp gallant_tesla a39a1c8073e4 bitmex:0.0.1 "/bin/bash" 3 weeks ago Up About an hour 0.0.0.0:7979->7979/tcp epic_williams malijun@MaLiJun-Ubuntu:~/workdir/dockerpro/bitmexpro$ docker run -d -P training/webapp python app.py e3459562205c2c166c4365d9c2a03a87c35d4c4b78c9fad55eb0b48fdbb48f0d malijun@MaLiJun-Ubuntu:~/workdir/dockerpro/bitmexpro$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e3459562205c training/webapp "python app.py" 3 seconds ago Up 1 second 0.0.0.0:32770->5000/tcp determined_chandrasekhar b33338a9942e training/webapp "python app.py" 33 seconds ago Up 32 seconds 0.0.0.0:32769->5000/tcp angry_goldberg 229bdf48dc14 training/webapp "python app.py" 2 minutes ago Up 2 minutes 0.0.0.0:32768->5000/tcp gallant_tesla a39a1c8073e4 bitmex:0.0.1 "/bin/bash" 3 weeks ago Up About an hour 0.0.0.0:7979->7979/tcp epic_williams
-it image-name /bin/bash 运行交互模式
-d image-name /bin/bash 后台模式
stop : 停止容器
stop name
start: 重启容器
start name
rm: 删除容器
rm name
进入已启动容器的方式:
docker exec -it name /bin/bash
重要常用命令
查看本机容器 docker ps (-a 全部)
malijun@MaLiJun-Ubuntu:~/workdir/dockerpro/bitmexpro$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a39a1c8073e4 bitmex:0.0.1 "/bin/bash" 3 weeks ago Up 39 minutes 0.0.0.0:7979->7979/tcp epic_williams
1 malijun@MaLiJun-Ubuntu:~/workdir/dockerpro/bitmexpro$ docker ps -a 2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3 a459f11da6bd hello-world "/bin/bash" 16 minutes ago Created sleepy_villani 4 4a5797c96195 bitmex:0.0.1 "/bin/bash" 17 minutes ago Exited (0) 17 minutes ago elastic_torvalds 5 a39a1c8073e4 bitmex:0.0.1 "/bin/bash" 3 weeks ago Up 42 minutes 0.0.0.0:7979->7979/tcp epic_williams 6 3ff928f25f1d bitmex:0.0.1 "/bin/bash" 3 weeks ago Created fervent_tesla 7 ec13939a5d93 bitmex:0.0.1 "/bin/bash" 3 weeks ago Exited (0) 3 weeks ago suspicious_mayer 8 29a2500a1912 bitmex:0.0.1 "/bin/bash" 3 weeks ago Exited (0) 3 weeks ago optimistic_cray 9 3c70c2966886 64f844a4e9f4 "/bin/sh -c 'pip ins…" 3 weeks ago Exited (1) 3 weeks ago loving_euler 10 c643331293b7 a6af3caedaf1 "/bin/sh -c 'pip ins…" 3 weeks ago Exited (1) 3 weeks ago suspicious_leavitt 11 fd6082845f76 mongo "docker-entrypoint.s…" 3 weeks ago Exited (255) 2 weeks ago 0.0.0.0:27017->27017/tcp silly_turing 12 984b7c6a3dca ubuntu "echo hello docker" 4 weeks ago Exited (0) 4 weeks ago reverent_fermi 13 f5bce82c251d e49942b750bf "/bin/sh -c 'apt-get…" 4 weeks ago Exited (100) 4 weeks ago inspiring_tereshkova 14 5709270a5f8d training/webapp "python app.py" 4 weeks ago Exited (255) 2 weeks ago 0.0.0.0:32770->5000/tcp priceless_goldwasser 15 4ff32f40c18b training/webapp "python app.py" 4 weeks ago Exited (255) 2 weeks ago 0.0.0.0:32769->5000/tcp goofy_bardeen 16 e4d5dfb71f48 training/webapp "python3 app.py" 4 weeks ago Exited (1) 4 weeks ago silly_yalow 17 f8ad0301fbed hello-world "/hello" 4 weeks ago Exited (0) 4 weeks ago sleepy_knuth
查看本机镜像 docker images
malijun@MaLiJun-Ubuntu:~/workdir/dockerpro/bitmexpro$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE bitmex 0.0.1 2fa73485f763 3 weeks ago 958MB <none> <none> 64f844a4e9f4 3 weeks ago 912MB <none> <none> a6af3caedaf1 3 weeks ago 912MB <none> <none> cb27a6f56c3a 3 weeks ago 89MB bitmexpro 0.1 e49942b750bf 4 weeks ago 1.19GB python 3.6 a6ce22fd0733 4 weeks ago 912MB mongo 3.6 87f1a6e84e00 4 weeks ago 368MB mongo latest 87f1a6e84e00 4 weeks ago 368MB ubuntu latest 113a43faa138 4 weeks ago 81.1MB debian wheezy 4c3b65fb373e 2 months ago 88.3MB hello-world latest e38bc07ac18e 2 months ago 1.85kB training/webapp latest 6fae60ef3446 3 years ago 349MB
查看容器的底层信息 docker inspect
malijun@MaLiJun-Ubuntu:~/workdir/dockerpro/bitmexpro$ docker inspect epic_williams [ { "Id": "a39a1c8073e485d223dfa3607c9804514de9c3721e67f8b6c24672d62f8f45bd", "Created": "2018-06-08T03:27:03.94398628Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 5253, "ExitCode": 0, "Error": "", "StartedAt": "2018-07-05T02:48:28.734928027Z", "FinishedAt": "2018-06-21T10:53:58.601796734+08:00" }, "Image": "sha256:2fa73485f7634259b1c46162f9d411917d97414d434c2740a590968c6652054a", "ResolvConfPath": "/var/lib/docker/containers/a39a1c8073e485d223dfa3607c9804514de9c3721e67f8b6c24672d62f8f45bd/resolv.conf", "HostnamePath": "/var/lib/docker/containers/a39a1c8073e485d223dfa3607c9804514de9c3721e67f8b6c24672d62f8f45bd/hostname", "HostsPath": "/var/lib/docker/containers/a39a1c8073e485d223dfa3607c9804514de9c3721e67f8b6c24672d62f8f45bd/hosts", "LogPath": "/var/lib/docker/containers/a39a1c8073e485d223dfa3607c9804514de9c3721e67f8b6c24672d62f8f45bd/a39a1c8073e485d223dfa3607c9804514de9c3721e67f8b6c24672d62f8f45bd-json.log", "Name": "/epic_williams", "RestartCount": 0, "Driver": "devicemapper", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "docker-default", "ExecIDs": [ "531f34fc45596ee11d94b3a3a36c57868239874cb9a92f52189719bea05fd614" ], "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": { "7979/tcp": [ { "HostIp": "", "HostPort": "7979" } ] }, "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": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "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": { "DeviceId": "225", "DeviceName": "docker-253:0-5115002-c069d2969f4da2d68487a005e275eb66f0fea0903b9f78e15bebb494ab3d42db", "DeviceSize": "10737418240" }, "Name": "devicemapper" }, "Mounts": [], "Config": { "Hostname": "a39a1c8073e4", "Domainname": "", "User": "", "AttachStdin": true, "AttachStdout": true, "AttachStderr": true, "ExposedPorts": { "7979/tcp": {} }, "Tty": true, "OpenStdin": true, "StdinOnce": true, "Env": [ "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "LANG=C.UTF-8", "GPG_KEY=0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D", "PYTHON_VERSION=3.6.5", "PYTHON_PIP_VERSION=10.0.1", "PYTHONUNBUFFERED=1" ], "Cmd": [ "/bin/bash" ], "Image": "bitmex:0.0.1", "Volumes": null, "WorkingDir": "/bitmex", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "bbc95a13a664d8cb902a1b14a5daa7056982d5ddc3f2e81f513edb7dffa50248", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "7979/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "7979" } ] }, "SandboxKey": "/var/run/docker/netns/bbc95a13a664", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "5b1e2f31ba14f101ca3b691c4f717ca5b167468a3a262c01ceacba7ae60abc20", "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": "efaf728f1b762246395671725cc70db3b26a6da78e58e6d42f98e6b4efcc495c", "EndpointID": "5b1e2f31ba14f101ca3b691c4f717ca5b167468a3a262c01ceacba7ae60abc20", "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 } } } } ]
查看容器内的进程 docker top name
malijun@MaLiJun-Ubuntu:~/workdir/dockerpro/bitmexpro$ docker top epic_williams UID PID PPID C STIME TTY TIME CMD root 5253 5233 0 10:48 pts/0 00:00:00 /bin/bash root 5485 5233 0 10:49 pts/1 00:00:00 /bin/bash
查看容易网络端口 port name
malijun@MaLiJun-Ubuntu:~/workdir/dockerpro/bitmexpro$ docker port epic_williams 7979/tcp -> 0.0.0.0:7979
查看容器的日志
malijun@MaLiJun-Ubuntu:~/workdir/dockerpro/bitmexpro$ docker logs -f epic_williams root@a39a1c8073e4:/bitmex# ls Dockerfile README.md bitmexpr request_test.py requirements.txt wsbitmex.py root@a39a1c8073e4:/bitmex# cd bitmexpr/ root@a39a1c8073e4:/bitmex/bitmexpr# ls bitmexpro datasave db.sqlite3 manage.py start.sh root@a39a1c8073e4:/bitmex/bitmexpr# python manage.py runserver 7979 Performing system checks... System check identified no issues (0 silenced). June 08, 2018 - 03:27:43 Django version 2.0.5, using settings 'bitmexpro.settings' Starting development server at http://127.0.0.1:7979/ Quit the server with CONTROL-C. ^Croot@a39a1c8073e4:/bitmex/bitmexpr# python manage.py runserver 0:7979 Performing system checks... System check identified no issues (0 silenced). June 08, 2018 - 03:44:16 Django version 2.0.5, using settings 'bitmexpro.settings' Starting development server at http://0:7979/ Quit the server with CONTROL-C. GET [08/Jun/2018 03:44:22] "GET / HTTP/1.1" 200 3 Not Found: /favicon.ico
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:docker container(容器)的使用方法 - Python技术站