Search code examples
dockerdocker-composedevops

Docker Compose: dynamically change the image architecture based on running host machine


quick question: is it possible to dynamically change the image's arch based on the host's machine instead of create 2 separate docker compose file?

Currently I have 2 files:

For arm64 (osx)

services:
  typesense:
    image: docker.io/typesense/typesense:0.25.2-arm64
    container_name: typesense
    restart: on-failure
    ports:
      - "8108:8108"
    volumes:
      - ./typesense-data:/data
    command: '--data-dir /data --api-key=xyz --enable-cors'

For non arm64

services:
  typesense:
    image: docker.io/typesense/typesense:0.25.2
    container_name: typesense
    restart: on-failure
    ports:
      - "8108:8108"
    volumes:
      - ./typesense-data:/data
    command: '--data-dir /data --api-key=xyz --enable-cors'

The only difference here is the image, and ideally running them e.g. docker compose up -d then let the engine decide which arch is appropriate in the host's machine instead of using docker compose -f <FILE> up -d.


Solution

  • The image used in that example is already a multi-platform image. To check the arch of image, one can check the running container:

    # docker inspect <container-id> or docker inspect <your-image>
    docker inspect typesense/typesense:0.25.2
    

    see docker ref: https://docs.docker.com/build/building/multi-platform

    devops thread: https://devops.stackexchange.com/a/19307/24024

    devtalk thread: https://forum.devtalk.com/t/docker-compose-dynamically-change-the-image-architecture-based-on-running-host-machine/155452/10?u=jaeyson