Running a bash script on Windows is a pain. Even on Linux, running a script tailored for a different distribution can be a hassle. Docker solves this while keeping your host system clean.
This post uses Alpine as an example to show how to set up an interactive environment to process files in your current directory.
How it works
The core idea is mounting your current directory into a container. Here is the command template:
| |
The parameters are as follows:
| Parameter | Meaning |
|---|---|
| –rm | Remove the container after exiting |
| -it | Combination of interactive and tty; allows interaction and output |
| -v | Path mapping; maps the current directory to /data in the container |
| -w | Sets the working directory, so you start inside /data |
| sh | The command to run (in this case, the shell) |
If you only want to run a single command without staying in an interactive shell, just append the command at the end:
| |
Windows
The commands differ between PowerShell and CMD due to how they handle environment variables. Here is how to open an interactive Alpine shell:
- PowerShell
| |
- CMD
| |
Linux
On Linux, containers run as root by default. To prevent files generated inside the container from being owned by root on your host, it’s best to map your current user’s UID and GID:
| |
MacOS
No special permission handling is usually required. Use the same variables as Linux:
| |
Notes - Alpine
Since Alpine is extremely minimal, you might need to install common utilities yourself. Use the apk add command to install what you need.