Docker学习笔记01

别裁伪体亲风雅,转益多师是汝师。这篇文章主要讲述Docker学习笔记01相关的知识,希望能为你提供帮助。
1、简介Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows操作系统的机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。

Docker学习笔记01

文章图片

2、Docker安装2.1 找到系统安装光盘镜像【Docker学习笔记01】
Docker学习笔记01

文章图片

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 FilesystemSizeUsed Avail Use% Mounted on devtmpfs1.9G01.9G0% /dev tmpfs1.9G18M1.9G1% /run tmpfs1.9G01.9G0% /sys/fs/cgroup /dev/mapper/rhel-root17G4.0G13G24% / tmpfs376M16K376M1% /run/user/42 tmpfs376M3.5M373M1% /run/user/0 tmpfs376M4.0K376M1% /run/user/1000 /dev/sda11014M169M846M17% /boot /dev/sr06.7G6.7G0 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. ======================================================================================================= PackageArchVersionRepositorySize ======================================================================================================= Installing: podman-dockernoarch1.0.0-2.git921f98f.module+el8+2785+ff8a053fAppSteram28 kTransaction Summary ======================================================================================================= Install1 PackageTotal 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.noarch1/1 Running scriptlet: podman-docker-1.0.0-2.git921f98f.module+el8+2785+ff8a053f.noarch1/1 Verifying: podman-docker-1.0.0-2.git921f98f.module+el8+2785+ff8a053f.noarch1/1 Installed products updated.Installed: podman-docker-1.0.0-2.git921f98f.module+el8+2785+ff8a053f.noarchComplete!

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 IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES 31ab9dbd5071docker.io/library/ubuntu:latest/bin/bashAbout an hour agoExited (100) About an hour agohardcore_hopper 7f08ac771ac4docker.io/library/ubuntu:latest/bin/bashAbout an hour agoExited (0) About an hour agommx f38d826c7abedocker.io/library/ubuntu:latest/bin/bashAbout an hour agoExited (0) About an hour agostupefied_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`IDNAMECPU %MEM USAGE / LIMITMEM %NET IOBLOCK IOPIDS 510b5a0d583edazzling_knuth--1.642MB / 3.941GB0.04%908B / 2.506kB-- / --1 da5bca339ba1practical_spence--1.671MB / 3.941GB0.04%908B / 3.512kB-- / --1 69d50bef69a7mmx--1.79MB / 3.941GB0.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



    推荐阅读