一、Docker安装
1 卸载原有的Docker
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2 安装工具包
yum install -y yum-utils
3 配置镜像仓库
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
4 更新yum软件包索引
yum makecache fast
5 安装Docker引擎 ce:社区版 ee:企业版
yum install docker-ce docker-ce-cli containerd.io
6 启动docker服务
systemctl start docker
docker version
7 Docker hello-word
docker run hello-world
8 查看docker镜像
[ root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 13 months ago 13.3kB
9 了解:卸载Docker
yum remove docker-ce docker-ce-cli containerd.io
rm -rf /var/lib/docker
二、Docker常用命令
1 帮助命令
docker version
docker info
docker 命令 --help
帮助文档的地址:https://docs.docker.com/engine/reference/
2 镜像命令
2.1 docker images:查看所有本地主机上的镜像
[ root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 13 months ago 13.3kB
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
-a, --all
-q, --quiet
2.2 docker search:搜索镜像
[ root@localhost ~]
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10455 [ OK]
mariadb MariaDB is a community-developed fork of MyS… 3886 [ OK]
--filter= STARS= 3000
2.3 docker pull:下载镜像
[ root@localhost ~]
Using default tag: latest
latest: Pulling from library/mysql
a076a628af6f: Pull complete
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
1a8c919e89bf: Pull complete
9f3cf4bd1a07: Pull complete
80539cea118d: Pull complete
201b3cad54ce: Pull complete
944ba37e1c06: Pull complete
Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
等价于
[ root@localhost ~]
5.7: Pulling from library/mysql
a076a628af6f: Already exists
f6c208f3f991: Already exists
88a9455a9165: Already exists
406c9b8427c6: Already exists
7c88599c0b25: Already exists
25b5c6debdaf: Already exists
43a5816f1617: Already exists
1831ac1245f4: Pull complete
37677b8c1f79: Pull complete
27e4ac3b0f6e: Pull complete
7227baa8c445: Pull complete
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
2.4 docker rmi:删除镜像
[ root@localhost ~]
[ root@localhost ~]
[ root@localhost ~]
3 容器命令
docker pull centos
3.1 docker run:新建容器并启动
docker run [ 可选参数] image
--name= "Name" 容器名称
-d 后台方式运行
-it 使用交互方式运行,进入容器内查看
-p 指定容器的端口
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P 随机指定容器的端口
[ root@localhost ~]
[ root@271e0738fb67 /]
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[ root@271e0738fb67 /]
exit
[ root@localhost ~]
Ctrl + P + Q
3.2 docker ps:列出所有运行的容器
docker ps
-a
-n= ?
-q
3.3 docker rm:删除容器
docker rm 容器id
-f
docker rm -f $( docker ps -aq)
docker ps -a -q| xargs docker rm
3.4 启动和停止容器
docker start 容器id
docker restart 容器id
docker stop 容器id
docker kill 容器id
3.5 其他常用命令
常见的坑:docker容器使用后台运行,就必须要有前台进程,docker发现没有应用,就会自动停止
docker logs 容器
-tf
--tail number
docker top 容器id
docker inspect 容器id
docker exec -it 容器id bash/shell
docker attach 容器id bash/shell
docker cp 容器id:容器内文件路径 宿主机路径