Docker学习

Docker学习

Centon 8安装 Docker

  1. 卸载旧版本

    yum remove docker
    
  2. 安装基本信息

    yum install -y yum-utils
    
  3. 设置镜像仓库

    # 不要用官网默认这个!
    $ sudo yum-config-manager \
        --add-repo \
        https://download.docker.com/linux/centos/docker-ce.repo # 默认是国外的
    
    # 换成下面的
    
    $ sudo yum-config-manager \
        --add-repo \
        https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 阿里云镜像
    
    1. 镜像软件包索引

      yum makecache fast
      

      安装中可能会报 yum makecache: error: argument timer: invalid choice: ‘fast’ (choose from ‘timer’)

      这说明centos8没有该参数,解决办法为:去掉fast参数,就可以了

      yum makecache
      
  4. 安装docker引擎

    yum install docker-ce docker-ce-cli containerd.io # docker-ce 社区版 ee 企业版
    

    安装时可能会出现如下问题

    Last metadata expiration check: 0:01:06 ago on Sat 05 Dec 2020 03:44:27 PM CST.
    Error:
    Problem: package docker-ce-3:19.03.14-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but none of the providers can be installed

    • cannot install the best candidate for the job

    • package containerd.io-1.2.10-3.2.el7.x86_64 is filtered out by modular filtering

    • package containerd.io-1.2.13-3.1.el7.x86_64 is filtered out by modular filtering

    • package containerd.io-1.2.13-3.2.el7.x86_64 is filtered out by modular filtering

    • package containerd.io-1.2.2-3.3.el7.x86_64 is filtered out by modular filtering

    • package containerd.io-1.2.2-3.el7.x86_64 is filtered out by modular filtering

    • package containerd.io-1.2.4-3.1.el7.x86_64 is filtered out by modular filtering

    • package containerd.io-1.2.5-3.1.el7.x86_64 is filtered out by modular filtering

    • package containerd.io-1.2.6-3.3.el7.x86_64 is filtered out by modular filtering

    • package containerd.io-1.3.7-3.1.el7.x86_64 is filtered out by modular filtering

    • package containerd.io-1.3.9-3.1.el7.x86_64 is filtered out by modular filtering
      (try to add '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages

    安装新版的containerd.i包就可以,安装后在执行 yum install docker-ce docker-ce-cli containerd.io

    yum update
    
    dnf install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
    
    yum install docker-ce docker-ce-cli containerd.io
    

    注意这里会有几个个y/n的判断

    image-20201205161329611

    一路 y 就可以了

    要看到Complet再收手!

  5. 启动Docker

    systemctl start docker # 代表启动成功
    
  6. 查看Docker版本

    docker version
    
    Client: Docker Engine - Community
     Version:           19.03.14
     API version:       1.40
     Go version:        go1.13.15
     Git commit:        5eb3275d40
     Built:             Tue Dec  1 19:20:42 2020
     OS/Arch:           linux/amd64
     Experimental:      false
    
    Server: Docker Engine - Community
     Engine:
      Version:          19.03.14
      API version:      1.40 (minimum version 1.12)
      Go version:       go1.13.15
      Git commit:       5eb3275d40
      Built:            Tue Dec  1 19:19:17 2020
      OS/Arch:          linux/amd64
      Experimental:     false
     containerd:
      Version:          1.2.6
      GitCommit:        894b81a4b802e4eb2a91d1ce216b8817763c29fb
     runc:
      Version:          1.0.0-rc8
      GitCommit:        425e105d5a03fabd737a126ad93d62a9eeede87f
     docker-init:
      Version:          0.18.0
      GitCommit:        fec3683
    
    

初次运行 hello

docker run hello-world

image-20201205162604663

中间一堆是签名信息

run的运行流程图

image-20200616161441669

查看下载镜像

docker images

image-20201205163426351

卸载Docker

# 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io

# 删除资源
rm -rf /var/lib/docker # docker 的默认工作路径

阿里云镜像加速

image-20200616154429105

image-20200616154455964image-20200616155201285image-20200616155649476

sudo mkdir -p /etc/docker # 创建一个陌路
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://uyfgafsw.mirror.aliyuncs.com"]
}
EOF # 编写配置文件

sudo systemctl daemon-reload # 重启服务
sudo systemctl restart docker # 重启docker

image-20200616160315298

底层原理

  1. Docker是怎么工作的?

    Docker是一个Client-Server结构的系统,Docker的守护进程在主机上。通过Socket从客户端访问!

    DockerServer接受到Docker-Client的指令,

image-20200616162107363

  1. Docker为什么比VM快?
    1. Docker有着比虚拟机更少的抽象层
    2. docker主要用的是宿主机的内核,vm需要Guest OS

image-20200616162302653

所以说新建一个容器的时候,docker不需要像虚拟机一样重新加载一个操作系统内核,避免引导

Docker命令

docker version # 显示docker的基本信息
docker info # 系统信息,镜像和容器的数量
docker 命令 --help # 全部信息

镜像命令

##### 查看所有镜像
docker images

image-20201205172016927

解释

REPOSITORY # 镜像仓库源
TAG # 镜像的标签
IMAGE ID # 镜像的ID
CREATED # 镜像的创建时间
SIZE # 镜像的大小

--all , -a		Show all images (default hides intermediate images) # 显示所有
--digests		Show digests
--filter , -f		Filter output based on conditions provided
--format		Pretty-print images using a Go template
--no-trunc		Don’t truncate output
--quiet , -q		Only show numeric IDs # 只显示id
镜像搜索
docker search mysql

image-20201205190743070

docker search --help  # search帮助文档

image-20201205191027063

docker search --filter=STARS=3000 # 搜索出Stars大于3000的

image-20201205191158936

下载镜像
docker pull mysql   # 下载mysql镜像,default tag,默认最新版latest
[root@ysl ~]# docker pull mysql
Using default tag: latest  # 不写版本号时,默认下载最新的
latest: Pulling from library/mysql
852e50cd189d: Pull complete  #layer 分成下载,docker image的核心,联合文件系统
29969ddb0ffb: Pull complete 
a43f41a44c48: Pull complete 
5cdd802543a3: Pull complete 
b79b040de953: Pull complete 
938c64119969: Pull complete 
7689ec51a0d9: Pull complete 
a880ba7c411f: Pull complete 
984f656ec6ca: Pull complete 
9f497bce458a: Pull complete 
b9940f97694b: Pull complete 
2f069358dc96: Pull complete 
Digest: sha256:4bb2e81a40e9d0d59bd8e3dc2ba5e1f2197696f6de39a91e90798dd27299b093#签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest#真实地址


# 即
docker pull mysql
# 等价于
docker pull docker.io/library/mysql:latest

docker pull mysql : 5.7 #指定下载的版本号

image-20201205192119856

删除镜像
# 删除一个 可以通过名称 也可以指定id -f表示删除所有
docker rmi -f 9cfcce23593a
# 删除多个 用空格分隔id
docker rmi -f id id id
# 删除所有 
docker rmi -f $(docker images -aq) # images -aq就是查所有镜像id,从而递归删除

容器命令

说明:有了镜像才能创建容器,linux,下载一个centos镜像来测试学习

新建容器并启动
docker run [可选参数] image

# 参数说明
--name=“Name” # 容器名字,用于区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 如-p 8080::8080
	-p ip:主机端口:容器端口
	-p 主机端口:容器端口
	-p 容器端口
	
-p 随机指定端口
进入退出容器
docker run -it centos /bin/bash  # 进入
ls #查看目录
exit # 退出

image-20201205193808212

注意这里面主机名,编程了centos的id

这里面就是一个容器,套娃啊

查看运行的容器
# 查看正在运行的容器
docker ps
# 查看曾经运行的容器
docker ps -a
# 显示最近创建的容器,设置显示个数
docker ps -a - n=? 
# 只显示容器的编号
docker ps -aq
[root@ysl ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@ysl ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
054c11f658bb        centos              "/bin/bash"         5 minutes ago       Exited (0) 5 minutes ago                       blissful_ramanujan
c34be0c5f89a        hello-world         "/hello"            3 hours ago         Exited (0) 3 hours ago                         practical_edison
[root@ysl ~]# docker ps -a -n 2
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
054c11f658bb        centos              "/bin/bash"         5 minutes ago       Exited (0) 5 minutes ago                       blissful_ramanujan
c34be0c5f89a        hello-world         "/hello"            3 hours ago         Exited (0) 3 hours ago                         practical_edison
[root@ysl ~]# docker ps -a -n 1
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
054c11f658bb        centos              "/bin/bash"         5 minutes ago       Exited (0) 5 minutes ago                       blissful_ramanujan
[root@ysl ~]# docker ps -aq
054c11f658bb
c34be0c5f89a
[root@ysl ~]# 

退出容器
# 容器停止退出
exit
# 容器不停止退出 注意必须在英文输入法下,中文输入法不行
Ctrl + P + Q
删除容器
# 删除指定容器 不能删除正在运行的容器,如果强制删除 rm -f
docker rm 容器id
# 删除所有容器
docker rm -f $(docker ps -aq)
# 删除所有容器
docker ps -a -q|xargs docker rm
[root@ysl ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
40e35224b8f8        centos              "/bin/bash"         2 minutes ago       Up 2 minutes                                    sad_liskov
054c11f658bb        centos              "/bin/bash"         11 minutes ago      Exited (0) 11 minutes ago                       blissful_ramanujan
c34be0c5f89a        hello-world         "/hello"            3 hours ago         Exited (0) 3 hours ago                          practical_edison
[root@ysl ~]# docker rm 054c11f658bb
054c11f658bb
[root@ysl ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                   PORTS               NAMES
40e35224b8f8        centos              "/bin/bash"         2 minutes ago       Up 2 minutes                                 sad_liskov
c34be0c5f89a        hello-world         "/hello"            3 hours ago         Exited (0) 3 hours ago                       practical_edison
[root@ysl ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
40e35224b8f8        centos              "/bin/bash"         4 minutes ago       Up 4 minutes                            sad_liskov
[root@ysl ~]# docker rm 40e35224b8f8  # 正在运行的容器无法删除
Error response from daemon: You cannot remove a running container 40e35224b8f8e16274f47cdb6d8c96d5dac4ec9774cc8d2acf1a22e15105a953. Stop the container before attempting removal or force remove
[root@ysl ~]# 

启动和停止容器的操作
docker start  #启动
docker restart  #重启
docker stop  #关闭
docker kill
[root@ysl ~]# docker run -it centos /bin/bash
[root@d0ae0f87520f /]# exit
exit
[root@ysl ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@ysl ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                      PORTS               NAMES
d0ae0f87520f        centos              "/bin/bash"         About a minute ago   Exited (0) 10 seconds ago                       eloquent_shamir
40e35224b8f8        centos              "/bin/bash"         11 minutes ago       Exited (0) 3 minutes ago                        sad_liskov
c34be0c5f89a        hello-world         "/hello"            3 hours ago          Exited (0) 3 hours ago                          practical_edison
[root@ysl ~]# docker start c34be0c5f89a
c34be0c5f89a
[root@ysl ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@ysl ~]# docker start d0ae0f87520f  # 启动容器
d0ae0f87520f
[root@ysl ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d0ae0f87520f        centos              "/bin/bash"         2 minutes ago       Up 2 seconds                            eloquent_shamir
[root@ysl ~]# docker stop d0ae0f87520f  # 关闭
d0ae0f87520f
[root@ysl ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@ysl ~]# 

常用其他命令

启动、关闭 Docker
systemctl start docker #启动Docker
systemctl stop  docker #关闭Docker

后台启动镜像
docker run -d 镜像名
# 后台运行,docker发现前台没有,容器启动后,发现自己没有提供服务,会立刻停止
[root@ysl ~]# docker run -d centos  #
831447907dacbce9d81d363cdaf946a4e1ab4e07c3ea41a4a8bebcc39f0e85dc
[root@ysl ~]# docker ps # 用docker ps 查看的时候 发现停止了,也就是 -d 可以检测容器中是否有提供的服务,没有的话将自动关闭 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@ysl ~]# 

查看日志
docker logs
docker logs -f -t --tail n 【id】# 查看日志,由于没有运行脚本,所以啥也没显示
docker logs --help  # 查看帮助文档

Usage:	docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
#运行centos里面加个脚本
[root@ysl ~]# docker run -d centos /bin/sh -c "while true;do echo 加油;sleep 1;done"
a090835bd80a92c306552ad1e3f8878d30218a3930363a02c7d3dda9fd58dc11
[root@ysl ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
a090835bd80a        centos              "/bin/sh -c 'while t…"   37 seconds ago      Up 36 seconds                           mystifying_knuth
a1a1ec80a208        centos              "/bin/bash"              4 minutes ago       Up 4 minutes                            charming_ride
[root@ysl ~]# docker logs -f -t --tail 10 a090835bd80a  # 查看日志 发现隔一秒打印一条
2020-12-06T11:06:27.430555293Z 加油
2020-12-06T11:06:28.432140658Z 加油
2020-12-06T11:06:29.433564252Z 加油
2020-12-06T11:06:30.435042497Z 加油
2020-12-06T11:06:31.436566532Z 加油
2020-12-06T11:06:32.438258329Z 加油
2020-12-06T11:06:33.439975118Z 加油
2020-12-06T11:06:34.441652220Z 加油
2020-12-06T11:06:35.443275405Z 加油

查看正在运行容器的完整信息
docker inspect 容器名
[root@ysl ~]# docker inspect a090835bd80a
[
    {
		#完整id    
        "Id": "a090835bd80a92c306552ad1e3f8878d30218a3930363a02c7d3dda9fd58dc11",
        #创建时间
        "Created": "2020-12-06T11:05:28.871924563Z",
       #脚本位置
       "Path": "/bin/sh",
        #运行的脚本
        "Args": [
            "-c",
            "while true;do echo 加油;sleep 1;done"
        ],
        "State": {
            "Status": "running", #状态,正在运行
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 102361, #父进程id
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-12-06T11:05:29.359496168Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        #来源镜像
        "Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566",
        "ResolvConfPath": "/var/lib/docker/containers/a090835bd80a92c306552ad1e3f8878d30218a3930363a02c7d3dda9fd58dc11/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/a090835bd80a92c306552ad1e3f8878d30218a3930363a02c7d3dda9fd58dc11/hostname",
        "HostsPath": "/var/lib/docker/containers/a090835bd80a92c306552ad1e3f8878d30218a3930363a02c7d3dda9fd58dc11/hosts",
        "LogPath": "/var/lib/docker/containers/a090835bd80a92c306552ad1e3f8878d30218a3930363a02c7d3dda9fd58dc11/a090835bd80a92c306552ad1e3f8878d30218a3930363a02c7d3dda9fd58dc11-json.log",
        "Name": "/mystifying_knuth",
        "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,
            "Capabilities": null,
            "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/e23f8f5784064d4b3702358bf17453096310b90f0dd43c180fafb30109672e6e-init/diff:/var/lib/docker/overlay2/ebace8a6bc46f06628921d249a99eb8ec3b12fdb47a2c40e66989690c97969d1/diff",
                "MergedDir": "/var/lib/docker/overlay2/e23f8f5784064d4b3702358bf17453096310b90f0dd43c180fafb30109672e6e/merged",
                "UpperDir": "/var/lib/docker/overlay2/e23f8f5784064d4b3702358bf17453096310b90f0dd43c180fafb30109672e6e/diff",
                "WorkDir": "/var/lib/docker/overlay2/e23f8f5784064d4b3702358bf17453096310b90f0dd43c180fafb30109672e6e/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],#挂载
        #基本配置
        "Config": {
            "Hostname": "a090835bd80a",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ], #基本环境变量
            #基本命令
            "Cmd": [
                "/bin/sh",
                "-c",
                "while true;do echo 加油;sleep 1;done"
            ],
            "Image": "centos",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20200809",
                "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": "a7b521474bb5603d76d12275115a14d85664a860b463f438859711ac71a0b032",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/a7b521474bb5",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "a8129a09e534148c7339fd04039ead2704468dec190059ad58c4ea376077e41c",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:03",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "6b3878e096dcd5ddd4238ff53e18ff7dc6e5056d7328a1dce1b980b4aedff9e4",
                    "EndpointID": "a8129a09e534148c7339fd04039ead2704468dec190059ad58c4ea376077e41c",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }
        }
    }
]

进入正在运行的容器
docker exec -it 容器id   /bin/bash   # 方法1 (常用)
docker attach 容器id 
#测试
#方法 1
[root@ysl ~]# docker exec -it a1a1ec80a208 /bin/bash
[root@a1a1ec80a208 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@a1a1ec80a208 /]# ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 11:01 pts/0    00:00:00 /bin/bash
root          15       0  1 11:23 pts/1    00:00:00 /bin/bash
root          29      15  0 11:24 pts/1    00:00:00 ps -ef
[root@a1a1ec80a208 /]# exit
exit
[root@ysl ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
a1a1ec80a208        centos              "/bin/bash"         25 minutes ago      Up 58 seconds                           charming_ride
#方法 2
[root@ysl ~]# docker attach a1a1ec80a208
[root@a1a1ec80a208 /]# docker ps 
bash: docker: command not found
[root@a1a1ec80a208 /]# ps -ef 
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 11:01 pts/0    00:00:00 /bin/bash
root          31       1  0 11:24 pts/0    00:00:00 ps -ef
[root@a1a1ec80a208 /]# exit
exit
[root@ysl ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

# 区别
# docker exec # 进入容器后开启一个新的终端,可以在里面操作,在exit退出后,容器正常运行
# docker attach 进入容器正在执行的终端,不会启动新的进程,在exit退出后,容器关闭

从容器内将文件拷贝到主机
docker cp 容器id: 容器文件全路径 需要拷贝到主机的路径
[root@ysl ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
a1a1ec80a208        centos              "/bin/bash"         2 hours ago         Up 2 hours                              charming_ride      
[root@ysl ~]# docker attach a1a1ec80a208
[root@a1a1ec80a208 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@a1a1ec80a208 /]# cd home/
[root@a1a1ec80a208 home]# ls     
[root@a1a1ec80a208 home]# touch test.java
[root@a1a1ec80a208 home]# ls
test.java
[root@a1a1ec80a208 home]# exit
exit
[root@ysl ~]# cd /home/
[root@ysl home]# ls
mysql
#容器文件拷贝到主机
[root@ysl home]# docker cp a1a1ec80a208:/home/test.java /home
[root@ysl home]# ls
mysql  test.java 
查看内存占用
docker stats
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
a1a1ec80a208        charming_ride       0.00%               1.102MiB / 1.705GiB   0.06%               1.87kB / 0B         0B / 0B             1

Docker 镜像

posted @ 2020-12-06 21:51  十一YS  阅读(170)  评论(0编辑  收藏  举报