Skip to content

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

Closed

Description

@yangjinheng

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

Activity

  1. idsulik commented on Jan 17, 2025

    @idsulik
    Collaborator

    Please, fill the Steps To Reproduce section

  2. ndeloof commented on Jan 17, 2025

    @ndeloof
    Contributor

    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

  3. yangjinheng commented on Jan 17, 2025

    @yangjinheng
    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

  4. yangjinheng commented on Jan 17, 2025

    @yangjinheng
    Author

    请填写重现步骤部分

    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
    
  5. ndeloof commented on Jan 17, 2025

    @ndeloof
    Contributor

    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

  6. yangjinheng commented on Jan 17, 2025

    @yangjinheng
    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

  7. yangjinheng commented on Jan 17, 2025

    @yangjinheng
    Author

    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

  8. deleted a comment from yangjinheng on Jan 17, 2025
  9. ndeloof commented on Jan 17, 2025

    @ndeloof
    Contributor

    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
    
  10. yangjinheng commented on Jan 23, 2025

    @yangjinheng
    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

  11. ndeloof commented on Jan 23, 2025

    @ndeloof
    Contributor

    restart do support depends_on.

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

  12. yangjinheng commented on Jan 23, 2025

    @yangjinheng
    Author

    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
  13. PrawinPracash commented on Mar 19, 2025

    @PrawinPracash

    Even I am facing the same issue. While 'systemctl restart docker' its restarting all the containers simultaneously but not considering depends_on and not spinning up the containers in a proper order. Is this issue fixed ? Please let me know.

  14. yangjinheng commented on Mar 19, 2025

    @yangjinheng
    Author

    Even I am facing the same issue. While 'systemctl restart docker' its restarting all the containers simultaneously but not considering depends_on and not spinning up the containers in a proper order. Is this issue fixed ? Please let me know.

    problem not fixed.

    I don't know why it was shut down.

  15. ndeloof commented on Mar 19, 2025

    @ndeloof
    Contributor

    @PrawinPracash systemctl restart docker is unrelated to docker compose. This restarts docker daemon, so all container will be restarted at once. depends_on is a compose-only feature, that only applies to docker compose .. commands, engine isn't aware of such dependencies.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions