'Using Stripe CLI correctly in docker

I would like to use Stripe CLI in docker. I have to mention that I am new to docker. I have loaded stripe/stripe-cli from dockerHub.

docker run --rm -it stripe/stripe-cli:latest.

docker image ls also shows me that the image is available locally. But docker ps does not show me a stripe CLI container? Is this right or wrong?

When I run docker run -it stripe/stripe-cli login

I have to authenticate with Stripe in the browser. This is the output:

Your pairing code is: xxxx-xxxx-xxx-xxxxx
This pairing code verifies your authentication with Stripe.
To authenticate with Stripe, please go to: https://dashboard.stripe.com/stripecli/confirm_auth?t=5ifDAxXxXxXxXxX
⣾ Waiting for confirmation... > Done! The Stripe CLI is configured for Mike's Test Stripe with account id add1_xxx

Please note: this key will expire after 90 days, at which point you'll need to re-authenticate.

My que I do that successfully but I still can't use stripe-cli afterwards? What am I missing?



Solution 1:[1]

I think there should be a docker compose solution too here. This is my working setup:

Framework: Django 4

Python: 3.9

version: "3.9"
   
services:
  stripe-cli:
    image: stripe/stripe-cli
    container_name: stripe-cli
    command: "listen --api-key ${STRIPE_API_KEY} --device-name ${STRIPE_DEVICE_NAME} --forward-to web:8000/payment/webhook/"
    env_file:
      - stripe_cli.env
  
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    

Contents of .env file (remember this should stay locally on the machine. Dont commit this file. create it locally)

STRIPE_API_KEY = rk_test_somelongkey***************
STRIPE_DEVICE_NAME = Name

Please note a few things:

In "web:8000", 'web' is service name of the django project

Solution 2:[2]

To retain configuration you need to use bind mounts (-v attribute). On https://stripe.com/docs/cli/login you can see that configuration is stored inside .config/stripe/config.toml so we can mount ~/.config/stripe from our local disk

docker run --rm -v ~/.config/stripe:/root/.config/stripe -it stripe/stripe-cli:latest login

Your pairing code is: asd-asd
This pairing code verifies your authentication with Stripe.
To authenticate with Stripe, please go to: https://dashboard.stripe.com/stripecli/confirm_auth?t=asdasdasdasd
? Waiting for confirmation... > Done! The Stripe CLI is configured for asd with account id acct_asd

Please note: this key will expire after 90 days, at which point you'll need to re-authenticate.

so now we can repeat without need to log in again

docker run --rm -v ~/.config/stripe:/root/.config/stripe -it stripe/stripe-cli:latest customers list

Solution 3:[3]

If your purpose is simply testing your webhooks do what their documentation says:

Since Docker containers are ephemeral, stripe-cli login cannot be used without defining a data volume. You can use the --api-key flag instead.

docker run --rm -it stripe/stripe-cli:latest listen --api-key sk_test_XXXXXX --forward-to localhost/stripe_hook

Solution 4:[4]

docker run's --rm option indicates that the container will be removed after it exits. And commands do exit unless it listens to something e.g. listen.

Note that the default entrypoint of this docker image is /bin/stripe; that is, it expects one of the stripe cli command.

e.g. docker run --rm stripe/stripe-cli:latest customers list (as in stripe customers list otherwise)

Looking at the way its Dockerfile is written, I suppose it's intended to be used as one-off command execution as opposed to bash-kind of interactive platform.

A simple solution seems to be the use of STRIPE_API_KEY or --api-key option. Set this env var and you're good to go for any command, even without login.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arindam Roychowdhury
Solution 2 duleorlovic
Solution 3 Nick Roz
Solution 4 Hiro