docker container的操作

简介: docker container的操作

一、docker exec  

docker exec 在容器中执行/bin/bash

~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
6b54d60933d7        ninesun0318/flask   "python app.py"     24 hours ago        Up 24 hours         5000/tcp            gracious_hawking
[root@iZ2vceej7yes1abpm7yec2Z ~]# 
[root@iZ2vceej7yes1abpm7yec2Z ~]# docker exec -it 6b54d60933d7 /bin/bash
root@6b54d60933d7:/app# ls
app.py
root@6b54d60933d7:/app# pdw
bash: pdw: command not found
root@6b54d60933d7:/app# pwd
/app

docker exec 在容器中执行python程序

~]# docker exec -it 6b54d60933d7 python
Python 3.6.12 (default, Oct 13 2020, 21:36:54) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello dockker!!")
hello dockker!!

查看容器ip地址

]# docker exec -it 6b54d60933d7 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
12: eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

二、停止容器

[root@iZ2vceej7yes1abpm7yec2Z ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
6b54d60933d7        ninesun0318/flask   "python app.py"     24 hours ago        Up 24 hours         5000/tcp            gracious_hawking
[root@iZ2vceej7yes1abpm7yec2Z ~]# docker stop 6b5
6b5
[root@iZ2vceej7yes1abpm7yec2Z ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

三、为docker 容器指定name

 ~]# docker run -d --name=test ninesun0318/flask
bb42c845a4edfdfbf20c6661a5cec0a92a69dd72f9ffaf4094c48d2e2207f5d8
[root@iZ2vceej7yes1abpm7yec2Z ~]# 
[root@iZ2vceej7yes1abpm7yec2Z ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
bb42c845a4ed        ninesun0318/flask   "python app.py"     6 seconds ago       Up 5 seconds        5000/tcp            test

四、通过name启动容器

~]# docker stop test
test
[root@iZ2vceej7yes1abpm7yec2Z ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@iZ2vceej7yes1abpm7yec2Z ~]# 
[root@iZ2vceej7yes1abpm7yec2Z ~]# docker start test
test
[root@iZ2vceej7yes1abpm7yec2Z ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
bb42c845a4ed        ninesun0318/flask   "python app.py"     About a minute ago   Up 2 seconds        5000/tcp            test

五、显示容器的完整信息

原来containerid 是一个这么长的id.....

~]# docker inspect bb42c845a4ed
[
    {
        "Id": "bb42c845a4edfdfbf20c6661a5cec0a92a69dd72f9ffaf4094c48d2e2207f5d8",
        "Created": "2020-11-10T13:49:00.495448812Z",
        "Path": "python",
        "Args": [
            "app.py"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 6383,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-11-10T13:50:04.26459612Z",
            "FinishedAt": "2020-11-10T13:49:54.877926435Z"
        },
        "Image": "sha256:1f7f63f7ff02b49931d72607b08612b5983d13b4881757b9e4e518f87a7e6f7f",
        "ResolvConfPath": "/var/lib/docker/containers/bb42c845a4edfdfbf20c6661a5cec0a92a69dd72f9ffaf4094c48d2e2207f5d8/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/bb42c845a4edfdfbf20c6661a5cec0a92a69dd72f9ffaf4094c48d2e2207f5d8/hostname",
        "HostsPath": "/var/lib/docker/containers/bb42c845a4edfdfbf20c6661a5cec0a92a69dd72f9ffaf4094c48d2e2207f5d8/hosts",
        "LogPath": "/var/lib/docker/containers/bb42c845a4edfdfbf20c6661a5cec0a92a69dd72f9ffaf4094c48d2e2207f5d8/bb42c845a4edfdfbf20c6661a5cec0a92a69dd72f9ffaf4094c48d2e2207f5d8-json.log",
        "Name": "/test",
        "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,
            "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": {
                "LowerDir": "/var/lib/docker/overlay2/54518bbb5ecc78fb83d27843c9857c66d341646e235c8264a527d8d03bfade30-init/diff:/var/lib/docker/overlay2/ad5a55f3ad24dc090e95dce379000d2811e34cce4ba250886643488bd80f044c/diff:/var/lib/docker/overlay2/48a511ea7f64437c2b3faf64663d79d5d298d8b6f44f96bb283d9b3515dc3bcb/diff:/var/lib/docker/overlay2/060b0ad334803cd90fc43f3be04972615fdcde8aa1af896bea0f38351eef5858/diff:/var/lib/docker/overlay2/1720343e33c2f8e5c732677d6601e4aec690cd51664831d017dd9246889029eb/diff:/var/lib/docker/overlay2/f378846670249d5b48e8d5cf928e4ad932c6d601e4095018f3a41825707f005b/diff:/var/lib/docker/overlay2/552e6dc16402f5b574ae236fd2435b7fc20b389179a05338cb3aa81a4da9d6c2/diff:/var/lib/docker/overlay2/dbff053a68cb853405d6bb37a7bf759d0339accb01b63882cd471ab2b8ca1989/diff:/var/lib/docker/overlay2/422dbe265e33617ef5ae32e4f0fa8c330fed23aa9366f699a74568003cc14c85/diff:/var/lib/docker/overlay2/bf041b8d900f0f548572fcbd5a85a95ea3f1e15e4c8c3f8d0ddcbdc6adca6e42/diff:/var/lib/docker/overlay2/ddee8b127b22713c581383f32ca94352095f46372fa601cf903c7954d84610c2/diff:/var/lib/docker/overlay2/78a6a24d71d6f4ea0fa5c65e3258165552e01cfb91177f75905f514978ae836a/diff",
                "MergedDir": "/var/lib/docker/overlay2/54518bbb5ecc78fb83d27843c9857c66d341646e235c8264a527d8d03bfade30/merged",
                "UpperDir": "/var/lib/docker/overlay2/54518bbb5ecc78fb83d27843c9857c66d341646e235c8264a527d8d03bfade30/diff",
                "WorkDir": "/var/lib/docker/overlay2/54518bbb5ecc78fb83d27843c9857c66d341646e235c8264a527d8d03bfade30/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "bb42c845a4ed",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "5000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "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.12",
                "PYTHON_PIP_VERSION=20.2.4",
                "PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/fa7dc83944936bf09a0e4cb5d5ec852c0d256599/get-pip.py",
                "PYTHON_GET_PIP_SHA256=6e0bb0a2c2533361d7f297ed547237caf1b7507f197835974c0dd7eba998c53c"
            ],
            "Cmd": [
                "python",
                "app.py"
            ],
            "ArgsEscaped": true,
            "Image": "ninesun0318/flask",
            "Volumes": null,
            "WorkingDir": "/app",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "ninesun0318<wangxucs123@126.com>"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "83bb2e753efe2b317ee80f94ca7b3c0a668531a742a512a58f1d5c314eb4814f",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "5000/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/83bb2e753efe",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "5ae0084aa81c630fa54778ad5f1368e0a7ed19a3e7042a7afd57995ebcd6ab2a",
            "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": "27fd76cd109c5917347b6b7df8b2f281abb5a14050875f947f4ada2ff3c8fb1e",
                    "EndpointID": "5ae0084aa81c630fa54778ad5f1368e0a7ed19a3e7042a7afd57995ebcd6ab2a",
                    "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
                }
            }
        }
    }
]

六、查看容器的log

~]# docker logs bb42c845a4ed
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

七、docker  1.7.1 基本操作

由于OS的原因,安装的1.7版本的docker  部分命令和之前的不太一样 这里记录一下

7.1  恢复docker镜像

在可以访问外网的机器上pull一个镜像

]# docker save -o spug_image.docker registry.aliyuncs.com/openspug/spug

然后将spug_image.docker copy到需要运行的机器上去.

在另外一台机器上恢复

docker run -i -t ubuntu /bin/bash <镜像名称>
/** **docker run参数**
-a=map[]:附加标准输入、输出或者错误输出
-c=0:共享CPU格式(相对重要)
-cidfile="":将容器的ID标识写入
-d=false:分离模式,在后台运行容器,并且打印出容器ID
-e=[]:设置环境变量
-h="":容器的主机名称
-i=false: 保持输入流开放即使没有附加输入流  
-privileged=false: 给容器扩展的权限  
-m="": 内存限制 (格式:<number><optional unit>, unit单位 = b, k, m or g
-n=true: 允许镜像使用网络  
-p=[]: 匹配镜像内的网络端口号  
-rm=false:当容器退出时自动删除容器 (不能跟 -d一起使用)  
-t=false: 分配一个伪造的终端输入  
-u="": 用户名或者ID  
-dns=[]: 自定义容器的DNS服务器  
-v=[]: 创建一个挂载绑定:[host-dir]:[container-dir]:[rw|ro].如果容器目录丢失,docker会创建一个新的卷  
-volumes-from="": 挂载容器所有的卷  
-entrypoint="": 覆盖镜像设置默认的入口点  
-w="": 工作目录内的容器  
-lxc-conf=[]: 添加自定义-lxc-conf="lxc.cgroup.cpuset.cpus = 0,1" 
-sig-proxy=true: 代理接收所有进程信号(even in non-tty mode)  
-expose=[]: 让你主机没有开放的端口  
-link="": 连接到另一个容器(name:alias)  
-name="": 分配容器的名称,如果没有指定就会随机生成一个  
-P=false: Publish all exposed ports to thehost interfaces 公布所有显示的端口主机接口 
docker load -i spug_image.docker

7.2 查看所有镜像

]#docker images
REPOSITORY                            TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
registry.aliyuncs.com/openspug/spug   latest              eebcf73e51a9        5 days ago          669.8 MB

7.3 启动容器

docker run -d --restart=always --name=spug -p 80:80 -v /home/spug/mydata/:/data registry.aliyuncs.com/openspug/spug

7.4 查看容器

]#docker ps
CONTAINER ID        IMAGE                                 COMMAND             CREATED             STATUS              PORTS                NAMES
2f6051471ba9        registry.aliyuncs.com/openspug/spug   "/entrypoint.sh"    20 hours ago        Up 20 hours         0.0.0.0:80->80/tcp   spug 

7.5 重启容器

docker restart spug
目录
相关文章
|
6月前
|
Docker 容器
【Docker】掌握 Docker 镜像操作:从基础到进阶
【Docker】掌握 Docker 镜像操作:从基础到进阶
|
1月前
|
Java Docker 索引
Springboot的Container Images,docker加springboot
本文介绍了如何使用Spring Boot的层索引文件和Docker优化Spring Boot应用程序的容器化过程,包括解压缩可执行jar和创建分层Docker映像的方法。
29 9
Springboot的Container Images,docker加springboot
|
5月前
|
JavaScript 前端开发 数据安全/隐私保护
阿里云云效操作报错合集之流水线中获取不到CI_COMMIT_REF_NAME变量,导致docker镜像打包失败,什么原因
本合集将整理呈现用户在使用过程中遇到的报错及其对应的解决办法,包括但不限于账户权限设置错误、项目配置不正确、代码提交冲突、构建任务执行失败、测试环境异常、需求流转阻塞等问题。阿里云云效是一站式企业级研发协同和DevOps平台,为企业提供从需求规划、开发、测试、发布到运维、运营的全流程端到端服务和工具支撑,致力于提升企业的研发效能和创新能力。
|
2月前
|
Unix Shell Linux
5-15|Docker报错OCI runtime exec failed: exec failed: unable to start container process: exec: “/bin/ba
5-15|Docker报错OCI runtime exec failed: exec failed: unable to start container process: exec: “/bin/ba
|
3月前
|
Docker 容器
【Azure 应用服务】App Service for Container 无法拉取Docker Hub中的镜像替代方案
【Azure 应用服务】App Service for Container 无法拉取Docker Hub中的镜像替代方案
|
3月前
|
存储 Linux Docker
【Azure 应用服务】应用服务中发布Docker Container,如何添加卷(如Azure File Share)以便永久存储文件
【Azure 应用服务】应用服务中发布Docker Container,如何添加卷(如Azure File Share)以便永久存储文件
|
5月前
|
JSON 数据格式 Docker
docker镜像源挂了后操作2024-6
简单操作实现docker镜像依然能顺利拉取。
700 12
|
4月前
|
存储 Docker 容器
docker查看日志:docker service logs 与 docker container logs
docker查看日志:docker service logs 与 docker container logs
314 0
|
4月前
|
运维 Ubuntu Shell
阿里云云效操作报错合集之流水线构建Docker镜像时,遇到报错:“error: failed to solve: rpc error: code”,该怎么办
本合集将整理呈现用户在使用过程中遇到的报错及其对应的解决办法,包括但不限于账户权限设置错误、项目配置不正确、代码提交冲突、构建任务执行失败、测试环境异常、需求流转阻塞等问题。阿里云云效是一站式企业级研发协同和DevOps平台,为企业提供从需求规划、开发、测试、发布到运维、运营的全流程端到端服务和工具支撑,致力于提升企业的研发效能和创新能力。
|
5月前
|
缓存 运维 Devops
阿里云云效操作报错合集之在构建过程中,Docker尝试从缓存中获取某个文件(或计算缓存键)时遇到了问题,该如何处理
本合集将整理呈现用户在使用过程中遇到的报错及其对应的解决办法,包括但不限于账户权限设置错误、项目配置不正确、代码提交冲突、构建任务执行失败、测试环境异常、需求流转阻塞等问题。阿里云云效是一站式企业级研发协同和DevOps平台,为企业提供从需求规划、开发、测试、发布到运维、运营的全流程端到端服务和工具支撑,致力于提升企业的研发效能和创新能力。