我安装的环境是CentOS 7 的Linux ,以下是Linux下的命令,来自官方文档。
# 1.安装所需的包 yum install -y yum-utils # 2.设置镜像仓库 yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo #国外地址 yum-config-manager \ --add-repo \ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #阿里云 #3. 更新yum软件包索引 yum makecache fast #4.安装docker docker-ce社区版 ee是企业版 yum install docker-ce docker-ce-cli containerd.io #5.启动Docker systemctl start docker #6.验证Docker是否成功 docker version # 7.hello-world docker run hello-world # 8.查看下载的hello-world镜像 docker images 卸载docker # 1.卸载依赖 yum remove docker-ce docker-ce-cli containerd.io # 2.删除资源 rm -rf /var/lib/docker #docker默认工作路径 rm -rf /var/lib/containerd
为了镜像速度,可以在阿里云后台配置一下镜像加速器。
1、登陆阿里云
2、阿里云后台--》弹性计算--》容器加速器--》阿里云镜像加速器
以上红框内的指令 ,直接复制到Linux执行就得啦。
下图提及到DockerHub的概念,就是官方的仓库里会放着许多不同版本的镜像。只有在本机找不到的时候会从dockerHub查找,都没有就会返回错误信息了。
Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上,通过Socket从客户端访问。DockerServer接收到Docke-Cilent的指令,就会执行该指令。
1、Docker有着比虚拟机更少的抽象层,由于Docker不需要Hypervisor实现硬件资源虚拟化,运行在Docker容器上的程序直接使用的都是实际物理机的硬件资源,因此在Cpu、内存利用率上Docker将会在效率上有明显优势。
2、Docker利用的是宿主机的内核,而不需要Guest OS,因此,当新建一个容器时,Docker不需要和虚拟机一样重新加载一个操作系统,避免了引导、加载操作系统内核这个比较费时费资源的过程,当新建一个虚拟机时,虚拟机软件需要加载Guest OS,这个新建过程是分钟级别的,而Docker由于直接利用宿主机的操作系统则省略了这个过程,因此新建一个Docker容器只需要几秒
--------------------------------------------------------------------------------------------------------------------------------
帮助文档地址:https://docs.docker.com/reference/docker
docker version #显示docker的版本信息 docker info #显示docker的系统信息 包括镜像和容器的数量 docker 命令 --help #万能命令 命令==例如ps run等
docker images 查看所有本地的主机上的镜像
[root@localhost alenwong]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest d1165f221234 4 months ago 13.3kB #REPOSITORY 镜像仓库源 #TAG 标识,我理解是版本的标签 #IMAGE ID 镜像ID #CREATE 该镜像创建的时间 #SIZE 该镜像的大小 #拓展 -a, --all #列出所有镜像 -q, --quiet #只显示出镜像的id
#docker search 镜像名 #搜索mysql镜像 docker search mysql #返回以下 NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 11142 [OK] mariadb MariaDB Server is a high performing open sou… 4224 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 829 [OK] #搜索收藏数大于3000的mysql docker search mysql --filter=stars=3000 #返回以下 NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 11142 [OK] mariadb MariaDB Server is a high performing open sou… 4224 [OK]
docker pull 镜像名 不写版本下载:
[root@localhost alenwong]# docker pull mysql #不指定版本 Using default tag: latest #tag latest=默认最新的版本 latest: Pulling from library/mysql b4d181a07f80: Pull complete #分层下载,docker iamge的核心 联合文件系统 a462b60610f5: Pull complete 578fafb77ab8: Pull complete 524046006037: Pull complete d0cbe54c8855: Pull complete aa18e05cc46d: Pull complete 32ca814c833f: Pull complete 9ecc8abdb7f5: Pull complete ad042b682e0f: Pull complete 71d327c6bb78: Pull complete 165d1d10a3fa: Pull complete 2f40c47d0626: Pull complete Digest: sha256:52b8406e4c32b8cf0557f1b74517e14c5393aff5cf0384eff62d9e81f4985d4b Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest #该镜像的真实地址 #等价于: docker pull mysql =等=价=于= docker pull docker.io/library/mysql:latest
docker pull 指定版本,注意: 版本号必须来自官方的DockerHub,不能乱写。 点入DockerHub搜想要d拉取的镜像。 滚下面可以看到指定几个的版本号。
docker pull 镜像名:版本号
[root@localhost alenwong]# docker pull mysql:5.7 5.7: Pulling from library/mysql b4d181a07f80: Already exists a462b60610f5: Already exists 578fafb77ab8: Already exists 524046006037: Already exists d0cbe54c8855: Already exists aa18e05cc46d: Already exists 32ca814c833f: Already exists 52645b4af634: Pull complete bca6a5b14385: Pull complete 309f36297c75: Pull complete 7d75cacde0f8: Pull complete Digest: sha256:1a2f9cd257e75cc80e9118b303d1648366bc2049101449bf2c8d82b022ea86b7 Status: Downloaded newer image for mysql:5.7 docker.io/library/mysql:5.7
#删除指定ID镜像 docker rmi -f 镜像ID #删除多个指定ID镜像 docker rmi -f 镜像ID 镜像ID 镜像ID 镜像ID #递归删除镜像ID docker fim -f $(docker images -aq) #等于传入全部镜像ID
我这里先下载一个CentOS套娃容器进行学习..
docker pull centos
docker run [可选参数] 镜像名或镜像ID? #参数说明 --name="NAME" 容器名称如 tomcat01 -d 后台方式运行, 注意:如果碰到没有前台交互的镜像,会自动退出 -it 启用前台方式交互运行,进入容器查看内容 -p 指定容器的端口 -p 8080:8080 -p 主机端口:容器端口 即主机端口映射到容器端口 如-p 8080:8080 -p 容器端口 如 -p 8080 -p 不写就随机指定端口 ----------demo #/bin/bash的意思是以这种方式的操作控制台形式进行交互(一般Linux都是这种) [root@localhost alenwong]# docker run -it centos /bin/bash [root@63b99200973f /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@63b99200973f /]# exit exit [root@localhost alenwong]# ls 公共 模板 视频 图片 文档 下载 音乐 桌面 [root@localhost alenwong]#
exit , 直接退出容器 Ctrl + P + Q 退出容器但不结束
#docer ps
空 #列出当前正在运行的容器
-a #列出当前正在运行的容器,并带出历史内容
-n=? #显示最近创建的内容 ?是显示的个数
-q #只显示容器的id
#demo 下面是没有容器在运行 [root@localhost alenwong]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES #demo 下面是有容器在运行 [root@localhost alenwong]# docker run -it centos /bin/bash [root@2c3e7ecb2926 /]# [root@localhost alenwong]# #这里我是Ctril + P+Q 操作 [root@localhost alenwong]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2c3e7ecb2926 centos "/bin/bash" 27 seconds ago Up 19 seconds brave_lederberg [root@localhost alenwong]# #demo 下面是查看曾经的容器记录 [root@localhost alenwong]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2c3e7ecb2926 centos "/bin/bash" 2 minutes ago Exited (0) 9 seconds ago brave_lederberg 63b99200973f centos "/bin/bash" 18 minutes ago Exited (0) 17 minutes ago elastic_curran f1e72d6e3a34 hello-world "/hello" 4 hours ago Exited (0) 4 hours ago quizzical_gagarin 7aa43808fbf8 hello-world "/hello" 4 hours ago Exited (0) 4 hours ago hungry_neumann
删除指定容器 docker rm #容器id #删除所有容器 docker rm -f $(docker ps -aq) #删除所有容器 docker ps -a -q|xargs docker rm
#启动容器 docker start 容器id #重启动容器 docker restart 容器id #停止当前正在运行动容器 docker stop 容器id #强制停止当前容器 docker kill 容器id
#容器要在跑,才可以看日志哦.. docker logs -f -t --tail 10 #容器id 没有日志 # 自己编写一段shell脚本 [root@bogon /]# docker run -d centos /bin/sh -c "while true;do echo Sxr;sleep 1;done"[root@bogon /]# docker psPORTS NAMESd5d5d925582f centos #显示日志-tf #显示日志 f带上时间--tail number #要显示的日志条数 [root@bogon /]# docker logs -f -t --tail 10 d5d5d925582f [root@bogon /]# docker logs -tf --tail 10 d5d5d925582f
[root@bogon /]# docker top 容器idUID PID PPID C STIME TTY TIME root 3064 3044 0 09:37 ? 00:00:00 root 3117 3064 0 09:38 ? 00:00:00
[root@localhost alenwong]# docker inspect 436b9a7bc8cd [ { "Id": "436b9a7bc8cd291d3f138cab1803dab00bc668eb77fba8476f08c90aae54d009", "Created": "2021-07-20T13:57:06.359593394Z", "Path": "/hello", "Args": [], "State": { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 0, "ExitCode": 0, "Error": "", "StartedAt": "2021-07-20T13:57:10.951556968Z", "FinishedAt": "2021-07-20T13:57:10.912682656Z" }, "Image": "sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726", "ResolvConfPath": "/var/lib/docker/containers/436b9a7bc8cd291d3f138cab1803dab00bc668eb77fba8476f08c90aae54d009/resolv.conf", "HostnamePath": "/var/lib/docker/containers/436b9a7bc8cd291d3f138cab1803dab00bc668eb77fba8476f08c90aae54d009/hostname", "HostsPath": "/var/lib/docker/containers/436b9a7bc8cd291d3f138cab1803dab00bc668eb77fba8476f08c90aae54d009/hosts", "LogPath": "/var/lib/docker/containers/436b9a7bc8cd291d3f138cab1803dab00bc668eb77fba8476f08c90aae54d009/436b9a7bc8cd291d3f138cab1803dab00bc668eb77fba8476f08c90aae54d009-json.log", "Name": "/exciting_williamson", "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/02df75b141067ac7ea2f22c494597397058416f08f3ff7e87192cbb7661acea6-init/diff:/var/lib/docker/overlay2/14a05bbb4b6427579b96a7f525fad5517a16042681814b0b96fa3652e09566e3/diff", "MergedDir": "/var/lib/docker/overlay2/02df75b141067ac7ea2f22c494597397058416f08f3ff7e87192cbb7661acea6/merged", "UpperDir": "/var/lib/docker/overlay2/02df75b141067ac7ea2f22c494597397058416f08f3ff7e87192cbb7661acea6/diff", "WorkDir": "/var/lib/docker/overlay2/02df75b141067ac7ea2f22c494597397058416f08f3ff7e87192cbb7661acea6/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "436b9a7bc8cd", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": true, "AttachStderr": true, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/hello" ], "Image": "hello-world", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "8d42c55dd527d0fa98c0dd7d43ef7c22b27f1e33766475d526c504a2ee6efd05", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/8d42c55dd527", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "", "Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "24edc026dabc9d6bccf4d8e11220ac8bf1303be9916b1e913e38e6e802121623", "EndpointID": "", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "", "DriverOpts": null } } } } ]
注意: exec是进入容器并且新开一个终端; attach是进入一个正在运行的容器,不会新开一个终端窗口。
#docker exec 容器ID [root@localhost alenwong]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dcdf651611cd mysql:5.7 "docker-entrypoint.s…" 18 minutes ago Up 18 minutes 3306/tcp, 33060/tcp mysql_01 8d9ae72043d9 centos "/bin/bash" 45 minutes ago Up 45 minutes condescending_murdock [root@localhost alenwong]# docker exec -it mysql:5.7 /bin/bash Error: No such container: mysql:5.7 [root@localhost alenwong]# clear [root@localhost alenwong]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dcdf651611cd mysql:5.7 "docker-entrypoint.s…" 19 minutes ago Up 19 minutes 3306/tcp, 33060/tcp mysql_01 8d9ae72043d9 centos "/bin/bash" 46 minutes ago Up 46 minutes condescending_murdock [root@localhost alenwong]# docker exec -it dcdf651611cd /bin/bash root@dcdf651611cd:/# mysql -uroot -p #docker attach ---------->直接进入容器,不新开窗口. [root@localhost alenwong]# docker attach dcdf651611cd
docker cp 容器id:容器路径 目标主机路径
docker cp 容器id:容器内路径 目的的主机路径 [root@bogon /home]# docker cp 容器id:/容器路径 /主机路径 #测试 [root@localhost alenwong]# pwd 先看看目前路径 /home/alenwong [root@localhost alenwong]# pwd /home/alenwong [root@localhost alenwong]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dcdf651611cd mysql:5.7 "docker-entrypoint.s…" 29 minutes ago Up 29 minutes 3306/tcp, 33060/tcp mysql_01 [root@localhost alenwong]# docker run -it centos /bin/bash #进入Centos容器创建文件 [root@c1e7eef34a80 /]# ls bin etc lib lost+found mnt proc run srv tmp var dev home lib64 media opt root sbin sys usr [root@c1e7eef34a80 /]# cd home [root@c1e7eef34a80 home]# ll bash: ll: command not found [root@c1e7eef34a80 home]# ls #该路径下没有东西 [root@c1e7eef34a80 home]# pwd /home [root@c1e7eef34a80 home]# mkdir text.txt #创建一个文件 [root@c1e7eef34a80 home]# ls text.txt [root@c1e7eef34a80 home]# exit exit [root@localhost alenwong]# docker ps -a #看看历史容器ID CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c1e7eef34a80 centos "/bin/bash" 57 seconds ago Exited (0) 17 seconds ago modest_brattain b50a426ffcf0 mysql:5.7 "docker-entrypoint.s…" 30 minutes ago Exited (1) 29 minutes ago reverent_cannon #关键是一以下的命令..........docker cp 容器id:路径 目标路径 [root@localhost alenwong]# docker cp c1e7eef34a80:/home/text.txt /home/alenwong [root@localhost alenwong]# ls text.txt 公共 模板 视频 图片 文档 下载 音乐 桌面 #多了个text.txt了吧