docker学习
专栏: 220908-小班辅导 标签: docker学习
2022-10-04 20:39:48 1941浏览
前言:
(1)每一款软件产品都是从开发到上线,从操作系统,到运行环境,再到应用配置。很多互联网公司都不得不面对的是开发和运维之间的协作,在各个软件版本的迭代后,不同版本环境的兼容,对于运维人员来说有很大的考验。
(2)环境配置如此麻烦,换一台机器就要重新来一次 ,浪费人力物力。于是很多大牛在思考,能不能从根本上解决问题,软件可以携带环境安装?也就是说,在运维人员安装部署的时候,能不能把原始环境一模一样地复制过来。
图标解释
为啥要用docker
减轻运维的工作量,解决“在我的机器上可以正常工作,在你的机器上不能正常工作”的问题。
举例:在使用Docker前相当于搬家,把之前屋子里的所有东西完完全全的搬进一个新的房子,但环境以及发生了改变。在使用Docker后,相当于搬楼,连带房子一起搬走。
docker的三要素:
镜像:Docker 镜像是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的一些配置参数(如匿名卷、环境变量、用户等)。镜像不包含任何动态数据,其内容在构建之后也不会被改变。
容器:容器的实质是进程,但与直接在宿主执行的进程不同,容器进程运行于属于自己的独立的命名空间容器可以被创建、启动、停止、删除和暂停等等,说到镜像与容器之间的关系,可以类比面向对象程序设计中的类和实例。
仓库:镜像构建完成后,可以很容易的在当前宿主机上运行,但是,如果需要在其它服务器上使用这个镜像,我们就需要一个集中的存储、分发镜像的服务,Docker Registry 就是这样的服务。一个 Docker Registry 中可以包含多个仓库;每个仓库可以包含多个标签;每个标签对应一个镜像,其中标签可以理解为镜像的版本号。
1.安装docker
curl -fsSL https://get.docker.com | bash -s docker –mirror Aliyun
如果安装失败,提示如下这个的话,改用yum安装的方式
Metadata Cache Created
sh -c ‘yum install -y -q docker-ce docker-ce-cli containerd.io docker-scan-plugin docker-compose-plugin docker-ce-rootless-
extras’
yum安装docker参考文章:
# step 1: 安装必要的一些系统工具
yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3
sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# Step 4: 更新并安装Docker-CE
(base) [root@localhost ~]# yum makecache fast
Loaded plugins: fastestmirror, langpacks
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 7.9 kB 00:00:00
* base: mirrors.aliyun.com
* epel: mirrors.bfsu.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
Metadata Cache Created
(base) [root@localhost ~]# yum -y install docker-ce
Loaded plugins: fastestmirror, langpacks
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.bfsu.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 3:20.10.17-3.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: 3:docker-ce-20.10.17-3.el7.x86_64
--> Processing Dependency: containerd.io >= 1.4.1 for package: 3:docker-ce-20.10.17-3.el7.x86_64
--> Processing Dependency: docker-ce-cli for package: 3:docker-ce-20.10.17-3.el7.x86_64
--> Processing Dependency: docker-ce-rootless-extras for package: 3:docker-ce-20.10.17-3.el7.x86_64
--> Running transaction check
---> Package container-selinux.noarch 2:2.119.2-1.911c772.el7_8 will be installed
---> Package containerd.io.x86_64 0:1.6.6-3.1.el7 will be installed
---> Package docker-ce-cli.x86_64 1:20.10.17-3.el7 will be installed
--> Processing Dependency: docker-scan-plugin(x86-64) for package: 1:docker-ce-cli-20.10.17-3.el7.x86_64
---> Package docker-ce-rootless-extras.x86_64 0:20.10.17-3.el7 will be installed
--> Processing Dependency: fuse-overlayfs >= 0.7 for package: docker-ce-rootless-extras-20.10.17-3.el7.x86_64
--> Processing Dependency: slirp4netns >= 0.4 for package: docker-ce-rootless-extras-20.10.17-3.el7.x86_64
--> Running transaction check
---> Package docker-scan-plugin.x86_64 0:0.17.0-3.el7 will be installed
---> Package fuse-overlayfs.x86_64 0:0.7.2-6.el7_8 will be installed
--> Processing Dependency: libfuse3.so.3(FUSE_3.2)(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
--> Processing Dependency: libfuse3.so.3(FUSE_3.0)(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
--> Processing Dependency: libfuse3.so.3()(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
---> Package slirp4netns.x86_64 0:0.4.3-4.el7_8 will be installed
--> Running transaction check
---> Package fuse3-libs.x86_64 0:3.6.1-4.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================================================================================
Installing:
docker-ce x86_64 3:20.10.17-3.el7 docker-ce-stable 22 M
Installing for dependencies:
container-selinux noarch 2:2.119.2-1.911c772.el7_8 extras 40 k
containerd.io x86_64 1.6.6-3.1.el7 docker-ce-stable 33 M
docker-ce-cli x86_64 1:20.10.17-3.el7 docker-ce-stable 29 M
docker-ce-rootless-extras x86_64 20.10.17-3.el7 docker-ce-stable 8.2 M
docker-scan-plugin x86_64 0.17.0-3.el7 docker-ce-stable 3.7 M
fuse-overlayfs x86_64 0.7.2-6.el7_8 extras 54 k
fuse3-libs x86_64 3.6.1-4.el7 extras 82 k
slirp4netns x86_64 0.4.3-4.el7_8 extras 81 k
Transaction Summary
==============================================================================================================================================================================================================
Install 1 Package (+8 Dependent packages)
Total download size: 97 M
Installed size: 394 M
Downloading packages:
(1/9): container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm | 40 kB 00:00:05
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-20.10.17-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY ] 670 kB/s | 45 MB 00:01:19 ETA
Public key for docker-ce-20.10.17-3.el7.x86_64.rpm is not installed
(2/9): docker-ce-20.10.17-3.el7.x86_64.rpm | 22 MB 00:01:13
(3/9): containerd.io-1.6.6-3.1.el7.x86_64.rpm | 33 MB 00:01:46
(4/9): docker-ce-rootless-extras-20.10.17-3.el7.x86_64.rpm | 8.2 MB 00:00:25
(5/9): fuse-overlayfs-0.7.2-6.el7_8.x86_64.rpm | 54 kB 00:00:05
(6/9): slirp4netns-0.4.3-4.el7_8.x86_64.rpm | 81 kB 00:00:05
(7/9): fuse3-libs-3.6.1-4.el7.x86_64.rpm | 82 kB 00:00:05
(8/9): docker-scan-plugin-0.17.0-3.el7.x86_64.rpm | 3.7 MB 00:00:11
(9/9): docker-ce-cli-20.10.17-3.el7.x86_64.rpm | 29 MB 00:01:30
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 603 kB/s | 97 MB 00:02:44
Retrieving key from https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
Importing GPG key 0x621E9F35:
Userid : "Docker Release (CE rpm) <docker@docker.com>"
Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
From : https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : docker-scan-plugin-0.17.0-3.el7.x86_64 1/9
Installing : 1:docker-ce-cli-20.10.17-3.el7.x86_64 2/9
Installing : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch 3/9
Installing : containerd.io-1.6.6-3.1.el7.x86_64 4/9
Installing : slirp4netns-0.4.3-4.el7_8.x86_64 5/9
Installing : fuse3-libs-3.6.1-4.el7.x86_64 6/9
Installing : fuse-overlayfs-0.7.2-6.el7_8.x86_64 7/9
Installing : 3:docker-ce-20.10.17-3.el7.x86_64 8/9
Installing : docker-ce-rootless-extras-20.10.17-3.el7.x86_64 9/9
Verifying : fuse3-libs-3.6.1-4.el7.x86_64 1/9
Verifying : containerd.io-1.6.6-3.1.el7.x86_64 2/9
Verifying : docker-ce-rootless-extras-20.10.17-3.el7.x86_64 3/9
Verifying : 1:docker-ce-cli-20.10.17-3.el7.x86_64 4/9
Verifying : slirp4netns-0.4.3-4.el7_8.x86_64 5/9
Verifying : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch 6/9
Verifying : 3:docker-ce-20.10.17-3.el7.x86_64 7/9
Verifying : docker-scan-plugin-0.17.0-3.el7.x86_64 8/9
Verifying : fuse-overlayfs-0.7.2-6.el7_8.x86_64 9/9
Installed:
docker-ce.x86_64 3:20.10.17-3.el7
Dependency Installed:
container-selinux.noarch 2:2.119.2-1.911c772.el7_8 containerd.io.x86_64 0:1.6.6-3.1.el7 docker-ce-cli.x86_64 1:20.10.17-3.el7 docker-ce-rootless-extras.x86_64 0:20.10.17-3.el7
docker-scan-plugin.x86_64 0:0.17.0-3.el7 fuse-overlayfs.x86_64 0:0.7.2-6.el7_8 fuse3-libs.x86_64 0:3.6.1-4.el7 slirp4netns.x86_64 0:0.4.3-4.el7_8
Complete!
# Step 4: 开启Docker服务
systemctl docker start
# 注意:
# 官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。例如官方并没有将测试版本的软件源置为可用,您可以通过以下方式开启。同理可以开启各种测试版本等。
# vim /etc/yum.repos.d/docker-ce.repo
# 将[docker-ce-test]下方的enabled=0修改为enabled=1
#
# 安装指定版本的Docker-CE:
# Step 1: 查找Docker-CE的版本:
# yum list docker-ce.x86_64 --showduplicates | sort -r
# Loading mirror speeds from cached hostfile
# Loaded plugins: branch, fastestmirror, langpacks
# docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
# docker-ce.x86_64 17.03.1.ce-1.el7.centos @docker-ce-stable
# docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
# Available Packages
# Step2: 安装指定版本的Docker-CE: (VERSION例如上面的17.03.0.ce.1-1.el7.centos)
# sudo yum -y install docker-ce-[VERSION]
yum -y install docker-ce-20.10.6
2.启动docker
systemctl start docker
3.安装镜像
例如:安装rabbitmq 其中加上registry.docker-cn.com/library可以加速下载
docker pull registry.docker-cn.com/library/rabbitmq: 3 management
配置阿里云镜像加速:
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://zerfqcke.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
4.下载镜像地址:
5.搜索镜像
例如搜:elasticsearch
docker search elasticsearch
6.查看所有镜像
docker images
7.删除镜像
docker rmi $(docker images -q)
docker rmi 镜像的名字
8.创建容器并启动
docker run
举例1:MySQL
docker run -p 3306:3306 --name latest_mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql
举例2:创建并启动redis容器
docker run --name redis -p 6379:6379 -v /mydata/redis/data:/data -v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf
9.查看运行中的容器
docker ps
10.查看所有容器(含未运行的)
dockers ps -a
11.启动容器
docker start
12.关闭容器
docker stop docker ps -aq --filter name=starapi
关闭某个容器:
docker stop 容器名字
13. 删除容器
docker rm $(docker ps -a -q)
docker rm 容器名字
14.进入容器中运行相关软件的指令
由于是docker进行安装,所以找不到执行mysql指令的命令,是放在容器中的。
正确执行命令应该是:
docker exec -it mysql01 /bin/bash #执行docker容器的mysql,这里容器名是mysql01
之后就可以进入MySQL容器,mysql也可以用了
mysql -u root -p #进入容器名为mysql01的mysql
退出:exit
如果安装的是精简版的redis的话,进入容器的命令 是docker exec -it mysql01 /binsh (把这个binsh改成sh)
15.查看日志
docker logs -f 8a5fca6e67b1
好博客就要一起分享哦!分享海报
此处可发布评论
评论(0)展开评论
展开评论
您可能感兴趣的博客