docker基础篇

一、docker的组成

1、镜像(安装包)

镜像就是一个只读的模板,镜像可以用来创建docker容器、一个镜像可以创建很多容器

2、容器(运行安装包的实例)

容器是用镜像创建的运行实例、可以被启动、开启、停止、删除,每个容器都是互相隔离的

3、仓库(存放安装包的地方)

集中存放镜像的地方
仓库分为公开仓库和私有仓库,一般国内使用的公开仓库为阿里云、网易云

二、docker下载

docker官网下载
https://docs.docker.com/engine/install/centos/   

docker hub 安装docker镜像仓库
https://hub.docker.com/

三、docker安装

CentOS docker安装

前提条件:
dokcer依赖已存在并运行的linux内核环境,所以docker必须部署在linux内核的系统上
linux内核版本为3.8以上、发行版本为centos7(64bit)

查看linux 内核版本信息命令
[root@stc-tst02 ~]# uname -r 
3.10.0-862.el7.x86_64

查看linux发行版本命令
[root@stc-tst02 ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 

安装步骤
1、yum安装gcc
yum -y install gcc
yum -y install gcc-c++

2、yum安装yum-utils
yum install -y yum-utils

3、设置stable镜像仓库(使用国内的)
yum-config-manager  --add-repo  http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4、更新yum索引(加快镜像拉取速度)
yum makecache fast

5、安装docker引擎(初次安装大约10分钟)
yum install -y docker-ce docker-ce-cli containerd.io 

6、启动docker
systemctl start docker

查看docker是否启动
[root@stc-tst02 yum.repos.d]# ps -ef |grep docker
root      6912     1  3 12:35 ?        00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root      7137  3921  0 12:36 pts/0    00:00:00 grep --color=auto docker

查看docker版本
[root@stc-tst02 yum.repos.d]# docker version 
Client: Docker Engine - Community
 Version:           20.10.16
 API version:       1.41
 Go version:        go1.17.10
 Git commit:        aa7e414
 Built:             Thu May 12 09:19:45 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.16
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.10
  Git commit:       f756502
  Built:            Thu May 12 09:18:08 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.4
  GitCommit:        212e8b6fa2f44b9c21b2798135fc6fb7c53efc16
 runc:
  Version:          1.1.1
  GitCommit:        v1.1.1-0-g52de29d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

7、测试docker
运行hello-world 镜像
[root@stc-tst02 ~]# docker run hello-world 
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Status: Downloaded newer image for hello-world:latest

Hello from Docker!

8、配置阿里云镜像加速


image.png

四、常用命令


1、启停、帮助命令
启动:systemctl start docker
停止:systemctl stop docker
重启:systemctl restart docker
查看状态:systemctl status docker
开启启动:systemctl enable docker
查看docker概要信息:docker info
查看帮助文档:docker --help
查看命令帮助:docker “具体命令” --help

2、镜像命令
docker images
-a 列出所有镜像(包括历史镜像)
-q 只显示镜像ID

#查找redis镜像
docker search redis 
#查找redis镜像,并只列出前5个
docker search  --limit 5 redis

docker pull  [tag]
#拉取redis镜像到本地,不加tag则拉去最新版本的redis
docker pull redis

#查看镜像、容器、数据卷占用的空间
docker system df

#删除镜像
docker rmi -f  镜像id

3、容器命令
#启动一个交互式的容器
docker run -it --name=myub ubuntu /bin/bash
run:运行镜像
i:交互式的
t:终端
--name:指定容器名字
/bin/bash:开启一个交互式的shell

#启动容器,并且让这个容器后台运行
docker run -d redis

#查看容器日志
docker logs 容器ID

#查看容器进程
docker top 容器ID

#查看正在运行容器
docker ps
参数
-n:显示最近n个创建的容器
-q:   静默模式,只显示容器编号
-a:列出当前运行和历史运行的容器
-l: 显示最近创建的容器

#退出容器
exit:run 进去的容器,执行此命令,退出容器,并停止容器
ctrl+p+q:run进去的容器,执行此命令,会退出容器

#启动、重启、停止删除容器
docker start      容器 ID/名字
docker restart
docker stop
docker rm

#查看容器内部详细信息
docker inspect 容器 ID

#退出容器后,重新进入容器
docker  exec -it  容器 ID

#将docker容器内的文件复制到宿主机
语法:docker cp  容器id:文件路径  宿主机文件路径

样例
[root@stc-tst02 ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED      STATUS      PORTS      NAMES
f2b6f5792177   redis     "docker-entrypoint.s…"   9 days ago   Up 9 days   6379/tcp   heuristic_cartwright
0299526298b9   ubuntu    "/bin/bash"              9 days ago   Up 9 days              myu
[root@stc-tst02 ~]# docker cp 0299526298b9:/root/test /root/

容器导出(备份)
#将容器导出为一个tar包文件
docker export  容器iD > 包名.tar (默认导出到当前路径)
docker export 0299526298b9 > tao.tar 

容器导入(恢复)
cat  包名.tar  | docker import    - 镜像用户/镜像名称:镜像版本号
cat tao.tar | docker import  - tao/ubuntu1:3.7

新建一个容器并提交,生成一个新的镜像
#运行一个ubuntu容器实例
#在ubuntu安装vim
#将带有vim的ubuntu,生成一个新的镜像
#进入ubuntu镜像
docker exec -it c3fa44618d7a /bin/bash
#更新安装包
apt-get update
#安全vim
apt-get -y install vim
#提交带有vim的容器,生成一个新的镜像
docker commit -m="vim add ok"  -a="lt" c3fa44618d7a   tao/myubuntu:1.1
-m:注释
-a:作者
c3fa44618d7a :容器id
tao/myubuntu:1.1:镜像名:标签版本

五、私有镜像仓库

#拉取仓库镜像
docker pull registry
#运行镜像
docker run -d  -p 5000:5000  -v /home/taoge/myregistry:/tmp/registry --privileged=true registry
#案例:启动一个最小版本的ubuntu并安装tree命令,然后提交成一个新的镜像。最后把带有tree命令的镜像提交到私有仓库
1、运行ubuntu镜像
docker run -it ubuntu /bin/bash

2、更新安装包
apt-get update

3、下载tree命令
apt-get -y install tree 

4、执行tree命令
tree

5、在宿主机上执行提交命令,生成新的镜像
docker commit -m="tree add ok" -a="tao" aa3f3f9eb46e ubuntu_tree:1.2

6、查看生成的镜像
[root@stc-tst02 home]# docker images 
REPOSITORY     TAG       IMAGE ID       CREATED             SIZE
ubuntu_tree    1.2       f270da53f5ec   24 seconds ago      109MB

7、修改镜像tag,使其符合私有仓库的命名规范
docker tag ubuntu_tree:1.2 172.31.186.xx:5000/ubuntu_tree:1.2
[root@stc-tst02 home]# docker images 
REPOSITORY                       TAG       IMAGE ID       CREATED             SIZE
172.31.186.xx:5000/ubuntu_tree   1.2       f270da53f5ec   6 minutes ago       109MB

8、私有仓库是支持https,修改文件/etc/docker/daemon.json,将仓库的地址添加进去
"insecure-registries": ["ip:port"]

Snipaste_2022-06-04_16-55-43.png
9、查看私有仓库中有哪些镜像
[root@stc-tst02 home]# curl -XGET http://172.31.186.xx:5000/v2/_catalog
{"repositories":[]}(空的)

10、将镜像推送到私有库
docker push 172.31.186.xx:5000/ubunty_tree:1.2

11、查看私有仓库中的镜像
curl -XGET http://172.31.186.xx:5000/v2/_catalog
{"repositories":["ubunty_tree"]}

12、从私有仓库拉取镜像到本地
docker pull 172.31.186.xx:5000/ubunty_tree:1.2
[root@stc-tst02 home]# docker images 
REPOSITORY                       TAG       IMAGE ID       CREATED          SIZE
172.31.186.xx:5000/ubunty_tree   1.2       df7e5ade7c61   29 minutes ago   109MB

六、docker容器卷

作用:使容器内的数据持久化  

特点:容器和宿主机之间的数据可以互通,容器卷挂载成功后,即使容器停止了,当宿主机在挂载的目录写入数据时,容器启动后依然可以同步到所有的数据(类似NFS)

语法:docker run -it --privileged=true -v /tmp/host_data:/tmp/docker_data --name="ubuntu1" ubuntu

参数:
--privileged=true :保证用户有root权限
/tmp/host_data:宿主机目录
/tmp/docker_data:docker目录

查看容器卷是否挂载成功
docker inspect 容器id

      "Mounts": [
            {
                "Type": "bind",
                "Source": "/tmp/host_data",
                "Destination": "/tmp/docker_data",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
权限控制
#限制容器内数据只能读取,在目录后面加入ro
docker run -it --privileged=true -v /tmp/host_data1:/tmp/docker_data1:ro ubuntu /bin/bash
#进入容器的/tmp/docker_data1,创建文件
touch test.txt
touch: cannot touch 'test.txt': Read-only file system

容器卷的继承
当一个容器卷挂载完毕后,其他的容器也可以继承这个容器卷挂载的规则
#运行容器取名为u1,并挂载一个容器卷
docker run  -it  --name="u1" -v /tmp/host_u1:/tmp/docker_u1  --privileged=true ubuntu /bin/bash

#运行容器取名为u2,并继承u1的容器卷
docker run  -it  --name="u2" --volumes-from u1   --privileged=true ubuntu /bin/bash

当u1容器停止,u2容器的容器卷挂载规则依然生效
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容