Apache APISIX Docker Deployment Guide

repository·master·Indexed 21 days ago

https://github.com/apache/apisix-docker

Documentation for deploying Apache APISIX, a high-performance dynamic API Gateway, using Docker. It covers various deployment modes including stand-alone mode and etcd-backed configurations via host or bridge networks. The guide provides instructions for building custom Docker images from source or specific releases, managing the Apache APISIX Dashboard, configuring SSL certificates with mkcert, and using the Admin API for service and route management.

Tokens
5K
Snippets
25
Records
25
Agent score
75%

What's inside apache-apisix-docker

  1. Reload Apache APISIX without downtime

    master

    If you modify the configuration files (such as config.yaml) inside a running APISIX container, you must trigger a reload for the changes to take effect. You can do this without causing downtime by executing the apisix reload command inside the container.

    docker exec -it apache-apisix apisix reload
  2. Build the Apache APISIX Dashboard Docker image

    master

    You can build the Apache APISIX Dashboard image using docker build. You must provide the APISIX_DASHBOARD_VERSION as a build argument.

    Requirements:

    • Docker version 17.05.0-ce or higher is required to support the build process.
    $ docker build --build-arg APISIX_DASHBOARD_VERSION=$APISIX_DASHBOARD_VERSION -t $IMAGE_NAME .
  3. Build a Docker image from an APISIX release

    master

    You can build a Docker image for a specific APISIX release version using make. You must specify the APISIX_VERSION (e.g., 3.17.0) and the DISTRO (either debian or redhat).

    APISIX_VERSION=3.17.0   # specify release version
    DISTRO=debian           # debian, redhat
    make build-on-$DISTRO
  4. Build an Apache APISIX Docker image from source

    master

    To build a custom Docker image, first clone the apisix-docker repository and enter the project directory.

    Note: Docker images in this repository are provided for convenience and are not official ASF releases. The recommended approach is to build from the source.

    git clone https://github.com/apache/apisix-docker.git
    cd apisix-docker
  5. Run Apache APISIX server container

    master

    Run the Apache APISIX container with specific port mappings and volume mounts for configuration and logs.

    Key Requirements:

    • The container must be on the same Docker network as the etcd server.
    • The config.yaml mounted to /usr/local/apisix/conf/config.yaml must have the correct etcd address pointing to the etcd container's IP (e.g., http://172.18.5.10:2379).

    Port Mappings:

    • 9080: HTTP port
    • 9091: Admin API port
    • 9443: HTTPS port
    docker run --name test-api-gateway \
     -v `pwd`/example/apisix_conf/config.yaml:/usr/local/apisix/conf/config.yaml \
     -v `pwd`/example/apisix_log:/usr/local/apisix/logs  \
     -p 9080:9080 \
     -p 9091:9091  \
     -p 9443:9443 \
     --network apisix \
     --ip 172.18.5.11 \
     -d apache/apisix
  6. Build a Docker image from locally customized or patched source code

    master

    If you have modified the APISIX source code locally, use the provided Dockerfile.local located in the debian-dev/ directory to build your development image.

    docker build -t apisix-dev-local -f /path/to/debian-dev/Dockerfile.local  .
  7. Configure APISIX services, routes, and SSL

    master

    You can configure APISIX using its Admin API. The following examples demonstrate how to create services with round-robin upstreams, map routes to those services based on hostnames, and configure SSL certificates for subdomains.

    Note: All requests require the X-API-KEY header. In this example, the key is edd1c9f034335f136f87ad84b625c8f1.

    # Create service 1 with web1 upstream
    curl http://127.0.0.1:9180/apisix/admin/services/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{
        "upstream": {
            "type": "roundrobin",
            "nodes": {
                "web1:80": 1
            }
        }
    }'
    
    # Create route 12 for host web1.lvh.me linked to service 1
    curl http://127.0.0.1:9180/apisix/admin/routes/12 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{
        "uri": "/*",
        "host": "web1.lvh.me",
        "service_id": "1"
    }'
    
    # Configure SSL for lvh.me
    curl http://127.0.0.1:9180/apisix/admin/ssl/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d "{\n    \"cert\": \"$( cat './mkcert/lvh.me+1.pem')\",\n    \"key\": \"$( cat './mkcert/lvh.me+1-key.pem')\",\n    \"sni\": \"lvh.me\"\n}"
  8. Run Apache APISIX using etcd with Docker Bridge Network

    master

    This solution uses a dedicated Docker bridge network to isolate etcd and APISIX. You must create a configuration file (config.yaml) that specifies the etcd host and sets admin.allow_admin to the subnet address of the Docker network to allow administrative access.

    # 1. Create a network and start etcd
    docker network create apisix-network --driver bridge && \
    docker network inspect -v apisix-network && \
    docker run -d --name etcd \
      --network apisix-network \
      -p 2379:2379 \
      -p 2380:2380 \
      -e ALLOW_NONE_AUTHENTICATION=yes \
      -e ETCD_ADVERTISE_CLIENT_URLS=http://127.0.0.1:2379 \
      bitnamilegacy/etcd:3.5.11
    
    # 2. Create config.yaml (Note: replace 0.0.0.0/0 with your subnet address for security)
    cat << EOF > $(pwd)/config.yaml
    deployment:
      role: traditional
      role_traditional:
        config_provider: etcd
      admin:
        allow_admin:
          - 0.0.0.0/0
      etcd:
        host:
          - "http://etcd:2379"
        prefix: "/apisix"
        timeout: 30
    EOF
    
    # 3. Start APISIX referencing the config file
    docker run -d --name apache-apisix \
      --network apisix-network \
      -p 9080:9080 \
      -p 9180:9180 \
      -v $(pwd)/config.yaml:/usr/local/apisix/conf/config.yaml \
      apache/apisix
  9. Run etcd server for APISIX

    master

    Run an etcd container to serve as the configuration center for Apache APISIX. This command assigns a static IP (172.18.5.10) within the apisix network to ensure the APISIX container can reliably connect to it. It also enables ALLOW_NONE_AUTHENTICATION for simplified setup.

    docker run -it --name etcd-server \
    -v `pwd`/example/etcd_conf/etcd.conf.yml:/opt/bitnami/etcd/conf/etcd.conf.yml \
    -p 2379:2379 \
    -p 2380:2380  \
    --network apisix \
    --ip 172.18.5.10 \
    --env ALLOW_NONE_AUTHENTICATION=yes bitnami/etcd:3.4.9
  10. Manual deployment of Apache APISIX via Docker

    master

    To deploy Apache APISIX manually using Docker, you must first create a dedicated bridge network, then run an etcd server to act as the configuration center, and finally run the APISIX server container.

    Prerequisites:

    • Ensure you have a configuration file for etcd located at example/etcd_conf/etcd.conf.yml.
    • Ensure you have an APISIX configuration file located at example/apisix_conf/config.yaml. You must verify or modify the etcd address in this file to match the IP assigned to the etcd container (e.g., http://172.18.5.10:2379).

    Windows Note: When using Windows, you must use absolute paths for volume mounts (e.g., -v /e/github/docker-apisix/example/apisix_conf/config.yaml:/usr/local/apisix/conf/config.yaml).

    # 1. Create the network
    docker network create \
      --driver=bridge \
      --subnet=172.18.0.0/16 \
      --ip-range=172.18.5.0/24 \
      --gateway=172.18.5.254 \
      apisix
    
    # 2. Run etcd server
    docker run -it --name etcd-server \
    -v `pwd`/example/etcd_conf/etcd.conf.yml:/opt/bitnami/etcd/conf/etcd.conf.yml \
    -p 2379:2379 \
    -p 2380:2380  \
    --network apisix \
    --ip 172.18.5.10 \
    --env ALLOW_NONE_AUTHENTICATION=yes bitnami/etcd:3.4.9
    
    # 3. Run Apache APISIX server
    docker run --name test-api-gateway \
     -v `pwd`/example/apisix_conf/config.yaml:/usr/local/apisix/conf/config.yaml \
     -v `pwd`/example/apisix_log:/usr/local/apisix/logs  \
     -p 9080:9080 \
     -p 9091:9091  \
     -p 9443:9443 \
     --network apisix \
     --ip 172.18.5.11 \
     -d apache/apisix