skip to Main Content

I’m trying to access the Minio S3-API endpoint from within my container, but my app can’t resolve the container name.

The issue is the framework that I’m using, uses the @smithy/middleware-endpoint API, which requires a fully qualified URL. As my MinIO instance is started with the rest of the stack with the endpoint passed into my app on start-up, using the container hostname; http://minio:9000 doesn’t work.

However, I can’t hardcode the IP in my docker-compose file as it changes every time I spin up docker-compose.

My docker-compose file looks something like this:

version: '3.9'

services:

  backend:
    build: ./backend/tus/.
    container_name: backend
    ports:
      - "8181:8181"
    environment:
      - MINIO_ENDPOINT=http://minio:9000
    depends_on:
      - minio

  # the minio instance that the back end streams the data uploads to
  minio:
    image: quay.io/minio/minio
    container_name: minio
    command: server /data --console-address ":9001"
    environment:
      - MINIO_ROOT_USER=minioadmin
      - MINIO_ROOT_PASSWORD=minioadmin
    volumes:
      - minio_data:/data
    ports:
      - "9000:9000"
      - "9001:9001"

The code that’s using the environment variable MINIO_ENDPOINT looks something like this:

/* Set up the s3 store to pass to the tus server */
const datafileS3Store = new S3Store({
    s3ClientConfig: {
        bucket: process.env.DATAFILE_BUCKET,
        region: "eu-west-2",
        endpoint: process.env.MINIO_ENDPOINT,
        credentials: {
            accessKeyId: process.env.MINIO_USERNAME,
            secretAccessKey: process.env.MINIO_PASSWORD,
        },
    },
})

However MinIO outputs its IP addresses on start-up, usually something like:

S3-API: http://172.19.0.5:9000 http://127.0.0.1:9000

Console: http://172.19.0.5:9001 http://127.0.0.1:9001

So, does anyone know how I might be able to get (or even set?) the S3-API IP of the MinIO Instance and pass it into my backend container?

2

Answers


  1. Chosen as BEST ANSWER

    The solution was to create a new network and hardcode the container IP address so that the IP can be passed in as as an env var into the backend.

    compose file:

    version: '3.9'
    
    services:
    
      backend:
        build: ./backend/tus/.
        container_name: backend
        ports:
          - "8181:8181"
        environment:
          - MINIO_ENDPOINT=http://172.20.0.10:9000
          - MINIO_USERNAME=minioadmin
          - MINIO_PASSWORD=minioadmin
        depends_on:
          - minio
        networks:
          customnetwork:
            ipv4_address: 172.20.0.11
    
    
      # the minio instance that the back end streams the data uploads to
      minio:
        image: quay.io/minio/minio
        container_name: minio
        command: server /data --console-address ":9001"
        environment:
          - MINIO_ROOT_USER=minioadmin
          - MINIO_ROOT_PASSWORD=minioadmin
        volumes:
          - minio_data:/data
        ports:
          - "9000:9000"
          - "9001:9001"
        networks:
          customnetwork:
            ipv4_address: 172.20.0.10
    
      # script that inits minio with some buckets
      createbuckets:
        image: minio/mc
        depends_on:
          - minio
        entrypoint: >
          /bin/sh -c "
          /usr/bin/mc alias set minio http://172.20.0.10:9000 minioadmin minioadmin;
          /usr/bin/mc mb minio/datafiles;
          /usr/bin/mc policy set public minio/datafiles;
          exit 0;
          "
        networks:
          customnetwork:
            ipv4_address: 172.20.0.13
    
    volumes:
      minio_data:
    
    networks:
     customnetwork:
      ipam:
        config:
        - subnet: 172.20.0.0/16
    

    The key thing here is that all other containers need to be added to network:

    networks:
      - customnetwork
    

  2. Your solution would be to create custom network and assigned static IP addresses to the containers. That does allow for a predictable IP address for the MinIO container that can be hard-coded as an environment variable in the backend container.

    +-----------------------------------+
    |          Docker Host              |
    |                                   |
    | +-------------+  customnetwork    |
    | |   Backend   |-----+             |
    | | 172.20.0.11 |     |             |
    | +-------------+     |             |
    |                     |             |
    | +-------------+     |             |
    | |    MinIO    |-----+             |
    | | 172.20.0.10 |                   |
    | +-------------+                   |
    |                                   |
    | +-----------------+               |
    | |  createbuckets  |               |
    | |  172.20.0.13    |               |
    | +-----------------+               |
    |                                   |
    +-----------------------------------+
    

    Each container is assigned a static IP address within the customnetwork. The Backend container can directly communicate with the MinIO container using the hard-coded IP address 172.20.0.10.


    But I would suggest using a BIND9 DNS container (sameersbn/docker-bind), in order to declare and resolve a FQDN for minio.
    See "Running a DNS Server with Docker" from Mike Polinowski for illustration.

    +-------------------------------------------------------------+
    |                        Docker Host                          |
    |                                                             |
    |  +------------------+     +---------------------+           |
    |  |      MinIO       |     |        BIND9        |           |
    |  |   (Service 1)    |<--->|      (Service 2)    |           |
    |  +------------------+     +---------------------+           |
    |         |                   |                               |
    |         | Interaction       | DNS Resolution                |
    |         v                   v                               |
    |  +-------------------+      minio.example.com               |
    |  |   Backend/tus    |                                       |
    |  |   (Service 3)    |                                       |
    |  +-------------------+                                      |
    |                                                             |
    +-------------------------------------------------------------+
    
    • MinIO service starts up first.
    • BIND9 service starts, obtains MinIO’s IP address, updates its zone file, and starts the DNS server.
    • Other Docker containers or external clients can now resolve minio.example.com via the BIND9 DNS service, which resolves it to MinIO’s IP address within the Docker network.

    The docker-compose.yml (v3) would be:

    version: '3.9'
    
    services:
      backend:
        build: ./backend/tus/.
        container_name: backend
        ports:
          - "8181:8181"
        environment:
          - MINIO_ENDPOINT=http://minio.example.com:9000
          - MINIO_USERNAME=minioadmin
          - MINIO_PASSWORD=minioadmin
        depends_on:
          - bind9
    
      minio:
        image: quay.io/minio/minio
        container_name: minio
        command: server /data --console-address ":9001"
        environment:
          - MINIO_ROOT_USER=minioadmin
          - MINIO_ROOT_PASSWORD=minioadmin
        volumes:
          - minio_data:/data
        networks:
          - customnetwork
        ports:
          - "9000:9000"
          - "9001:9001"
    
      bind9:
        image: sameersbn/bind
        volumes:
          - ./bind:/etc/bind
        networks:
          - customnetwork
        depends_on:
          - minio
        entrypoint: /bin/bash -c "sleep 30; nslookup minio > /etc/bind/minio_ip.txt; ip=$(cat /etc/bind/minio_ip.txt | grep 'Address: ' | cut -d ' ' -f 2); echo -e '@       IN      SOA     ns.example.com. admin.example.com. ( 2023100901 604800 86400 2419200 604800 )n        IN      NS      ns.example.com.nminio.example.com   IN      A       '$ip > /etc/bind/zones/db.example.com; named -g"
    
      createbuckets:
        image: minio/mc
        depends_on:
          - minio
        entrypoint: >
          /bin/sh -c "
          /usr/bin/mc alias set minio http://minio.example.com:9000 minioadmin minioadmin;
          /usr/bin/mc mb minio/datafiles;
          /usr/bin/mc policy set public minio/datafiles;
          exit 0;
          "
        networks:
          - customnetwork
    
    networks:
      customnetwork:
        ipam:
          config:
          - subnet: 172.20.0.0/16
    
    volumes:
      minio_data:
      bind:
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search