Brc's blog
首页
前端
后端
运维
  • 工作笔记
  • 分类
  • 标签
  • 归档
关于

Brc

努力中
首页
前端
后端
运维
  • 工作笔记
  • 分类
  • 标签
  • 归档
关于
  • Linux基础

  • Linux进阶

    • shell

    • nginx

    • keepalived

    • LVS

    • ansible

    • docker

      • docker
      • docker入门
        • 软件部署
          • 基础知识
          • 加速器实践
        • 镜像管理
        • 容器管理
        • 数据管理
          • 数据卷实践
          • 数据卷容器实践
        • 网络管理
          • 默认随机映射
          • 指定端口映射
          • 多端口映射
        • 网络模型
          • 命令语法
        • 仓库管理
      • Dockerfile
    • mysql

  • 其他

  • 运维
  • Linux进阶
  • docker
Brc
2022-04-05
目录

docker入门

# 软件部署

# 基础知识

源配置

# 使用阿里的源
# step 1: 安装必要的一些系统工具
root@docker:~# apt-get update
root@docker:~# apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
root@docker:~# curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
root@docker:~# add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
1
2
3
4
5
6
7
8

查找Docker-CE的版本

root@docker:~# apt-cache madison docker-ce
 docker-ce | 5:20.10.14~3-0~ubuntu-focal | https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
 docker-ce | 5:20.10.13~3-0~ubuntu-focal | https://mirrors.aliyun.com/docker-ce/linux/ubuntu focal/stable amd64 Packages
1
2
3

更新并安装Docker-CE

root@docker:~# apt-get -y update
root@docker:~# apt-get -y install docker-ce
1
2

安装指定版本的Docker-CE

# sudo apt-get -y install docker-ce=[VERSION]
1

aws的ec2安装docker

amazon-linux-extras install docker
1

检查服务

systemctl [start|stop|restart|status] docker
docker version|info
1
2

安装校验

root@docker:~# docker version
Client: Docker Engine - Community
 Version:           20.10.14
 API version:       1.41
...
Server: Docker Engine - Community
 Engine:
  Version:          20.10.14
  API version:      1.41 (minimum version 1.12)
...

root@docker:~# docker info
...
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

测试效果

image-20220405161646837

核心目录

软件数据目录 /var/lib/docker/
专属服务目录 /etc/docker
1
2

# 加速器实践

国内使用docker的官方镜像源,会因为网络的原因,造成无法下载,或者一直处于超时。我们可以使用很多国内的公共源仓库,比如,我们这里采用阿里云的方法进行加速配置。

root@docker:~# tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://3lhpcuov.mirror.aliyuncs.com"],
  "insecure-registries":[]
}
EOF
root@docker:~# systemctl daemon-reload
root@docker:~# systemctl restart docker
1
2
3
4
5
6
7
8

可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器。

# 镜像管理

​ 镜像是一个只读的文件,就类似于我们安装操作系统时候所需要的那个iso光盘镜像,通过运行这个镜像来完成各种应用的部署。 ​ 这里的镜像就是一个能被docker运行起来的一个程序。

镜像相关命令

root@docker:~# docker help | grep image
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  trust       Manage trust on Docker images
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  load        Load an image from a tar archive or STDIN
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rmi         Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

搜索

docker search [image_name]
1

获取

docker pull [image_name]
1

查看

docker images <image_name>
1

历史

docker history [image_name]
1

标签

docker tag [old_image]:[old_version] [new_image]:[new_version]
1

删除

docker rmi [image_id/image_name:image_version]
1

导出

docker save -o [包文件] [镜像]
1

导入

docker load < [image.tar_name]
1

# 容器管理

容器就类似于我们运行起来的一个操作系统,而且这个操作系统启动了某些服务。

这里的容器指的是运行起来的一个Docker镜像。

容器相关命令

root@docker:~# docker help | grep container
A self-sufficient runtime for containers
  container   Manage containers
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

查看

查看所有:
	docker ps -a
查看部分:
	docker ps --filter status=exited -q
1
2
3
4

启动

docker run -itd <image_name>
常见参数:
  -e, --env list                       Set environment variables
      --env-file list                  Read in a file of environment variables
  -i, --interactive                    Keep STDIN open even if not attached
  -t, --tty                            Allocate a pseudo-TTY
  -d, --detach                         Run container in background and print
                                       container ID
      --name string                    Assign a name to the container
  -h, --hostname string                Container host name
      --rm                             Automatically remove the container when
      --restart string                 Restart policy to apply when a container
                                       exits (default "no")
1
2
3
4
5
6
7
8
9
10
11
12
13

启动

docker start [container_id]
1

关闭

docker stop [container_id]
1

删除

docker rm [-f] [container_id]
技巧:
	docker rm -f $(docker ps -a -q)
1
2
3

进入

docker exec -it [container_id] /bin/bash
属性解析:
	-i:让容器的标准输入保持打开
	-t:让docker分配一个伪终端,并绑定到容器的标准输入上
	/bin/bash:执行一个命令
1
2
3
4
5

退出容器

方法一:exit
方法二:Ctrl + D
1
2

提交

docker commit -m '改动信息' -a '作者信息' [container_id] [new_image:tag]
1

日志

docker log [container_id]
1

属性

查看所有属性
	docker inspect [container_id]
格式化输出属性:
	专用格式: docker inspect -f '{{json .NetworkSettings.Ports}}' [container_id]
	定制格式: docker inspect -f 'Hello from container {{.Name}}' [container_id]
高阶格式信息输出:
	docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' [container_id]

属性解析:
	{{}}表示模板指令,里面存放属性的名称
	“.”表示当前上下文,表示容器元数据的整个数据结构,两个属性间的.代表上下级
	range用于遍历结构内返回值的所有数据
1
2
3
4
5
6
7
8
9
10
11
12

# 数据管理

​ docker的镜像是只读的,虽然依据镜像创建的容器可以进行操作,但是我们不能将数据保存到容器中,因为容器会随时关闭和开启,可以使用数据卷和数据卷容器将数据保存下来。

  • 数据卷:就是将宿主机的某个目录,映射到容器中,作为数据存储的目录,我们就可以在宿主机对数据进行存储缺点是:太单一了
  • 数据卷容器:将宿主机的某个目录,使用容器的方式来表示,然后其他的应用容器将数据保存在这个容器中,达到大批量应用数据同时存储的目的

命令解析

docker run --help | grep volume
  -v, --volume list                    Bind mount a volume
      --volume-driver string           Optional volume driver for the container
      --volumes-from list              Mount volumes from the specified container(s)
1
2
3
4

数据卷操作

docker run -itd --name [容器名字] -v [宿主机文件]:[容器文件] [镜像名称]
注意:
	-v 宿主机文件:容器文件 可以存在多个,表示同时挂载多个
	宿主机问文件尽量用绝对路径,容器文件即使不存在,Docker自动创建
1
2
3
4

数据卷容器操作

1.创建数据卷容器
	docker create -v [宿主机文件]:[容器数据卷目录] --name [容器名字] [镜像名称] [命令(可选)]
2.使用数据卷容器
	docker run --volumes-from [数据卷容器名字] -d --name [容器名字] [镜像名称] [命令(可选)]
1
2
3
4

# 数据卷实践

目录文件

# 创建测试文件
root@docker:~# echo "file1" > /tmp/file1.txt

# 启动一个容器,挂载数据卷
root@docker:~# docker run -itd --name test1 -v /tmp:/test1 nginx

# 测试效果
root@docker:~# docker exec -it 4db821b57b5e /bin/bash
root@4db821b57b5e:/# cat /test1/file1.txt
file1
1
2
3
4
5
6
7
8
9
10

普通文件

# 创建测试文件
root@docker:~# echo "file1" > /tmp/file1.txt
# 启动一个容器,挂载数据卷
docker run -itd --name test2 -v /tmp/file1.txt:/nihao/nihao.sh nginx
# 测试效果
root@docker:~# docker exec -it b3a2276ccf67 /bin/bash
root@b3a2276ccf67:/# cat /nihao/nihao.sh
file1
1
2
3
4
5
6
7
8

# 数据卷容器实践

创建数据卷

root@docker:~# docker create -v /data --name v-test nginx
1

挂载数据卷

# 创建vc-test1容器
root@docker:~# docker run -itd --volumes-from b99e1a2fb7be -d --name vc-test1 nginx

# 创建vc-test2容器
root@docker:~# docker run -itd --volumes-from b99e1a2fb7be -d --name vc-test2 nginx
1
2
3
4
5

确认效果

# 进入vc-test1,操作数据卷容器
root@docker:~# docker exec -it vc-test1 /bin/bash
root@082d6187d9ab:/# ls /data/
root@082d6187d9ab:/# echo 'v-test1' > /data/v-test1.txt
root@082d6187d9ab:/# exit

# 进入vC-test2,确认数据卷
root@docker:~# docker exec -it vc-test2 /bin/bash
root@3f8fde305ce3:/# ls /data/
v-test1.txt
root@3f8fde305ce3:/# echo 'v-test2 ' >/data/v-test2.txt
root@3f8fde305ce3:/# exit

# 回到vc-test1进行验证
root@docker:~# docker exec -it vc-test1 /bin/bash
root@082d6187d9ab:/# ls /data/
v-test1.txt  v-test2.txt
root@082d6187d9ab:/# cat /data/v-test2.txt
v-test2

# 回到宿主机查看/data/目录
root@docker:~# ls /data/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 网络管理

Docker 网络实现方式有两种︰

  • 端口映射
    • 将容器内服务端口与宿主机端口关联在一起,通过宿主机ip:宿主机port达到访问容器服务效果
  • 网络模式
    • 借助于独立的docker网卡功能实现访问容器服务的效果

命令格式︰

docker -P|p [镜像名称]
1

注意︰

  • -P(大写)指的是容器应用PORT随机映射到宿主机上的PORT
    • 自动绑定所有对外提供服务的容器端口,映射的端口将会从没有使用的端口池中自动随机选择,但是如果连续启动多个容器的话,则下一个容器的端口默认是当前容器占用端口号+1
    • 生产场景一般不使用随机映射,好处是由docker分配,宿主机端口不会冲突
  • -p(小写) 宿主机IP:宿主机PORT:容器PORT
    • 宿主机IP不写表示0.0.0.0,宿主机PORT不写表示随机端口,容器PORT必须指定
    • 可以同时对多个端口进行映射绑定
    • 指定端口映射,在标准化场景下使用频率高

# 默认随机映射

root@docker:~# docker run -d -P nginx
结果显示:
	多次执行后,宿主机的端口会自动出现
1
2
3

image-20220405195148849

image-20220405195251043

# 指定端口映射

root@docker:~# docker run -d -p 10.0.0.21:777:80 --name nginx-1 nginx
root@docker:~# docker run -d -p 10.0.0.21::80 --name nginx-2 nginx
root@docker:~# docker run -d -p :666:80 --name nginx-3 nginx
root@docker:~# docker run -d -p ::80 --name nginx-4 nginx
1
2
3
4

image-20220405195556583

# 多端口映射

root@docker:~# docker run -d -p 520:443 -p 6666:80 --name nginx-1 nginx
1

image-20220405195736417

# 网络模型

基本命令

root@docker:~# docker network help
Usage:  docker network COMMAND
Manage networks
Commands:
  connect     Connect a container to a network
  create      Create a network
  disconnect  Disconnect a container from a network
  inspect     Display detailed information on one or more networks
  ls          List networks
  prune       Remove all unused networks
  rm          Remove one or more networks
1
2
3
4
5
6
7
8
9
10
11

默认网络

root@docker:~# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
a1f9f31b7118   bridge    bridge    local
6f91742cf8c5   host      host      local
93ba74f24f75   none      null      local
1
2
3
4
5

五种模型

模型名称 特点 备注
bridge 默认模式,地址转换 网络效率低
host 使用宿主机的ip地址 效率高
container 容器和容器共享网络 局域网
none 无任何网络配置 最干净、最复杂
overlay 容器间跨网段通信 第三方工具

# 命令语法

创建网络:

docker network create --driver [网络类型] [网络名称]
1

使用网络:

docker run --net=[网络名称] -itd --name=[容器名称] [镜像名称]
1

端口网络

docker network disconnect [网络名] [容器名]
1

连接网络

docker network connect [网络名] [容器名]
1

# 仓库管理

仓库就类似于我们在网上搜索操作系统光盘的一个镜像站。这里的仓库指的是Docker镜像存储的地方。

分类:

  • 公有仓库:Docker hub、Docker cloud、等
  • 私有仓库:registry、 harbor等
  • 本地仓库:在当前主机存储镜像的地方。

相关命令

root@docker:~# docker help | grep -E 'rep|reg'
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
1
2
3
4
5

基本流程

  1. 根据registry镜像创建容器
  2. 配置仓库权限
  3. 提交镜像到私有仓库
  4. 测试

仓库配置

# 下载registry镜像
root@docker:~# docker pull registry

# 启动仓库容器
root@docker:~# docker run -d -p 5000:5000 registry

# 检查容器效果
root@docker:~# curl 127.0.0.1:5000/v2/_catalog

# 配置容器权限
root@docker:~# vim /etc/docker/daemon.json
{
  "registry-mirrors": ["https://3lhpcuov.mirror.aliyuncs.com"],
  "insecure-registries":["10.0.0.21:5000"]
}

注意:
	私有仓库的ip地址是宿主机的ip,而且ip两侧有双引号

# 重启docker服务
root@docker:~# systemctl restart docker
root@docker:~# systemctl status docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

查看效果

# 启动容器
root@docker:~# docker start ef6519877d6e

# 标记镜像
root@docker:~# docker tag ubuntu:16.04 10.0.0.21:5000/ubuntu-16.04

# 提交镜像
root@docker:~# docker push 10.0.0.21:5000/ubuntu-16.04

# 下载镜像
root@docker:~# docker pull 10.0.0.21:5000/ubuntu-16.04
1
2
3
4
5
6
7
8
9
10
11

注意: 镜像的名称必须携带仓库的地址,格式是:仓库地址/镜像名

#docker
Last Updated: 2024/03/29, 17:39:25
docker
Dockerfile

← docker Dockerfile→

最近更新
01
谷歌云创建GKE集群
07-26
02
ElastiCacheForRedis启用密码
07-26
03
upload-to-gcs
06-29
更多文章>
Theme by Vdoing | Copyright © 2021-2024 Brc | MIT License | 浙ICP备19031881号-4
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式