Câu hỏi phỏng vấn Docker
Câu hỏi

How can we control the startup order of services in Docker compose?

Câu trả lời

To control the startup order of services in Docker Compose, you can use the depends_on option. This option allows you to specify dependencies between services, ensuring that certain services start before others. However, it is important to note that depends_on only ensures that the dependent services are started, not that they are fully ready to accept connections.

Here are the key points to consider:

  1. Using depends_on:

    • The depends_on option specifies the order in which services are started. For example, if service A depends on service B, service B will start before service A.
    • Example:
      yaml Copy
      version: "3"
      services:
        web:
          build: .
          depends_on:
            - db
        db:
          image: postgres
  2. Handling Service Readiness:

    • Docker Compose does not wait for a service to be fully ready (e.g., a database accepting connections) before starting the next service. It only waits until the service is running.
    • To handle this, you can use wrapper scripts or tools like wait-for-it, dockerize, or sh-compatible wait-for to poll a given host and port until it is accepting TCP connections[1][2][3][4].
  3. Example with wait-for-it.sh:

    • You can wrap your service’s command with a script that waits for the dependent service to be ready.
    • Example:
      yaml Copy
      version: "3"
      services:
        web:
          build: .
          depends_on:
            - db
          command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
        db:
          image: postgres
  4. Custom Wrapper Script:

    • Alternatively, you can write your own wrapper script to perform a more application-specific health check.
    • Example script (wait-for-postgres.sh):
      sh Copy
      #!/bin/sh
      set -e
      host="$1"

...

middle

middle

Gợi ý câu hỏi phỏng vấn

middle

Could you explain what is Emulation?

senior

How is Container different from a Virtual Machine?

junior

What’s the difference between a repository and a registry?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào