+

How to install Anypoint Flex Gateway in connected mode as a Docker container

10 min read
Was this tutorial helpful?
Thank you for your feedback!

This tutorial was updated in August 2022 and is based on Flex Gateway 1.1.0.

You can use Flex Gateway locally or in connected mode. If you use it locally, you will only be able to run it from your local machine, but you can’t manage the APIs from Anypoint Platform. In connected mode, you can view your gateways, your API instances, and policy configurations in API Manager. In this case, we’ll register and run Flex Gateway in connected mode using the Flex Gateway Docker image.

In this tutorial, you’ll learn how to:

  • Install the Flex Gateway Docker image on your local computer
  • Register the gateway in connected mode
  • Start the gateway
  • Add additional replicas
  • Remove replicas


Prerequisites

  • Anypoint Platform - We’ll need an Anypoint Platform account to manage our Flex Gateway. You can sign up for a free trial account here.
  • Flex Gateway permissions - You shouldn’t need to assign any specific permissions to follow this tutorial when using a new free trial account. However, if you experience permission issues, please see Flex Gateway prerequisites.
  • Docker - We’ll need to install Docker to download, install, and run the Flex Gateway image on our local machine. You can download Docker Desktop here.
  • (Optional) Overview and introduction - If you want to first see an overview and introduction to Flex Gateway without a technical how-to, see this tutorial.

Set up Flex Gateway in Runtime Manager

1 - Log in to Anypoint Platform and navigate to Runtime Manager. Click on the Flex Gateways tab on the left and select Add Gateway.

If this is the first time you access Anypoint Platform, it may ask you to select an environment. Choose Sandbox.

Runtime Manager preview

2 - You will see different options to select where to set up your Flex Gateway. Let’s select Docker.

Runtime Manager > Add a gateway

Install the Flex Gateway Docker image

1 - Download the Flex Gateway Docker image by running the following command. We will be using the latest version in this tutorial.

1
docker pull mulesoft/flex-gateway:latest

You can browse the repository in DockerHub (here) to find any version you want to use instead. To use a different version, run docker pull mulesoft/flex-gateway:<version>. For example, docker pull mulesoft/flex-gateway:1.0.0.

2 - Verify the image was correctly installed by running docker images or checking your images from the Docker Desktop app. You should see the new mulesoft/flex-gateway image.

Command-line output:

1
2
REPOSITORY              TAG       IMAGE ID       CREATED      SIZE
mulesoft/flex-gateway   latest    4ba042860c8f   7 days ago   273MB

Docker Desktop preview:

Docker Desktop > Images

Register the gateway in connected mode

1 - Copy the command from Runtime Manager to register your gateway. It already contains the token and organization ID you need to authenticate and connect the gateway with your Anypoint Platform account. You need to replace <gateway-name> with your own name (i.e. my-gateway) and remove the version from the flex-gateway image to use the one you previously downloaded.

It’s better to run this command in a new folder or in a known location to quickly identify the files that will be created.

You should end up with something like the following (with your own token and organization):

Command:

1
2
3
4
5
6
7
docker run --entrypoint flexctl \
-v "$(pwd)":/registration mulesoft/flex-gateway \
register --organization=e04b331c-a0d3-4b0b-b40c-55254205c981 \
--token=4c3fa08c-5d08-431f-ba9a-cb4416db7dc7 \
--output-directory=/registration \
--connected=true \
my-gateway

Output:

1
2
Starting registration, please be patient.
Registration completed, the configuration files were written in directory "/registration". For security, modify the file permissions to restrict production scenario access to the user running flex.

This is using Auth Token as the authentication mechanism for registering the gateway. You can also choose to connect it using a Connected App or your Anypoint Platform Username and Password.

2 - Verify there is a new registration.yaml file in the same directory you executed the command.

3 - If you check the Flex Gateway list in Runtime Manager, your should now see your new gateway (my-gateway) listed with a status of Disconnected.

image

Start the gateway

1 - Run the following command to start your gateway using port 8081.

1
2
3
4
docker run --rm \
-v "$(pwd)":/usr/local/share/mulesoft/flex-gateway/conf.d \
-p 8081:8081 \
mulesoft/flex-gateway

You should see an output similar to the following. Note that the command doesn’t finish running. If you kill the process from your local, your gateway will be disconnected.

1
2
3
4
5
6
7
8
...
[flex-gateway-agent][info] Validating gateway
[flex-gateway-agent][info] Generating config
[flex-gateway-agent][info] Gateway default/9fd77888f949: Adding Service default/internal-pe-anypoint-metrics http://127.0.0.1:15100/
[flex-gateway-agent][info] Writing envoy bootstrap configuration to /tmp/envoy.json
[flex-gateway-agent][info] Gateway: Platform=https://anypoint.mulesoft.com OrgID=e04b331c-a0d3-4b0b-b40c-55254205c981 EnvID=36df410e-a800-44ab-be86-853820d277b2 Name=3952d507-3044-4a28-be97-1d89c5d97418 Mode=connected ReplicaName=9fd77888f949.default
[flex-gateway-envoy][info] cds: add 1 cluster(s), remove 3 cluster(s)
[flex-gateway-envoy][info] cds: added/updated 1 cluster(s), skipped 0 unmodified cluster(s)

To run the Docker container in a detached mode, you can add -d at the beginning of the command. Like so: docker run --rm -d ...

2 - If you check the Flex Gateways list in Runtime Manager, you should now see your new gateway (my-gateway) with a status of Connected.

Runtime Manager > Flex Gateways

3 - You can also verify that the container is running in your local with docker container ls in a new command line, or check if there’s a running container in Docker Desktop.

Command-line output:

1
2
CONTAINER ID  IMAGE                   COMMAND   CREATED         STATUS         PORTS                    NAMES
2d55079f848e  mulesoft/flex-gateway   "/init"   6 minutes ago   Up 6 minutes   0.0.0.0:8081->8081/tcp   stupefied_kalam

Docker Desktop preview:

Docker Desktop > Containers / Apps

Add additional replicas

1 - Open a new terminal and run the following command to add a new replica on port 8082.

1
2
3
4
docker run --rm \
-v "$(pwd)":/usr/local/share/mulesoft/flex-gateway/conf.d \
-p 8082:8081 \
mulesoft/flex-gateway

You should see an output similar to the previous command we ran. Same as before, the command doesn’t finish running. If you kill the process from your local, your replica will be disconnected.

1
2
3
4
5
6
7
8
...
[flex-gateway-agent][info] Validating gateway
[flex-gateway-agent][info] Generating config
[flex-gateway-agent][info] Gateway default/35cfc6ea53ed: Adding Service default/internal-pe-anypoint-metrics http://127.0.0.1:15100/
[flex-gateway-agent][info] Writing envoy bootstrap configuration to /tmp/envoy.json
[flex-gateway-agent][info] Gateway: Platform=https://anypoint.mulesoft.com OrgID=e04b331c-a0d3-4b0b-b40c-55254205c981 EnvID=36df410e-a800-44ab-be86-853820d277b2 Name=3952d507-3044-4a28-be97-1d89c5d97418 Mode=connected ReplicaName=35cfc6ea53ed.default
[flex-gateway-envoy][info] cds: add 1 cluster(s), remove 3 cluster(s)
[flex-gateway-envoy][info] cds: added/updated 1 cluster(s), skipped 0 unmodified cluster(s)

To run the Docker container in a detached mode, you can add -d at the beginning of the command. Like so: docker run --rm -d ...

2 - If you check the Flex Gateways list in Runtime Manager, you should now see 2 replicas listed in your gateway (my-gateway), both with a status of Connected.

image

3 - You can also verify that the new container is running in your local with docker container ls in a new command line, or check if there’s a new running container in Docker Desktop.

Command-line output:

1
2
3
CONTAINER ID   IMAGE                   COMMAND   CREATED          STATUS          PORTS                    NAMES
392073434dcf   mulesoft/flex-gateway   "/init"   4 minutes ago    Up 4 minutes    0.0.0.0:8082->8082/tcp   xenodochial_dubinsky
2d55079f848e   mulesoft/flex-gateway   "/init"   16 minutes ago   Up 16 minutes   0.0.0.0:8081->8081/tcp   stupefied_kalam

Docker Desktop preview:

Docker Desktop > Containers / Apps

4 - You can continue adding more replicas with the same command but changing the port number for each replica.

Remove replicas

1 - To remove a replica, you can stop running the docker run command (if you didn’t use detached mode) and this will remove the container. Alternatively, you can run docker container stop <container-name> (i.e., docker container stop stupefied_kalam) or stop the container from Docker Desktop by clicking on the Stop button on the right side of the container.

Docker Desktop > Stop

2 - If you remove all replicas, you can verify no replicas are running with docker container ls or in Docker Desktop.

Command-line output:

1
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

Docker Desktop preview:

Docker Desktop > Containers / Apps

3 - The gateway might still be listed in Runtime Manager but will have a status of Disconnected if no replicas are connected.

Runtime Manager > Flex Gateways

Runtime Manager > Flex Gateways > my-gateway

4 - To delete a gateway from Runtime Manager, click on the 3-dot button next to the gateway and select Delete gateway.

image

The gateway will now have a status of Deleted and will be removed from the UI in 30 days. For more information, see Anypoint Flex Gateway.

Next Steps

In this tutorial, you learned how to:

  • Install the Flex Gateway Docker image on your local computer
  • Register the gateway in connected mode
  • Start the gateway
  • Add additional replicas
  • Remove replicas

In the next tutorial, you’ll learn how to manage your APIs from API Manager using the Flex Gateway you created in this tutorial.

Click on the Next button below to continue to the next tutorial.

Previous Next

Try Anypoint Platform for free

Start free trial