Docker基础

Docker安装

#下载docker仓库
wget https://download.docker.com/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
#安装docker引擎
yum install -y docker-ce docker-ce-cli containerd.io
#启动并设置开机自启
systemctl start docker && systemctl enable docker

#验证是否安装正确
docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
#表示成功

#卸载
yum remove docker-ce docker-ce-cli containerd.io
#删除所有镜像、容器和卷
rm -rf /var/lib/docker

镜像加速器

可前往阿里云-->登录-->搜索‘容器镜像服务’-->左侧镜像中心-->镜像极速器

容器操作

#docker子命令补全
yum install -y bash-completion
//安装完成后重新连接系统即可

输入命令后双击Tab

#运行容器
docker run -d -it  --name 容器名称 镜像名称[:版本号]
	-d :后台运行
	-i :分配一个伪TTY,以便和容器进行交互
	-t :持续和 容器交互,防止断开
	-name : 为容器命名
	--rm  :阅后即焚
例>> docker run -d -it --name centos centos:7

#进入/退出容器
docker exec -it 容器id  bash
exit (退出)

#文件(目录)的拷贝与挂载
docker cp 本地文件(路径) 容器id:容器路径
	//将本地文件拷贝到容器指定路径
例>> docker cp /root/a.txt  bu68:/

docker run  -it -v 本地文件:容器文件 镜像名称[:版本号]
	-v :本地文件的绝对路径
	//挂载本地文件与容器文件,目录也一样(已存在容器无法挂载)>> docker run -it -v /www/html:/var/www/html nginx

#端口映射
docker -p 本地端口:容器端口  镜像名称[:版本]
	-p :映射端口
例>> docker -p 8000:80 nginx

#启动/停止/重启容器
docker start 容器id或容器名称
docker stop  容器id
docker restart 容器id

#查看容器
docker ps -a  :查看所有容器,包括未开启的
docker ps -q  :查看所有容器id
docker ps  :查看已开启容器
#查看容器日志
docker logs -f 容器id
	-f :动态查看

#删除容器(先停止后删除)
docker rm 容器id(容器名称)
docker rm $(docker ps -qa)
	//删除全部容器

镜像操作

#拉取到本地(下载)
docker pull 镜像名称[:版本号]>> docker pull centos:7
#搜索镜像
docker search 镜像名称

#查看全部本地镜像
docker images    或   docker image ls
#删除本地镜像
docker rmi 镜像名称或ID
	//先删容器后删镜像

#将本地镜像导出
docker save -o 导出路径 名称及版本或镜像id
	若只写名称不写版本则把相同名称镜像全部导入
#加载本地镜像
docker load -i 镜像文件
#修改镜像名称
docker tag 镜像ID  新的镜像名
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值