Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] the restart command ignores the depends_on constraint #12477

Closed
yangjinheng opened this issue Jan 17, 2025 · 11 comments
Closed

[BUG] the restart command ignores the depends_on constraint #12477

yangjinheng opened this issue Jan 17, 2025 · 11 comments

Comments

@yangjinheng
Copy link

yangjinheng commented Jan 17, 2025

Description

the restart command ignores the depends_on constraint

Is this normal?

Steps To Reproduce

No response

Compose Version

Docker Compose version v2.32.4

Docker Environment

Client:
 Version:           27.4.1
 API version:       1.47
 Go version:        go1.22.10
 Git commit:        b9d17ea
 Built:             Tue Dec 17 15:44:45 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.4.1
  API version:      1.47 (minimum version 1.24)
  Go version:       go1.22.10
  Git commit:       c710b88
  Built:            Tue Dec 17 15:46:06 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.7.24
  GitCommit:        88bf19b2105c8b17560993bee28a01ddc2f97182
 runc:
  Version:          1.2.3
  GitCommit:        v1.2.3-0-g0d37cfd
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Anything else?

No response

@idsulik
Copy link
Collaborator

idsulik commented Jan 17, 2025

Please, fill the Steps To Reproduce section

@ndeloof
Copy link
Contributor

ndeloof commented Jan 17, 2025

depends_on is only applied as container is created, until you explicitly declare a dependent service to require being restarted after it's dependency was updated/restarted, see https://docs.docker.com/reference/compose-file/services/#long-syntax-1

@yangjinheng
Copy link
Author

depends_on is only applied as container is created, until you explicitly declare a dependent service to require being restarted after it's dependency was updated/restarted, see https://docs.docker.com/reference/compose-file/services/#long-syntax-1

I read this document, but it doesn't solve my problem. restart = true is not what I want.

I need the dependent service to be restarted after my service

Ensure data is saved correctly

@yangjinheng
Copy link
Author

yangjinheng commented Jan 17, 2025

请填写重现步骤部分

all containers restart at the same time, and operations with dependencies will fail

root@palyground:~/deploy# docker compose restart
[+] Restarting 7/7
 ✔ Container prod-mysql-1         Restarting    0.5s
 ✔ Container prod-prometheus-1    Restarting    0.5s
 ✔ Container prod-quickrest-1     Restarting    0.5s
 ✔ Container prod-nginx-1         Restarting    0.5s
 ✔ Container prod-quickproxy-1    Restarting    0.5s
 ✔ Container prod-quickadmin-1    Restarting    0.5s
 ✔ Container prod-quickservice-1  Restarting    0.5s

@ndeloof
Copy link
Contributor

ndeloof commented Jan 17, 2025

I need the dependent service to be restarted after my service

This is exactly what restart is about

Please provide an illustration example. Reporting issue with a single sentence and none of the requested information won't bring you the best assistance we can offer

@yangjinheng
Copy link
Author

请填写重新现步骤部分

所有容器同时重启,有依赖关系的操作将会失败

root@palyground:~/deploy# docker compose restart
[+] Restarting 7/7
 ✔ Container prod-mysql-1         Restarting    0.5s
 ✔ Container prod-prometheus-1    Restarting    0.5s
 ✔ Container prod-quickrest-1     Restarting    0.5s
 ✔ Container prod-nginx-1         Restarting    0.5s
 ✔ Container prod-quickproxy-1    Restarting    0.5s
 ✔ Container prod-quickadmin-1    Restarting    0.5s
 ✔ Container prod-quickservice-1  Restarting    0.5s

I want prod-quickadmin-1 to restart first, and then prod-mysql-1 to restart

@yangjinheng
Copy link
Author

yangjinheng commented Jan 17, 2025

I need the dependent service to be restarted after my service

This is exactly what restart is about

Please provide an illustration example. Reporting issue with a single sentence and none of the requested information won't bring you the best assistance we can offer

networks:
  quicknet:

volumes:
  data:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /data/
  mysql:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /data/mysql

services:
  mysql:
    image: mysql:8.4.0-oraclelinux8
    environment:
      - TZ=Asia/Shanghai
      - MYSQL_USER=root
      - MYSQL_DATABASE=db_quick
      - MYSQL_PASSWORD=password
      - MYSQL_ROOT_PASSWORD=password
    command: ["--character-set-server=utf8mb4", "--collation-server=utf8mb4_bin"]
    restart: always
    healthcheck:
      test: ["CMD-SHELL", 'mysqladmin ping --silent --password="$$MYSQL_ROOT_PASSWORD"']
      start_period: 10m
      start_interval: 3s
      timeout: 10s
      retries: 3
      interval: 60s
    networks: ["quicknet"]
    volumes:
      - mysql:/var/lib/mysql

  quickadmin:
    image: quickadmin:v1.0.0
    env_file: [".env"]
    environment:
      - TZ=Asia/Shanghai
      - DOCKER_HOST=unix:///var/run/docker.sock
    command: ["quickadmin", "-f", "/etc/quick/service/admin.yaml"]
    healthcheck:
      test: ["CMD-SHELL", 'curl -s -o /dev/null http://127.0.0.1']
      start_period: 10m
      start_interval: 3s
      timeout: 10s
      retries: 3
      interval: 60s
    restart: always
    depends_on:
      mysql:
        condition: service_healthy
      prometheus:
        condition: service_started
    networks: ["quicknet"]
    volumes:
      - data:/data/
      - /var/run/docker.sock:/var/run/docker.sock
    ports: []

The quickadmin service will capture the 15 signal and save some data to mysql

@docker docker deleted a comment from yangjinheng Jan 17, 2025
@ndeloof
Copy link
Contributor

ndeloof commented Jan 17, 2025

IIUC you want quickadmin service to be restart as you restart mysql service.
Then restart is what you need

services:
  db:
    ...
  admin:
    ...
    depends_on:
      db:
        condition: service_started
        restart: true
$ docker compose --progress=plain restart db
 Container truc-db-1  Restarting
 Container truc-db-1  Started
 Container truc-admin-1  Restarting
 Container truc-admin-1  Started

@ndeloof ndeloof closed this as not planned Won't fix, can't repro, duplicate, stale Jan 21, 2025
@yangjinheng
Copy link
Author

IIUC 您希望quickadmin在重新启动服务时重新启动服务mysql。 那么这restart就是您所需要的

services:
db:
...
admin:
...
depends_on:
db:
condition: service_started
restart: true

$ docker compose --progress=plain restart db
 Container truc-db-1  Restarting
 Container truc-db-1  Started
 Container truc-admin-1  Restarting
 Container truc-admin-1  Started

thanks for reply

i want to support depends_on in docker compose restart

do not specify a service name

@ndeloof
Copy link
Contributor

ndeloof commented Jan 23, 2025

restart do support depends_on.

Please provide a minimal example with the actual behavior and what you expect to be the desired behavior

@yangjinheng
Copy link
Author

yangjinheng commented Jan 23, 2025

restart do support depends_on.

Please provide a minimal example with the actual behavior and what you expect to be the desired behavior

thank you for your patience

look at this code, it is my application
after receiving the signal, it writes some data to the database

main.go

package main

import (
	"database/sql"
	"errors"
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/go-sql-driver/mysql"
)

func main() {
	c := mysql.Config{
		User:   "root",
		Passwd: "password",
		Net:    "tcp",
		Addr:   "mysql:3306",
		DBName: "example",
	}
	db, err := sql.Open("mysql", c.FormatDSN())
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// make sure the table exists
	_, err = db.Exec("CREATE TABLE IF NOT EXISTS counter (id INTEGER PRIMARY KEY AUTO_INCREMENT, create_at VARCHAR(255))")
	if err != nil {
		log.Fatal(err)
	}

	// print the last record
	var (
		id       int
		createAt string
	)
	err = db.QueryRow("SELECT id, create_at FROM counter ORDER BY id DESC LIMIT 1").Scan(&id, &createAt)
	if err != nil && !errors.Is(err, sql.ErrNoRows) {
		log.Fatal(err)
	}
	fmt.Println("id:", id, "create_at:", createAt)

	// wait for OS signal, then write some data to the database
	signals := make(chan os.Signal, 1)
	signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT)

        // !!! this is important
	<-signals
	_, err = db.Exec("INSERT INTO counter (create_at) VALUES (?)", time.Now().Format(time.DateTime))
	if err != nil {
		log.Fatal(err)
	}
}

docker-compose.yaml

networks:
  example:

volumes:
  mysql:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /data/mysql

services:
  mysql:
    image: mysql:8.4.0-oraclelinux8
    environment:
      - MYSQL_DATABASE=example
      - MYSQL_ROOT_PASSWORD=password
    command: ["--character-set-server=utf8mb4", "--collation-server=utf8mb4_bin"]
    restart: always
    healthcheck:
      test: ["CMD-SHELL", 'mysqladmin ping --silent --password="$$MYSQL_ROOT_PASSWORD"']
      start_period: 10m
      start_interval: 3s
      timeout: 10s
      retries: 3
      interval: 60s
    networks: [example]
    volumes:
      - mysql:/var/lib/mysql

  myapp:
    build: .
    image: myapp:1.0.0
    restart: always
    depends_on:
      mysql:
        condition: service_healthy
    networks: [example]
FROM golang:1.23.5-alpine

WORKDIR /app
COPY . .

#ENV GOPROXY='https://goproxy.cn,https://goproxy.io,direct'

RUN go build -o myapp .
CMD ["./myapp"]

let's run it

docker-compose up -d --build

then run

docker-compose down # because of depends_on the data is preserved

run as a comparison

docker-compose restart # data saving failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants