skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. Not sure if you’re still looking for an answer, if so, I’ve outlined one here for you

    https://github.com/devobs-ob/stackoverflow/tree/main/docker/DynamicImage-78493215

    TLDR:

    • create a script that retrieves the type of arch, store it in a variable, and let it choose the image based on the value
    • replace the image with a variable in the image field of the Dockerfile.

    You can find instructions and the scripts in the Github repo linked above. Hope this helps and thanks for sharing your question.

    — Ob

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search