系列文章目录
前言
官网及下载
https://docker.com/
一、卸载旧版本docker
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
二、安装yum-utils常用工具包
[root@localhost ~]# yum -y install yum-utils
三、更换国内镜像源下载docker
1 更换
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
2 更新安装包索引
[root@localhost ~]# yum makecache fast
3 安装最新的docker
yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
4启动docker
[root@localhost ~]# systemctl start docker //启动docker服务
5 docker version 判断是否启动成功
三、启动hallo world测试
[root@localhost ~]# docker run hello-world
[root@localhost ~]# docker images //查看是否启动成功
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 7 months ago 13.3kB
四 卸载删除docker
卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
删除资源
rm -rf /var/lib/docker
五、阿里云加速
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://1edmtwcy.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
六、docker镜像常用命令
docker version // 显示docker版本信息
docker info //显示docker的系统信息,包括镜像和容量数量
docker --help //查看帮助文档
docker images //查看本地所有镜像
- docker search //搜索镜像
[root@localhost ~]# docker search mysql // 搜索镜像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12525 [OK]
mariadb MariaDB Server is a high performing open sou… 4817 [OK]
percona Percona Server is a fork of the MySQL relati… 575 [OK]
- –filter=stars=3000 //搜索条件 3000以上的镜像
[root@localhost ~]# docker search mysql --filter=stars=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12525 [OK]
mariadb MariaDB Server is a high performing open sou… 4817 [OK]
- docker pull //下载镜像
[root@localhost ~]# docker pull mysql
[root@localhost ~]# docker pull mysql //pull 下载mysql
Using default tag: latest //如果不写tag,默认是latest最新版
latest: Pulling from library/mysql
72a69066d2fe: Pull complete //分层下载
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete //下面是签名
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e77
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest //真实地址
- docker pull mysql:5.7 //指定版本下载
[root@localhost ~]# docker pull mysql:5.7 //指定下载版本
5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists //联合文件系统之前已经下载过
99da31dd6142: Already exists 的文件不用重复下载
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
- docker -rmi -f $(docker images -qa) //删除全部
[root@localhost ~]# docker images //查看本地镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 4 months ago 448MB
mysql latest 3218b38490ce 4 months ago 516MB
hello-world latest feb5d9fea6a5 7 months ago 13.3kB
[root@localhost ~]# docker rmi -f 3218b38490ce //指定id删除
Untagged: mysql:latest
Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f773
七、docker容器常用命令
docker run [可选参数] image
#参数说明
–name=“Name” //容器名称 tomcat01 tomcat02 用来区分容器
-d //后台方式运行
-it //使用交互方式运行,进入容器查看内容
-P //指定容器的端口 -p 8080:8080
-P ip:主机端口:容器端口
-P 主机端口:容器端口(常用)
-P 容器端口
-p //随机指定端口好(小写)
[root@localhost ~]# docker pull centos //下载最新版本centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@localhost ~]# docker images //查看本地镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 4 months ago 448MB
hello-world latest feb5d9fea6a5 7 months ago 13.3kB
centos latest 5d0da3dc9764 7 months ago 231MB
[root@localhost ~]# docker run -it centos /bin/bash //使用交互方式进入centos
[root@ea69cb4a177f /]# exit //直接停止并退出容器
-
ctrl + p + q 不停止退出容器
-
列出运行容器
[root@localhost ~]# docker ps //列出当前正在运行容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a //列出之前运行容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea69cb4a177f centos "/bin/bash" 11 minutes ago Exited (127) 16 seconds ago laughing_mclean
817c41ac6a0f hello-world "/hello" 4 hours ago Exited (0) 4 hours ago wonderful_babbage
[root@localhost ~]#
八、删除容器
docker rm [容器id] //指定删除容器
docker rm -f $(docker -ap) //删除所有容器
docker ps -a -q | xargs docker rm //删除所有容器
九、启动和停止容器
docker start 容器id //开启容器
docker restart 重启容器id //重启容器
docker stop 容器id //停止容器
docker kill 容器id //强制停止容器