2团
Published on 2024-08-16 / 39 Visits
0
0

在Docker Compose中重建Docker容器

1. 前言

Halo镜像的脚本中,默认设置堆大小为256M。因为云主机的资源较为充沛,因此想更改堆大小至2G,因此尝试在Docker Compose文件中添加环境变量,然而重启应用后发现配置未生效。

发现需要重新构建Halo镜像,在网上搜索到博文(https://blog.51cto.com/coderaction/5647297),转而进行记录至本博客进行备份。

2. 创建组合镜像

创建包含两个容器的docker-compose.yml配置文件:一个引用最新的Ubuntu镜像,一个引用最新的apline镜像。每个镜像均添加tty:true的伪终端配置,以此防止容器在启动之后自动退出。

version: "3.9"
services:
  ubuntu:
    image: "ubuntu:latest"
    tty: true
  alpine:
    image: "alpine:latest"
    tty: true

执行命令,在后台创建容器:

image-tskb.png

3. 独立重建和重启容器

如果需要重新构建Ubuntu镜像,在容器启动命令中指定Ubuntu镜像,并添加force-recreate标志,具体如下所示:

image-ryqs.png

4. 容器依赖于其他容器

删除先前创建的容器:

image-bipo.png
重置docker-compose.yml文件,内容如下所示:

version: "3.9"
services:
  ubuntu:
    image: "ubuntu:latest"
    tty: true
    depends_on:
      - "alpine"
  alpine:
    image: "alpine:latest"
    tty: true

在当前的配置中,Ubuntu容器依赖于Apline容器,此时若要重建Ubuntu容器,可以添加no-deps​告知docker compose不需要重建链接的容器:

docker compose up -d --force-recreate --build --no-deps ubuntu

5. Halo堆大小设置

在docker-compose文件halo配置中添加JVM_OPTS设置,具体如下所示:

version: "3"

services:
  halo:
    image: halohub/halo:2.8.0
    container_name: halo
    restart: on-failure:3
    network_mode: "host"
    volumes:
      - ./:/root/.halo2
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8090/actuator/health/readiness"]
      interval: 30s
      timeout: 5s
      retries: 5
      start_period: 30s
    environment:
      JVM_OPTS: "-Xmx2G -Xms2G"
    command:
      - --halo.cache.page.disabled=true
      - --spring.r2dbc.url=r2dbc:pool:mysql://localhost:3306/halo


Comment