个人笔记
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
3.6 KiB

3 years ago
# 安装(centos)
- 卸载旧版本
```shell
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
```
- 安装依赖
```shell
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
```
- 设置仓库(非必须)
官方源地址(比较慢)
```shell
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
```
阿里云
```shell
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
```
清华大学源
```shell
yum-config-manager \
--add-repo \
https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
```
- 安装**docker**
安装最新版本
```shell
yum install docker-ce docker-ce-cli containerd.io
```
安装指定版本
```shell
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
```
启动**docker**
```shell
systemctl start docker
```
设置开机启动
```shell
systemctl enable docker
```
停止**docker**
```
systemctl stop docker
```
- 卸载**docker**
删除安装包
```shell
yum remove docker-ce
```
删除镜像、容器、配置文件等内容,默认路径为:/var/lib/docker,自定义路径为:/etc/docker/daemon.json中data-root属性
# 使用
- 镜像
获取最新版本镜像
```shell
docker pull <镜像名称>
docker pull <镜像名称>:latest
```
获取指定版本
```shell
docker pull <镜像名称>:<版本号>
```
删除镜像
```shell
docker rmi <镜像ID>
```
- 创建与启动容器
创建新容器并启动
```shell
docker run -itd --<容器名> <镜像名> <容器内执行的命令>
```
参数说明
```tex
-i: 以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
-d: 后台运行容器,并返回容器ID;
-p: 指定端口映射,格式为:主机(宿主)端口:容器端口
-v: 挂载宿主机的一个目录到容器,格式为:主机(宿主)目录:容器目录;
-e: username="ritchie": 设置环境变量;
-m: :设置容器使用内存最大值;
--privileged 容器内root用户是否真正拥有root权限
--restart :容器退出是重启策略
no默认值,表示容器退出时,docker不自动重启容器
on-failure 若容器的退出状态非0,则docker自动重启容器,还可以指定重启次数,若超过指定次数未能启动容器则放弃
always 容器退出时总是重启
unless-stopped 容器退出时总是重启,但不考虑Docker守护进程启动时就已经停止的容器
如果容器启动时没有设置–restart参数,则通过下面命令进行更新:
docker update --restart=always <容器ID>
```
停止容器
```shell
docker stop <容器ID>
```
启动容器
```shell
docker start <容器ID>
```
- 进入退出
查看所有容器
```shell
docker ps -a
```
进入容器,**attach**或**exec**(推荐使用,退出容器终端容器不会停止)
```shell
docker exec -it <容器ID> /bin/bash
```
进入容器后输入**exit** 退出容器
- 删除与导入导出
删除容器
```shell
docker rm -f <容器ID>
```
导出容器
```shell
docker export <容器ID> > <文件名>
```
导入容器
```shell
cat <文件名> | docker import - <镜像名>
```