docker使用记录

烂柯 发布于 2024-01-05 119 次阅读


一、概述

Compose 是一个用于定义和运行多容器 Docker 应用程序的工具(docker编排管理工具)。通过 Compose,您可以使用 YAML 文件来配置应用程序的服务(构建或运行)。使用单个命令,可以根据配置创建并启动所有服务( 2023 年 7 月起,Compose V1 停止接收更新,并且不再出现在新的 Docker Desktop 版本中。Compose V2 已取代它)。

docker安装文档,docker中访问宿主机使用的主机名:host.docker.internal

二、配置

linux设置开机自启systemctl enable docker

取消开机自启systemctl disable docker

查看docker信息 docker info

1、配置代理

有三种方式可以实现docker配置代理,这里推荐配置daemon.json文件的方式

#编辑daemon.json文件
vi /etc/docker/daemon.json

{
  "proxies": {
    "http-proxy": "http://proxy.example.com:3128",
    "https-proxy": "https://proxy.example.com:3129",
    "no-proxy": "*.test.example.com,.example.org,127.0.0.0/8"
  }
}

#重启docker
sudo systemctl restart docker

2、配置存储

docker安装官方文档都比较详细,只是需要注意下docker的root目录

查看存储目录:sudo docker info | grep "Docker Root Dir"

#根据docker情况,自行判断是否需要迁移旧目录信息
#迁移默认目录
rsync -avz /var/lib/docker /mnt/docker-data

方式一、配置daemon.json

vi /etc/docker/daemon.json
{
  "data-root": "/mnt/docker-data"
}

方式二、软连接

#默认存储位置: /var/lib/docker
#注意如果目录目录已存在需要重命名或者删除掉docker目录
#mv /var/lib/docker /var/lib/docker-backup
ln -s /mnt/docker-data  /var/lib/docker

方式三、启动命令

dockerd参数配置信息

#可以直接调整docker服务启动命令
#/usr/lib/systemd/system/docker.service
#或者通过systemd系统中的Drop-In机制,创建docker.service.d配置docker.conf进行参数覆盖
sudo vi /etc/systemd/system/docker.service.d/docker.conf 

[Service] 
ExecStart=/usr/bin/dockerd -H fd:// --data-root="/mnt/docker-data" --containerd=/run/containerd/containerd.sock

三、镜像选择

四、构建运行

下面以构建运行最小nginx容器为例

1、下载nginx源码

curl  -kO https://github.com/nginx/nginx/archive/refs/tags/release-1.25.3.tar.gz

2、创建dockerfile

dockerfile参考文档 docker-nginx

#不一定非要以此命名,可以定义名称,只需要在编译时通过 -f 参数指定自定义名称
vi dockerfile
FROM alpine:3.18 as build
#设置国内镜像源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
#添加环境
RUN apk add --no-cache --virtual .build-deps \
    gcc libc-dev make openssl-dev pcre2-dev &>/dev/null
#添加源码
ADD release-1.25.3.tar.gz /opt
WORKDIR /opt/nginx-release-1.25.3
RUN ./auto/configure --prefix=/usr/local/nginx --with-http_ssl_module \
    && make&&make install &>/dev/null
#卸载编译环境#删除源码
RUN apk del .build-deps  \
    && rm -rf /opt/nginx-release-1.25.3 &>/dev/null

FROM alpine:3.18
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \
    && apk add --no-cache openssl pcre2 &>/dev/null
COPY --from=build /usr/local/nginx /usr/local/nginx
WORKDIR /usr/local/nginx
#设置时区
RUN apk add --no-cache tzdata \
    && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone \
    && apk del tzdata &>/dev/null
EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

构建优化内容点(单文件、多文件、多阶段)

  1. 不需要的输出内容写入 /dev/null
  2. 减少RUN指令
  3. 采用多阶段构建,即使用FROM分层镜像

3、测试构建运行

#构建
docker build -t test-nginx:t0.0.1 .
#运行
docker run -dp 8080:80 --name test-nginx test-nginx:t0.0.1

五、docker集群及编排

1、docker compose

​ Compose 是用于定义和运行多容器 Docker 应用程序的工具。可以使用 YML 文件来配置应用程序需要的所有服务。然后通过 docker compose 命令创建和启动、编排所有配置的的服务。

dockercompose参考文档,可用通过composerize将run命令转为docker-compose.yml

a)、创建docker-compose

#不一定非要以此命名,可以定义名称只需要在使用时通过 -f 参数指定自定义名称
#项目名称默认以当前文件名称为项目名称,使用时可以通过 -p 参数指定项目名称
vi docker-compose.yml
name: <project name>
services:
  #服务1名称
  test-nginx:
    build:
      #dockerfile文件所在目录
      context: ./
      #dockerfile: dockerfile
    image: test-nginx:t0.0.1
    ports:
      - 8081:80
    #自动重启
    restart: always
    #网络模式
    #network_mode: host
    #容器执行后缀参数
    #command: --urls=http://*:8500
    #存储映射
    #volumes:
    #  - /opt/api/appsettings.gateway.json:/publish/appsettings.Development.json
    #环境变量
    #environment:
    #  - ASPNETCORE_ENVIRONMENT=Development
user  nginx;

events {
    worker_connections   1000;
}

http {
  server {
    listen 80 default_server;
    location / {
      #设置DNS过期时间为1s
      resolver 127.0.0.11 valid=1s;
      set $backend http://nginx;
      proxy_pass $backend;
    }
  }
}

b)、操作命令

#启用
docker compose up
#启用后台
#docker compose up -d
#设置指定服务副本数量
docker compose scale nginx=3
#卸载
docker compose down

2、docker swarm

容器集群工具,比k8s更轻量没有那么复杂

#初始化
docker swarm init
#可以通过参数 --advertise-addr 指定接口地址
Command Description
docker swarm ca Display and rotate the root CA
docker swarm init Initialize a swarm
docker swarm join Join a swarm as a node and/or manager
docker swarm join-token Manage join tokens
docker swarm leave Leave the swarm
docker swarm unlock Unlock swarm
docker swarm unlock-key Manage the unlock key
docker swarm update Update the swarm

3、docker service

集群服务管理命令,用于定义和运行多容器 Docker 应用程序的工具。service直接使用命令进行容器编排管理

#创建服务
docker service create \
  -p 8080:80 \
  --env ASPNETCORE_ENVIRONMENT=Development \
  --network bridge \
  --replicas 2 \
  --mount type=bind,src=/mnt/web-api/appsettings.json,dst=/publish/appsettings.json \
  --name nginx_service nginx:v0.0.1;

#更新服务镜像
docker service update --image nginx:v0.0.2 nginx_service;
Command Description
docker service create Create a new service
docker service inspect Display detailed information on one or more services
docker service logs Fetch the logs of a service or task
docker service ls List services
docker service ps List the tasks of one or more services
docker service rm Remove one or more services
docker service rollback Revert changes to a service's configuration
docker service scale Scale one or multiple replicated services
docker service update Update a service

4、docker stack

容器编排工具,service命令的升级版本,可以聚合批量管理多个服务。Docker stack 忽略了build指令,无法使用stack命令构建新镜像

Command Description
docker stack config Outputs the final config file, after doing merges and interpolations
docker stack deploy Deploy a new stack or update an existing stack
docker stack ls List stacks
docker stack ps List the tasks in the stack
docker stack rm Remove one or more stacks
docker stack services List the services in the stack

docker compose和docker stack工具命令很相似,compose可以build更适合测试环境,stack强化service概念生产部署表现会更好

六、镜像导入导出

注意以下两种方式不可交叉使用,

方式一、保存镜像

#1、保存镜像并归档压缩
docker save test-image:latest|gzip > test-image.tar.gz
#仅归档
#docker save test-image:latest > test-image.tar
#2、载入镜像
docker load < test-image.tar.gz

注:save后面可以是镜像id,但是导入后镜像名称也会是镜像id

方式二、导出容器镜像

#1、根据容器id导出镜像归档并压缩
docker export e8becd36488b |gzip > test-image.tar.gz
#仅归档
#docker export e8becd36488b > test-image.tar
#2、导入镜像,指定镜像名称及tag
docker import - test-image:v0.0.1 < test-image.tar.gz
烂柯

最后更新于 2024-12-28