Building Temporary Interactive Linux Environments with Docker

📢 This article was translated by gemini-3-flash-preview

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:

1
docker run --rm -it -v "$(pwd)":/data -w /data alpine sh

The parameters are as follows:

ParameterMeaning
–rmRemove the container after exiting
-itCombination of interactive and tty; allows interaction and output
-vPath mapping; maps the current directory to /data in the container
-wSets the working directory, so you start inside /data
shThe 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:

1
docker run --rm -v "$(pwd)":/data -w /data alpine ls -la

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
1
docker run --rm -it -v ${PWD}:/data -w /data alpine sh
  • CMD
1
docker run --rm -it -v %cd%:/data -w /data alpine sh

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:

1
docker run --rm -it -u $(id -u):$(id -g) -v "$(pwd)":/data -w /data alpine sh

MacOS

No special permission handling is usually required. Use the same variables as Linux:

1
docker run --rm -it -v "$(pwd)":/data -w /data alpine sh

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.

This post is licensed under CC BY-NC-SA 4.0 by the author.