How to authorize doctl in a script

Linux box 18.04, starts a droplet using images stored in a DO registry. I am creating the droplet, updating it, then copying all my assets into it ( including my docker-compose.YAML and everything needed to start the app). ( current end goal is to see my API at an address/port. I can’t pull my images out of the registry ( yet ) without installing doctl, authorizing it ( which includes pasting my API key at a prompt ) then logging into the machine, then I can run my docker-compose up, and everything goes as planned. I want to automate the entire process. My root image is alpine, I would rather not install bash, but if it helps this problem go away I will LOL. Any advice is appreciated!!


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

There are a few ways that you can acomplish this using environment variables:

  1. If you pass the --access-token flag when configuring auth. The command will run without needing user input:

    doctl auth init --access-token $DO_TOKEN

Note that this will print the token to STOUT. So if you are logging the script, you might want to redirect that so your token can not be viewed in logs:

doctl auth init --access-token $DO_TOKEN> /dev/null
  1. You could also write a configuration file containing the token manually:

    echo “access-token: $DO_TOKEN” > ~/.config/doctl/config.yaml

  2. The --access-token flag works with all commands, you could just always pass it when using doctl:

    doctl compute droplet create
    –image ubuntu-20-04-x64
    –size s-1vcpu-1gb
    –region nyc1
    –access-token $DO_TOKEN