Docker execute a command and store the output to a variable

Posted on February 22, 2023 in Docker

How to exectute a command inside a docker and write the output to a variable

This command bringsup a docker and exectes the command inside the docker and stores the output into client

client =$(docker exec command)

Example : This command bringsup a docker and exectes the command inside the docker and stores the output into client

``` client=$(docker-compose -f quickstart.yml exec hydra \ hydra create client \ --endpoint http://127.0.0.1:4445/ \ --format json \ --grant-type client_credentials)

```

Notes :

We can parse the JSON response using jq to get the client ID and client secret:

client_id=$(echo $client | jq -r '.client_id') client_secret=$(echo $client | jq -r '.client_secret')

JQ is a lightweight and flexible command-line JSON processor. Please refer https://stedolan.github.io/jq/ for more info