-
( The following presumes you have installed Docker on your machine )
Lets say you want to execute a command using something like the
jq
json parsing program, butjq
is not installed on your machine. You could installjq
, but perhaps for some reason you'd prefer not to. This could be for a variety of reasonsThe
run_in_alpine_with
function in the snippet above can helpIt lets you easily run commands using packages you'd rather not install. It runs your command inside a tiny
alpine
Docker container after installing your required package(s) into it. Any available Alpine packages can be usedYou can add the function to your
~.zshrc
or~.bashrc
After adding it, restart your terminal session
Then you can paste a command like this into your terminal:
run_in_alpine_with jq <<'EOF' echo '{"name": "Alice", "age": 30}' | jq -r '.name' EOF
The example above outputs
Alice
Or you could run it like this:
run_in_alpine_with curl <<'EOF' curl -s https://api.github.com/users/montehurd EOF
Which outputs something like this:
{ "login": "montehurd", "id": 3143487, "node_id": "MDQ6VXNlcjMxNDM0ODc=", "avatar_url": "https://avatars.githubusercontent.com/u/3143487?v=4", "gravatar_id": "", "url": "https://api.github.com/users/montehurd", "html_url": "https://github.com/montehurd", "followers_url": "https://api.github.com/users/montehurd/followers", "following_url": "https://api.github.com/users/montehurd/following{/other_user}", "gists_url": "https://api.github.com/users/montehurd/gists{/gist_id}", "starred_url": "https://api.github.com/users/montehurd/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/montehurd/subscriptions", "organizations_url": "https://api.github.com/users/montehurd/orgs", "repos_url": "https://api.github.com/users/montehurd/repos", "events_url": "https://api.github.com/users/montehurd/events{/privacy}", "received_events_url": "https://api.github.com/users/montehurd/received_events", "type": "User", "site_admin": false, "name": null, "company": "Wikimedia Foundation", "blog": "", "location": "San Francisco, US", "email": null, "hireable": null, "bio": "software engineer @wikimedia", "twitter_username": null, "public_repos": 14, "public_gists": 7, "followers": 23, "following": 3, "created_at": "2012-12-28T17:58:53Z", "updated_at": "2024-08-22T21:18:13Z" }
This demonstrates how to pipe that same command as a string to the function. It can require more quote escaping than the other method though:
echo "curl -s https://api.github.com/users/montehurd" | run_in_alpine_with curl
Here you can see how to install and use multiple packages in the Alpine container:
run_in_alpine_with curl jq <<'EOF' curl -s https://api.github.com/users/montehurd | jq -r '.bio' EOF
Which should output
software engineer @wikimedia