Search code examples
dockerapache-apisix

apisix docker wont start


I’m trying to set up APISIX (version 3.11.0) using Docker Compose, but I’m running into an issue where APISIX cannot connect to etcd. I’ve been stuck on this for a while and would appreciate any help or guidance.

What I’m trying to do: I’m following the official APISIX Docker setup to deploy APISIX and etcd using Docker Compose. My goal is to get APISIX running and connected to etcd.

The problem: APISIX fails to start because it cannot connect to etcd. The logs show the following error:

Copy

Warning! Request etcd endpoint 'http://127.0.0.1:2379/version' error, connection refused, retry time=1
Warning! Request etcd endpoint 'http://127.0.0.1:2379/version' error, connection refused, retry time=2
request etcd endpoint 'http://127.0.0.1:2379/version' error, connection refused
all etcd nodes are unavailable

However, when I test the etcd endpoint from my host machine, it works fine:

bash Copy wget http://127.0.0.1:2379/version Response:

json Copy {"etcdserver":"3.5.11","etcdcluster":"3.5.0"} My setup: Here’s my docker-compose.yml file:

yaml Copy

version: "3"

services:
  apisix:
    image: apache/apisix:${APISIX_IMAGE_TAG:-3.11.0-debian}
    restart: always
    volumes:
      - ./apisix_conf/config.yaml:/usr/local/apisix/conf/config.yaml:ro
    depends_on:
      - etcd
    ports:
      - "9180:9180/tcp"
      - "9080:9080/tcp"
      - "9091:9091/tcp"
      - "9443:9443/tcp"
      - "9093:9092/tcp"
    networks:
      - apisix

  etcd:
    image: bitnami/etcd:3.5.11
    restart: always
    volumes:
      - etcd_data:/bitnami/etcd
    environment:
      ETCD_ENABLE_V2: "true"
      ALLOW_NONE_AUTHENTICATION: "yes"
      ETCD_ADVERTISE_CLIENT_URLS: "http://etcd:2379"
      ETCD_LISTEN_CLIENT_URLS: "http://0.0.0.0:2379"
    ports:
      - "2379:2379/tcp"
    networks:
      - apisix

networks:
  apisix:
    driver: bridge

volumes:
  etcd_data:
    driver: local

And here’s my config.yaml for APISIX:

yaml Copy

apisix:
  node_listen: 9080
  enable_admin: true
  admin_key:
    - name: "admin"
      key: "admin123"
      role: admin

etcd:
  host:
    - "http://etcd:2379"

What I’ve tried so far: Verified that etcd is running and accessible from the host machine.

Checked that both APISIX and etcd containers are attached to the same Docker network (apisix-docker_apisix).

Confirmed that the config.yaml file is correctly mounted in the APISIX container.

Modified the etcd.host configuration to use http://etcd:2379 instead of http://127.0.0.1:2379.

Restarted the containers multiple times.

Additional information: Docker version: 20.10.12

Docker Compose version: 2.20.3

Operating System: Ubuntu 24.04

Questions: Why is APISIX still trying to connect to http://127.0.0.1:2379 instead of http://etcd:2379?

Is there something wrong with my docker-compose.yml or config.yaml configuration?

Are there any additional steps I need to take to ensure APISIX can connect to etcd?

Any help or suggestions would be greatly appreciated! Thank you in advance.


Solution

  • The issue is that your misplaced some keys in the config file.

    What's Wrong

    File apisix_conf/config.yaml:

    • admin_key field is child of deployment.admin, not apisix:

      deployment:
        admin:
          admin_key:
            - name: "admin"
              key: "admin123"
              role: admin
      
    • etcd is child of deployment, not root level:

      deployment:
        etcd:
          host:
            - "http://etcd:2379"
      

    Extra

    File apisix-compose.yaml:

    • The port mapping 9093:9092/tcp is probably an oversight, maybe you meant 9092:9092/tcp

    • depends_on doesn't work properly, since in this case it only ensures that apisix container is started after etcd one. A better solution would be to include a healthcheck, to prevent the "failed healthcheck flooding" in APISIX container would be the following:

      services:
        apisix:
          depends_on:
            etcd:
              condition: service_healthy
          # Remaining config for apisix service
      
        etcd:
          healthcheck:
            test: "etcdctl endpoint health"
            interval: 5s
            timeout: 30s
            retries: 5
          # Remaining config for etcd service
      

      This ensures that APISIX container will only start when the etcd container is healthy (i.e. the healthcheck succeeds);

    • As suggested in logs, the attribute version (version: "3") is obsolete, and is ignored, so it can be removed;

    Solution

    This is a draft of what a correct base configuration would look like:

    File apisix-compose.yaml:

    services:
      etcd:
        container_name: etcd
        image: bitnami/etcd:3.5.17
        volumes:
          - etcd_data:/bitnami/etcd
        environment:
          ETCD_ENABLE_V2: "true"
          ALLOW_NONE_AUTHENTICATION: "yes"
          ETCD_ADVERTISE_CLIENT_URLS: "http://etcd:2379"
          ETCD_LISTEN_CLIENT_URLS: "http://0.0.0.0:2379"
        healthcheck:
          test: "etcdctl endpoint health"
          interval: 5s
          timeout: 30s
          retries: 5
        ports:
          - "2379:2379"
        networks:
          apisix:
    
      apisix:
        container_name: apisix
        image: apache/apisix:${APISIX_IMAGE_TAG:-3.11.0-debian}
        volumes:
          - ./apisix_conf/config.yaml:/usr/local/apisix/conf/config.yaml:ro
        restart: always
        depends_on:
          etcd:
            condition: service_healthy
        ports:
          - "9180:9180/tcp"
          - "9080:9080/tcp"
          - "9091:9091/tcp"
          - "9443:9443/tcp"
          - "9092:9092/tcp"
        networks:
          apisix:
    
    networks:
      apisix:
        driver: bridge
    
    volumes:
      etcd_data:
        driver: local
    
    

    File apisix_conf/config.yaml:

    apisix:
      node_listen: 9080
      enable_admin: true
    
    deployment:
      admin:
        admin_key:
          - name: "admin"
            key: "admin123"
            role: admin
    
      etcd:
        host:
          - "http://etcd:2379"