Docker学习笔记01

Posted 明明星空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker学习笔记01相关的知识,希望对你有一定的参考价值。

1、简介

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows操作系统的机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。

2、Docker安装

2.1 找到系统安装光盘镜像

2.2 配置yum仓库

[root@localhost ~]# cat /etc/yum.repos.d/redhat.repo 
#
# Certificate-Based Repositories
# Managed by (rhsm) subscription-manager
#
# *** This file is auto-generated.  Changes made here will be over-written. ***
# *** Use "subscription-manager repo-override --help" if you wish to make changes. ***
#
# If this file is empty and this system is subscribed consider
# a "yum repolist" to refresh available repos
#
[BaseOS]
name=BaseOS
baseurl=file:///media/cdrom/BaseOS
enabled=1
gpgcheck=0
[AppSteram]
name=AppStream
baseurl=file:///media/cdrom/AppStream
enabled=1
gpgcheck=0

2.3 新建挂载点,将挂载点写入开机自启文件夹

[root@localhost ~]# mkdir -p /media/cdrom/
[root@localhost ~]# echo "/dev/cdrom /media/cdrom iso9660 defaults 0 0" >> /etc/fstab

2.4 挂载文件,并查看

[root@localhost ~]# mount -a
mount: /media/cdrom: WARNING: device write-protected, mounted read-only.
[root@localhost ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               1.9G     0  1.9G   0% /dev
tmpfs                  1.9G   18M  1.9G   1% /run
tmpfs                  1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/rhel-root   17G  4.0G   13G  24% /
tmpfs                  376M   16K  376M   1% /run/user/42
tmpfs                  376M  3.5M  373M   1% /run/user/0
tmpfs                  376M  4.0K  376M   1% /run/user/1000
/dev/sda1             1014M  169M  846M  17% /boot
/dev/sr0               6.7G  6.7G     0 100% /media/cdrom

2.5 安装Docker

[root@localhost ~]# dnf install docker
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Last metadata expiration check: 0:07:12 ago on Mon 07 Mar 2022 02:07:21 PM CST.
Dependencies resolved.
=======================================================================================================
 Package            Arch        Version                                           Repository      Size
=======================================================================================================
Installing:
 podman-docker      noarch      1.0.0-2.git921f98f.module+el8+2785+ff8a053f       AppSteram       28 k

Transaction Summary
=======================================================================================================
Install  1 Package

Total size: 28 k
Installed size: 3.5 k
Is this ok [y/N]: y
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                               1/1 
  Installing       : podman-docker-1.0.0-2.git921f98f.module+el8+2785+ff8a053f.noarch              1/1 
  Running scriptlet: podman-docker-1.0.0-2.git921f98f.module+el8+2785+ff8a053f.noarch              1/1 
  Verifying        : podman-docker-1.0.0-2.git921f98f.module+el8+2785+ff8a053f.noarch              1/1 
Installed products updated.

Installed:
  podman-docker-1.0.0-2.git921f98f.module+el8+2785+ff8a053f.noarch                                     

Complete!

2.6 修改镜像加速器

# 我选用的是阿里镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-EOF

  "registry-mirrors": ["https://阿里ID.mirror.aliyuncs.com"]

EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

2.7 修改别名

[root@localhost ~]# echo "alias podman=docker" >> .bashrc 
[root@localhost ~]# docker -v
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
podman version 1.0.2-dev

3、测试操作

3.1回显有结果,代表可以运行

[root@localhost ~]# docker info
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
host:
  BuildahVersion: 1.6-dev
  Conmon:
    package: podman-1.0.0-2.git921f98f.module+el8+2785+ff8a053f.x86_64
    path: /usr/libexec/podman/conmon
    version: conmon version 1.14.0-dev, commit: be8255a19cda8a598d76dfa49e16e337769d4528-dirty
  Distribution:
    distribution: "rhel"
    version: "8.0"
  MemFree: 1373954048
  MemTotal: 3941175296
  OCIRuntime:
    package: runc-1.0.0-54.rc5.dev.git2abd837.module+el8+2769+577ad176.x86_64
    path: /usr/bin/runc
    version: runc version spec: 1.0.0
  SwapFree: 2147479552
  SwapTotal: 2147479552
  arch: amd64
  cpus: 4
  hostname: localhost.localdomain
  kernel: 4.18.0-80.el8.x86_64
  os: linux
  rootless: false
  uptime: 47m 3.88s
insecure registries:
  registries: []
registries:
  registries:
  - registry.redhat.io
  - quay.io
  - docker.io
store:
  ConfigFile: /etc/containers/storage.conf
  ContainerStore:
    number: 0
  GraphDriverName: overlay
  GraphOptions:
  - overlay.override_kernel_check=true
  GraphRoot: /var/lib/containers/storage
  GraphStatus:
    Backing Filesystem: xfs
    Native Overlay Diff: "true"
    Supports d_type: "true"
  ImageStore:
    number: 0
  RunRoot: /var/run/containers/storage

3.2 运行我们第一个容器

命令&参数 含义
docker run 运行容器
docker pull 下载镜像
-i 保证容器中STDIN是开启状态
-t 要为Docker创建的容器分配一个伪终端
ubuntu 操作系统
/bin/bash 使用的伪终端

3.2 下载镜像

# 镜像下载有进度条
[root@localhost ~]# docker pull ubuntu
Trying to pull registry.redhat.io/ubuntu:latest...Failed
Trying to pull quay.io/ubuntu:latest...Failed
Trying to pull docker.io/ubuntu:latest...Getting image source signatures
Copying blob 7c3b88808835: 27.24 MiB / 27.24 MiB [==========================] 9s
Copying config 2b4cba85892a: 1.43 KiB / 1.43 KiB [==========================] 0s
Writing manifest to image destination
Storing signatures
2b4cba85892afc2ad8ce258a8e3d9daa4a1626ba380677cee93ef2338da442ab

3.3 运行容器

[root@localhost ~]# docker run -i -t ubuntu /bin/bash
# 进入界面,并且能看到版本号等相关内容
root@f38d826c7abe:/# uname -a
Linux f38d826c7abe 4.18.0-80.el8.x86_64 #1 SMP Wed Mar 13 12:02:46 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
root@f38d826c7abe:/# hostname 
f38d826c7abe
root@f38d826c7abe:/# 

4、 容器的基础操作命令

4.1 使用容器

# 运行Ubuntu
[root@localhost ~]docker run -i -t ubuntu /bin/bash
root@f38d826c7abe:/# exit

4.2 查看当前运行容器

[root@localhost ~]# docker ps -a
CONTAINER ID  IMAGE                            COMMAND    CREATED            STATUS                          PORTS  NAMES
31ab9dbd5071  docker.io/library/ubuntu:latest  /bin/bash  About an hour ago  Exited (100) About an hour ago         hardcore_hopper
7f08ac771ac4  docker.io/library/ubuntu:latest  /bin/bash  About an hour ago  Exited (0) About an hour ago           mmx
f38d826c7abe  docker.io/library/ubuntu:latest  /bin/bash  About an hour ago  Exited (0) About an hour ago           stupefied_sammet

4.3 命名容器

[root@localhost ~]#  docker run --name mmx -i -t ubuntu /bin/bash

4.4 启动、关闭容器

[root@localhost ~]# docker start mmx
mmx
[root@localhost ~]# docker stop mmx
69d50bef69a7e6bfd05bea4eeda40b2de8df3c2bb2bce90bce75681a9c458394

4.4 附着到已经启动的容器中

[root@localhost ~]# docker attach mmx
root@7f08ac771ac4:/# 
# 或者
[root@localhost ~]# docker attach 7f08ac771ac4
root@7f08ac771ac4:/# 
root@7f08ac771ac4:/# hostname 
7f08ac771ac4
root@7f08ac771ac4:/# 

4.5 删除容器

[root@localhost ~]# docker rm mmx
7f08ac771ac4d128c163d5f493e69467ed7bc1d1be5790668621649545772d2d

4.6 删除所有容器

[root@localhost ~]# docker rm `docker ps -a -q`
3d40b2108a853415068f5ed621543b9643a1a86a5122ae8c68189c3dc023f42e
8cd290cb2d531a0d021498ac9d73792e5bcbed8fdf43170cfc7bffc3480e065b
0c60710a2707db31250a9d97d4ccf0b042a86ac6625d7b42b65bdd737faa9af3

4.6 启动所有容器

[root@localhost ~]# docker start `docker ps -a -q`
510b5a0d583e
da5bca339ba1
69d50bef69a7

4.7 查看所有容器的运行状态

[root@localhost ~]# podman stats `docker ps -aq`

ID             NAME               CPU %   MEM USAGE / LIMIT   MEM %   NET IO              BLOCK IO   PIDS
510b5a0d583e   dazzling_knuth     --      1.642MB / 3.941GB   0.04%   908B / 2.506kB      -- / --    1
da5bca339ba1   practical_spence   --      1.671MB / 3.941GB   0.04%   908B / 3.512kB      -- / --    1
69d50bef69a7   mmx                --      1.79MB / 3.941GB    0.05%   1.118kB / 9.182kB   -- / --    1

4.8 设置容器自动重启

docker run --restart=always --name mmx -d ubuntu /bin/bash -c " while true; do echo hello world; sleep 1; done"

4.9 查看容器详细信息

[root@localhost ~]# docker inspect mmx
[
    
        "ID": "69d50bef69a7e6bfd05bea4eeda40b2de8df3c2bb2bce90bce75681a9c458394",
        "Created": "2022-03-07T18:07:28.182543882+08:00",
        "Path": "/bin/bash",
        "Args": [
            "/bin/bash"
        ],
        "State": 
            "OciVersion": "1.0.1-dev",
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 13346,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-03-08T17:11:36.9915363+08:00",
            "FinishedAt": "2022-03-08T17:05:58.237282858+08:00"
        ,
        "Image": "2b4cba85892afc2ad8ce258a8e3d9daa4a1626ba380677cee93ef2338da442ab",
        "ImageName": "docker.io/library/ubuntu:latest",
        "Rootfs": "",
        "ResolvConfPath": "/var/run/containers/storage/overlay-containers/69d50bef69a7e6bfd05bea4eeda40b2de8df3c2bb2bce90bce75681a9c458394/userdata/resolv.conf",
        "HostnamePath": "/var/run/containers/storage/overlay-containers/69d50bef69a7e6bfd05bea4eeda40b2de8df3c2bb2bce90bce75681a9c458394/userdata/hostname",
        "HostsPath": "/var/run/containers/storage/overlay-containers/69d50bef69a7e6bfd05bea4eeda40b2de8df3c2bb2bce90bce75681a9c458394/userdata/hosts",
        "StaticDir": "/var/lib/containers/storage/overlay-containers/69d50bef69a7e6bfd05bea4eeda40b2de8df3c2bb2bce90bce75681a9c458394/userdata",
        "LogPath": "/var/lib/containers/storage/overlay-containers/69d50bef69a7e6bfd05bea4eeda40b2de8df3c2bb2bce90bce75681a9c458394/userdata/ctr.log",
        "Name": "mmx",
        "RestartCount": 0,
        "Driver": "overlay",
        "MountLabel": "system_u:object_r:container_file_t:s0:c93,c901",
        "ProcessLabel": "system_u:system_r:container_t:s0:c93,c901",
        "AppArmorProfile": "",
        "EffectiveCaps": [
            "CAP_CHOWN",
            "CAP_DAC_OVERRIDE",
            "CAP_FSETID",
            "CAP_FOWNER",
            "CAP_MKNOD",
            "CAP_NET_RAW",
            "CAP_SETGID",
            "CAP_SETUID",
            "CAP_SETFCAP",
            "CAP_SETPCAP",
            "CAP_NET_BIND_SERVICE",
            "CAP_SYS_CHROOT",
            "CAP_KILL",
            "CAP_AUDIT_WRITE"
        ],
        "BoundingCaps": [
            "CAP_CHOWN",
            "CAP_DAC_OVERRIDE",
            "CAP_FSETID",
            "CAP_FOWNER",
            "CAP_MKNOD",
            "CAP_NET_RAW",
            "CAP_SETGID",
            "CAP_SETUID",
            "CAP_SETFCAP",
            "CAP_SETPCAP",
            "CAP_NET_BIND_SERVICE",
            "CAP_SYS_CHROOT",
            "CAP_KILL",
            "CAP_AUDIT_WRITE"
        ],
        "ExecIDs": [],
        "GraphDriver": 
            "Name": "overlay",
            "Data": 
                "LowerDir": "/var/lib/containers/storage/overlay/68a85fa9d77ecac87de23805c4be8766bda08a86787e324036cbcf84b62640fa/diff",
                "MergedDir": "/var/lib/containers/storage/overlay/05bba7e4bb5e3d8586626e6c1f501156da8b1c5cecf6862c86ca7c6f6ebb3c82/merged",
                "UpperDir": "/var/lib/containers/storage/overlay/05bba7e4bb5e3d8586626e6c1f501156da8b1c5cecf6862c86ca7c6f6ebb3c82/diff",
                "WorkDir": "/var/lib/containers/storage/overlay/05bba7e4bb5e3d8586626e6c1f501156da8b1c5cecf6862c86ca7c6f6ebb3c82/work"
            
        ,
        "Mounts": [
            
                "destination": "/proc",
                "type": "proc",
                "source": "proc",
                "options": [
                    "nosuid",
                    "noexec",
                    "nodev"
                ]
            ,
            
                "destination": "/dev",
                "type": "tmpfs",
                "source": "tmpfs",
                "options": [
                    "nosuid",
                    "strictatime",
                    "mode=755",
                    "size=65536k"
                ]
            ,
            
                "destination": "/sys",
                "type": "sysfs",
                "source": "sysfs",
                "options": [
                    "nosuid",
                    "noexec",
                    "nodev",
                    "ro"
                ]
            ,
            
                "destination": "/dev/pts",
                "type": "devpts",
                "source": "devpts",
                "options": [
                    "nosuid",
                    "noexec",
                    "newinstance",
                    "ptmxmode=0666",
                    "mode=0620",
                    "gid=5"
                ]
            ,
            
                "destination": "/dev/mqueue",
                "type": "mqueue",
                "source": "mqueue",
                "options": [
                    "nosuid",
                    "noexec",
                    "nodev"
                ]
            ,
            
                "destination": "/dev/shm",
                "type": "bind",
                "source": "overlay-containers",
                "options": [
                    "bind",
                    "private"
                ]
            ,
            
                "destination": "/etc/hostname",
                "type": "bind",
                "source": "/var/run/containers/storage/overlay-containers/69d50bef69a7e6bfd05bea4eeda40b2de8df3c2bb2bce90bce75681a9c458394/userdata/hostname",
                "options": [
                    "bind",
                    "private"
                ]
            ,
            
                "destination": "/run/.containerenv",
                "type": "bind",
                "source": "/var/run/containers/storage/overlay-containers/69d50bef69a7e6bfd05bea4eeda40b2de8df3c2bb2bce90bce75681a9c458394/userdata/.containerenv",
                "options": [
                    "bind",
                    "private"
                ]
            ,
            
                "destination": "/run/secrets",
                "type": "bind",
                "source": "/var/run/containers/storage/overlay-containers/69d50bef69a7e6bfd05bea4eeda40b2de8df3c2bb2bce90bce75681a9c458394/userdata/run/secrets",
                "options": [
                    "bind",
                    "private"
                ]
            ,
            
                "destination": "/etc/hosts",
                "type": "bind",
                "source": "/var/run/containers/storage/overlay-containers/69d50bef69a7e6bfd05bea4eeda40b2de8df3c2bb2bce90bce75681a9c458394/userdata/hosts",
                "options": [
                    "bind",
                    "private"
                ]
            ,
            
                "destination": "/etc/resolv.conf",
                "type": "bind",
                "source": "/var/run/containers/storage/overlay-containers/69d50bef69a7e6bfd05bea4eeda40b2de8df3c2bb2bce90bce75681a9c458394/userdata/resolv.conf",
                "options": [
                    "bind",
                    "private"
                ]
            ,
            
                "destination": "/sys/fs/cgroup",
                "type": "cgroup",
                "source": "cgroup",
                "options": [
                    "rprivate",
                    "nosuid",
                    "noexec",
                    "nodev",
                    "relatime",
                    "ro"
                ]
            
        ],
        "Dependencies": [],
        "NetworkSettings": 
            "Bridge": "",
            "SandboxID": "",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": [],
            "SandboxKey": "/var/run/netns/cni-3b5b0d26-96e3-b5e5-bb66-56cc979ec8f5",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "10.88.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "10.88.0.10",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "62:93:22:04:21:58"
        ,
        "ExitCommand": null,
        "Namespace": "",
        "IsInfra": false,
        "HostConfig": 
            "ContainerIDFile": "",
            "LogConfig": null,
            "NetworkMode": "bridge",
            "PortBindings": null,
            "AutoRemove": false,
            "CapAdd": [],
            "CapDrop": [],
            "DNS": [],
            "DNSOptions": [],
            "DNSSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "host",
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": [],
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 65536000,
            "Runtime": "runc",
            "ConsoleSize": null,
            "CpuShares": null,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": null,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": null,
            "CpuQuota": null,
            "CpuRealtimePeriod": null,
            "CpuRealtimeRuntime": null,
            "CpuSetCpus": "",
            "CpuSetMems": "",
            "Devices": null,
            "DiskQuota": 0,
            "KernelMemory": null,
            "MemoryReservation": null,
            "MemorySwap": null,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": [],
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "Tmpfs": []
        ,
        "Config": 
            "Hostname": "",
            "Domainname": "",
            "User": 
                "uid": 0,
                "gid": 0
            ,
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "TERM=xterm",
                "HOSTNAME=",
                "container=podman"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "docker.io/library/ubuntu:latest",
            "Volumes": null,
            "WorkingDir": "/",
            "Entrypoint": "",
            "Labels": null,
            "Annotations": 
                "io.kubernetes.cri-o.ContainerType": "sandbox",
                "io.kubernetes.cri-o.TTY": "true"
            ,
            "StopSignal": 15
        
    
]
# 该命令是查看容器是否开启

[root@localhost ~]# docker inspect mmx  --format=" .State.Running "
true

以上是关于Docker学习笔记01的主要内容,如果未能解决你的问题,请参考以下文章

docker学习笔记

Docker学习笔记

Docker 学习笔记总结

Docker 学习笔记总结

Docker学习笔记-p1(初始Docker)

docker 学习笔记