03 容器操作

容器是由镜像实例化而来的

一般操作

  • 列出所有的容器
docker ps -a
  • 启动容器
docker run []
# 参数说明
--name 容器名字
-d 		 后台运行
-it		 交互式运行,启动并进入容器
-p 		 (小p)指定容器端口 8080:8081
		-p 宿主机端口:容器端口
		
-P     (大P)
-n.    显示最近创建的容器个数-n=1
  • 后台启动
docker run -d --name dockername docker.io/lynntian/alpine:laster /bin/sleep 900
  • 交互式启动
docker run -it docker.io/lynntian/alpine:laster /bin/sh
  • 一次性容器,运行完删除

停止后会把容器删除

docker run -d -- name 容器name --rm 镜像id或名字 
  • 进入up状态的容器中
docker exec -it  容器名字或id /bin/sh
  • 退出容器
exit # 直接退出容器并停止
ctrl + p + q # 容器不停止退出
  • 停止容器
docker stop 容器名字或id
  • 启动容器
docker start 容器名字或id
  • 重启容器
docker restart 容器名字或id
  • 强制停止容器
docker kill id
  • 删除容器
docker rm id        # 不能删除运行的容器docker rm. -f id		# 强制删除容器docker rm -f $(docker ps -aq)   # 删除所有的容器docker ps -qa | xargs docker rm  # 删除所有的容器

eg:

docker run -d -p 3344:80 -v/root/html/:/usr/share/nginx/html/ --name nginx nginx

高级用法

  • 提交容器
docker commit -m="描述信息" -a="作者" 容器id new_image_name:tagdocker commit -p image_id new_image_name
  • 打包镜像
docker save docker_image_id > 文件名
  • 把打包的镜像转为镜像
docker load  <  文件
  • 查看镜像日志
docker logs -tf id            # 显示所有的日志docker logs -tf  --tail 10    # 显示10条日志
  • 查看docker的基本信息(cpu、内存、磁盘)
docker stats
  • 查看容器中的进程
docker top id
  • docker的基本信息
docker inspect id
[    {        "Id": "3de4477a4e21f93e0d33038ba0633c4c554f1c31fc4838a6ae61f771b645e187",        "Created": "2021-09-26T07:20:36.034147069Z",        "Path": "/bin/sh",        "Args": [],        "State": {            "Status": "running",            "Running": true,            "Paused": false,            "Restarting": false,            "OOMKilled": false,            "Dead": false,            "Pid": 6750,            "ExitCode": 0,            "Error": "",            "StartedAt": "2021-09-26T07:23:37.155468742Z",            "FinishedAt": "2021-09-26T07:23:20.130388946Z"        },        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",        "ResolvConfPath": "/var/lib/docker/containers/3de4477a4e21f93e0d33038ba0633c4c554f1c31fc4838a6ae61f771b645e187/resolv.conf",        "HostnamePath": "/var/lib/docker/containers/3de4477a4e21f93e0d33038ba0633c4c554f1c31fc4838a6ae61f771b645e187/hostname",        "HostsPath": "/var/lib/docker/containers/3de4477a4e21f93e0d33038ba0633c4c554f1c31fc4838a6ae61f771b645e187/hosts",        "LogPath": "/var/lib/docker/containers/3de4477a4e21f93e0d33038ba0633c4c554f1c31fc4838a6ae61f771b645e187/3de4477a4e21f93e0d33038ba0633c4c554f1c31fc4838a6ae61f771b645e187-json.log",        "Name": "/centos",        "RestartCount": 0,        "Driver": "overlay2",        "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,            "CgroupnsMode": "host",            "Dns": [],            "DnsOptions": [],            "DnsSearch": [],            "ExtraHosts": null,            "GroupAdd": null,            "IpcMode": "private",            "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,            "DeviceRequests": null,            "KernelMemory": 0,            "KernelMemoryTCP": 0,            "MemoryReservation": 0,            "MemorySwap": 0,            "MemorySwappiness": null,            "OomKillDisable": false,            "PidsLimit": null,            "Ulimits": null,            "CpuCount": 0,            "CpuPercent": 0,            "IOMaximumIOps": 0,            "IOMaximumBandwidth": 0,            "MaskedPaths": [                "/proc/asound",                "/proc/acpi",                "/proc/kcore",                "/proc/keys",                "/proc/latency_stats",                "/proc/timer_list",                "/proc/timer_stats",                "/proc/sched_debug",                "/proc/scsi",                "/sys/firmware"            ],            "ReadonlyPaths": [                "/proc/bus",                "/proc/fs",                "/proc/irq",                "/proc/sys",                "/proc/sysrq-trigger"            ]        },        "GraphDriver": {            "Data": {                "LowerDir": "/var/lib/docker/overlay2/bf1cb1a7b0d7e9d62e89676fef1ad0da5fd2d9270b357643cbc407697db5e47a-init/diff:/var/lib/docker/overlay2/4159d523996155b455f13d688ad9b49f6c343d854e3053f5eb51f40cf1f3e08f/diff",                "MergedDir": "/var/lib/docker/overlay2/bf1cb1a7b0d7e9d62e89676fef1ad0da5fd2d9270b357643cbc407697db5e47a/merged",                "UpperDir": "/var/lib/docker/overlay2/bf1cb1a7b0d7e9d62e89676fef1ad0da5fd2d9270b357643cbc407697db5e47a/diff",                "WorkDir": "/var/lib/docker/overlay2/bf1cb1a7b0d7e9d62e89676fef1ad0da5fd2d9270b357643cbc407697db5e47a/work"            },            "Name": "overlay2"        },        "Mounts": [],        "Config": {            "Hostname": "3de4477a4e21",            "Domainname": "",            "User": "",            "AttachStdin": true,            "AttachStdout": true,            "AttachStderr": true,            "Tty": true,            "OpenStdin": true,            "StdinOnce": true,            "Env": [                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"            ],            "Cmd": [                "/bin/sh"            ],            "Image": "centos",            "Volumes": null,            "WorkingDir": "",            "Entrypoint": null,            "OnBuild": null,            "Labels": {                "org.label-schema.build-date": "20210915",                "org.label-schema.license": "GPLv2",                "org.label-schema.name": "CentOS Base Image",                "org.label-schema.schema-version": "1.0",                "org.label-schema.vendor": "CentOS"            }        },        "NetworkSettings": {            "Bridge": "",            "SandboxID": "110bbdd5d9591493c8a8b803996e3abca05b072349b5e3bd0281e3a2db1d1181",            "HairpinMode": false,            "LinkLocalIPv6Address": "",            "LinkLocalIPv6PrefixLen": 0,            "Ports": {},            "SandboxKey": "/var/run/docker/netns/110bbdd5d959",            "SecondaryIPAddresses": null,            "SecondaryIPv6Addresses": null,            "EndpointID": "1b599e8386427783fdba21eb57fc540e40b6f1bb8382bafb8414b1c5fb91b35f",            "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": "8972b312faca57dc51ae7ac8e018310d22fcc0cf559cad95c5aa5b8157b02467",                    "EndpointID": "1b599e8386427783fdba21eb57fc540e40b6f1bb8382bafb8414b1c5fb91b35f",                    "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 exec -it 容器id /bin/sh   # 进入容器后开启一个新的终端,可以在里边操作docker attach -it 容器id /bin/sh # 进入容器正在执行的终端,不会启动新的
  • 从容器拷贝文件到宿主机
docker cp 容器id:容器内的文件路径 宿主机的路径
  • 映射端口

把容器内的80端口映射到主机的(容器外的)81上

-p

docker run -d --name nginx_service -p81:80 uimage_id 
  • 挂载目录

-v 把宿主机的目录挂载到容器的指定目录

宿主机的目录会和容器中的目录实时同步

docker -d --rm --name mynginx -p82:80 -v/root/index:/usr/share/nginx/html imgae_id
  • 查看所有的挂载信息
docker volume ls
  • 查看容器的基本信息
docker inspect docker_id
  • 添加环境变量
docker run -d --rm -e A_PATH=VALUE --name e-docker image_id

例子:

docker run -it  -e A_PATH=6dbb9cc54074 docker.io/library/alpine:latest printenv

04 容器的生命周期

  • 1 检查本地是否存在镜像,如果不存在,从远端仓库检索并pull
  • 2 利用镜像启动容器
  • 3 分配一个文件系统,并在只读的镜像层外,挂载一层可读可写层
  • 4 从宿主机配置的网桥接口种桥接一个虚拟接口到容器
  • 5 从地址池配置一个ip地址给容器
  • 6 执行用户指定的指令
  • 7 执行完后容器终止

05 portainer—docker的可视化工具

一般不用,自己玩玩

docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

Web:

http://ip:9000/
posted @ 2021-10-18 16:43  tianzhh_lynn  阅读(68)  评论(0编辑  收藏  举报