docker离线安装及配置

第一步:下载docker离线包
https://download.docker.com/linux/static/stable/x86_64/docker-20.10.6.tgz

第二步:下载离线安装工具
https://github.com/Jrohy/docker-install/

第三步:在linux环境下,创建/root/setup/docker目录
mkdir setup
cd setup/
mkdir docker

第四步:将下载好的资源放在一个目录

上传docker-20.10.6.tgz,docker-install-master.zip到 /root/setup/docker

第五步:执行安装操作

cd /root/setup/docker

unzip docker-install-master.zip

cd docker-install-master/

cp install.sh /root/setup/docker/

cd ..

chmod +x install.sh

./install.sh -f docker-20.10.6.tgz

第六步:安装成功以后,检查安装状态
docker info

查看docker版本
docker -v

Docker服务基本操作

1.启动docker服务
systemctl start docker

查看Docker状态
systemctl status docker

设置Docker开机自启
systemctl disable docker

禁用Docker开机自启
systemctl disable docker

重新启动Docker服务
systemctl restart docker

查看Docker信息
docker info

查看docker info中具体key的信息,例如:
docker info | grep 'Docker Root Dir:'

停止docker服务
systemctl stop docker

Docker镜像加速
修改配置文件 /etc/docker/daemon.json

cat < /etc/docker/daemon.json
{
  "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn",
    "http://hub-mirror.c.163.com"
  ],
  "max-concurrent-downloads": 10,
  "log-driver": "json-file",
  "log-level": "warn",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
    },
  "data-root": "/var/lib/docker"
}
EOF

Docker镜像操作实践

下载镜像
官网:https://hub.docker.com/

下载镜像  docker pull 镜像名:版本号
docker pull hello-world
docker pull centos:7
docker pull java:8

浏览镜像文件(相当于ls,ll)
docker images

查看镜像详情 docker inspect 镜像名或镜像id
docker inspect hello-world

查看镜像历史(docker history )
docker history hello-world

导出镜像文件
docker save  hello-world | gzip > hello-world.tar.gz  

删除镜像文件 语法:docker image rm 镜像名或镜像id
docker image rm hello-world


查看所有容器
docker ps -a

删除指定容器
docker container rm 容器id前3位即可

导入镜像操作(要在hello-world.tar.gz 文件所在目录下执行)及镜像目录导入
docker load < hello-world.tar.gz  

运行镜像文件
docker run hello-world

创建并启动容器(Container)
docker run -it xxxx bash


其中:
1)xxxx - 镜像名, 或 image id 的前几位,
2)-it 这是两个参数(-i表示交互式操作, -t 表示终端)
3) bash 表示进入操作终端,基于交互式进行相关操作(例如执行linux相关指令)
案例:通过docker启动运行 centos7镜像  //进入镜像的centos(可以理解为搭建的虚拟机)
docker run -it centos:7 bash
----(后台一直运行centos)
docker run -dit centos:7 bash

查看Docker中的容器(要在宿主机执行docker指令)
docker ps

从容器中退出(exit)
假如从宿主机进入了启动的容器,退出容器需要使用exit指令
exit

查看容器日志(logs)信息(假如容器没有启动,要通过此指令去看一下错误日志)
docker logs 802  #802为自己的容器id(一般写前三位即可)

停止(stop)或重启(Restart)容器
docker stop 802  #802为容器自己的id
重新启动容器,代码如下:
docker restart 802 #802位容器自己的id

进入(exec)指定容器(Container)
docker exec -it 802 bash #802为容器id

从容器(Container)中退出(exit)
exit

删除(rm)容器(Container)  (假如容器不用了,可执行删除操作,例如:)
docker  rm 802 #802为容器id

如果删除运行中的容器,需要添加 -f 参数执行强制删除,例如:
docker container rm -f 802 #802为容器id

清理所有处于终止状态容器,例如:
docker container prune
执行完这个指令以后,可以通过docker ps -a 再查看容器,看看是否有删除.

Docker数据管理实践

在容器中管理数据主要有两种方式:
数据卷(Volumes)
挂载主机目录 (Bind mounts)

-----数据卷操作
第一步:创建数据卷
docker volume create container-vol

第二步:查看所有数据卷
docker volume ls

查看指定 数据卷 的信息
docker volume inspect container-vol

第三步:启动挂载数据卷的容器
docker run -it --mount source=container-vol,target=/root centos:7 bash
docker run -dit --mount source=container-vol,target=/root centos:7 bash
或者采用如下简写方式
docker run -it -v container-vol:/root centos:7 bash
docker run -dit -v container-vol:/root centos:7 bash

-v container-vol:/root 把数据卷 container-vol 挂载到容器的 /root 目录

容器是基于镜像创建的,执行镜像生成容器,方可进入容器
启动容器命令: docker run <相关参数> <镜像 ID> <初始命令>
docker run -i -t -v /root/software/:/mnt/software/ 9f38484d220f /bin/bash

docker run -i -t -v /root/java/:/root/javafiles/ eeb6ee3f44bd /bin/bash

参数解析
  -i:表示以“交互模式”运行容器
  -t:表示容器启动后会进入其命令行
  -v:表示需要将本地哪个目录挂载到容器中,格式:-v <宿主机目录>:<容器目录>
  /bin/bash:一旦容器启动,需要执行的命令,当前使用 "/bin/bash", 表示启动后直接进bash shell
  /root/software是宿主机器(Linux)上创建的一个文件夹;
  /mnt/software是centos的容器里面的目录文件
  这里挂载的意思就是 9f38484d220f 创建的容器访问 /mnt/software/ 目录下的文件就相当于访问 宿主机的 /root/software/下的文件,且两者文件夹里内容相

第四步:删除数据卷(如果数据卷被容器使用则无法删除)
docker volume rm container-vol

清理无主数据卷
docker volume prune

挂载主机目录
我们还可以在启动容器时,以目录直接挂载的方式进行数据操作
docker run -it -v /usr/app:/opt/app centos:7 bash

其中:
1)/usr/app:为宿主机目录
2)/opt/app: 为启动容器的一个目录
3)-v 用于指定挂载目录,如果本地目录(宿主机目录)不存在, Docker 会自动为你按照挂载目录进行目录的创建

查看挂载目录信息
docker inspect 91a #91a 为容器id

你可能感兴趣的:(docker,容器,linux)