Docker安装使用
安装教程:https://www.runoob.com/docker/ubuntu-docker-install.html
官方教程:https://docs.docker.com/engine/install/ubuntu/
本文记录本人安装docker的相关流程笔记(ubuntu)
一、 docker安装
卸载并删除原有docker
sudo apt-get remove docker docker-engine docker.io containerd runc
rm -rf var/lib/docker
安装相关依赖包:
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release
(1) 官网方法下载gpg
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
(2) 其他方式下载gpg密钥(选一个安装就行):
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -//中科大的
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - //阿里云的
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - //官方的
展示 Docker 的官方 GPG 密钥:
sudo apt-key fingerprint 0EBFCD88
展示apt-key
sudo apt-key list
删除apt-key
aptkey apt-key del 0EBFCD88
添加Docker远程仓库
sudo add-apt-repository "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
(1) 安装最新版本的 Docker Engine-Community 和 containerd
(安装核心、客户端、docker 容器 一般用ce,docker-ee 是企业授权版本的 一般不用)
sudo apt-get install docker-ce docker-ce-cli containerd.io
(2) 安装最新版本的 Docker Engine-Community 和 containerd
查看各个版本 Docker:
apt-cache madison docker-ce
选择一个版本进行安装
sudo apt-get install docker-ce=5:20.10.9~3-0~ubuntu-focal docker-ce-cli=5:20.10.9~3-0~ubuntu-focal containerd.io
docker 安装完毕了!!
二、 启动docker服务端
sudo service docker start
systemctl start docker
查看docker 版本
docker version
启动测试:docker hello world
sudo docker run hello-world
docker的运行流程:
1、从本地寻找hello-world镜像
2、没有的话就从远程仓库pull到本地
3、之后就运行了
三、使用阿里加速器:
阿里云网址:https://cr.console.aliyun.com/,自己在阿里云申请账号,并找到镜像加速器(免费的,超赞!),后面针对不同的环境配置。
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://XXXXXXX.mirror.aliyuncs.com"] //换成自己的仓库地址
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
四、docker基本命令
查看版本
docker version
查看明细信息
docker info
docker显示的明细信息
五、镜像命令
查看镜像 sudo docker images
dangs@dangs-YangTianM4000e-17:/etc/docker$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 5 weeks ago 13.3kB
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE_ID 镜像的ID
CREATE镜像的创建时间
SIZE 镜像的大小
-a --all 列出所有的镜像
-q --quiet 显示镜像的idxxxxxxxxxx
搜索镜像
docker search mysql --filter=STARS=3000 //可以找到3000star以上的
拉取镜像 以mysql为例
docker pull mysql
dangs@dangs-YangTianM4000e-17:/etc/docker$ sudo docker pull mysql (:5.7 加上这个就不是最新版本了) 联合文件系统
Using default tag: latest 后面不加tag就默认的是最新版本
latest: Pulling from library/mysql
b380bbd43752: Pull complete //分段下载
f23cbf2ecc5d: Pull complete
30cfc6c29c0a: Pull complete
b38609286cbe: Pull complete
8211d9e66cd6: Pull complete
2313f9eeca4a: Pull complete
7eb487d00da0: Pull complete
4d7421c8152e: Pull complete
77f3d8811a28: Pull complete
cce755338cba: Pull complete
69b753046b9f: Pull complete
b2e64b0ab53c: Pull complete
Digest: sha256:6d7d4524463fe6e2b893ffc2b89543c81dec7ef82fb2020a1b27606666464d87
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest 最新的docker
//两者等价
docker pull mysql
docker pull docker.io/library/mysql:latest
如果下载5.7 有一部分已经存在了
dangs@dangs-YangTianM4000e-17:/etc/docker$ sudo docker pull mysql:5.7
5.7: Pulling from library/mysql
b380bbd43752: Already exists
f23cbf2ecc5d: Already exists
30cfc6c29c0a: Already exists
b38609286cbe: Already exists
8211d9e66cd6: Already exists
2313f9eeca4a: Already exists
7eb487d00da0: Already exists
a71aacf913e7: Pull complete
393153c555df: Pull complete
06628e2290d7: Pull complete
ff2ab8dac9ac: Pull complete
Digest: sha256:2db8bfd2656b51ded5d938abcded8d32ec6181a9eae8dfc7ddf87a656ef97e97
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
删除镜像
docker rmi -f [容器id] 删除镜像
docker rmi -f $ (docker images -aq) 删除全部的镜像
六、容器命令
说明:我们有了镜像才可以创建容器,linux
sudo docker pull ubuntu //套娃ubuntu
sudo docker run -it ubuntu /bin/bash
docker启动命令
docker run [可选] image
#参数说明
--name="Name" 容器名字 tomcat01 tomcat02 区分容器
-d 后台方式运行 类似nohup
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080(小p是指定端口)
-p 主机端口:容器端口(常用)
-p ip:主机端口:容器端口
-p 容器端口
容器端口
-P 随机指定端口(大P是随机指定端口)
退出docker
exit
展示当前运行的docker
docker ps -a
-a 附带历史运行的docker
-n=1 只显示一个docker记录
dangs@dangs-YangTianM4000e-17:/var/cache$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2fe47605af02 ubuntu "/bin/bash" 3 minutes ago Exited (127) 8 seconds ago bold_lamport
c13a5b86d692 hello-world "/hello" 27 hours ago Exited (0) 27 hours ago modest_elion
f9cc958dee9a hello-world "/hello" 7 days ago Exited (0) 7 days ago interesting_elbakyan
3b502643b938 hello-world "/hello" 7 days ago Exited (0) 7 days ago vigorous_hugle
2a5bdc5e6e2d hello-world "/hello" 7 days ago Exited (0) 7 days ago affectionate_neumann