Docker Config Reference (docker exec)

We go over many examples of using docker exec.

Docker Config Reference (docker exec)
docker exec example guide

Example 1

docker exec mycontainer ls /app

This command executes the "ls /app" instruction within the running container "mycontainer", listing the contents of the "/app" directory, which is applicable for inspecting file structures during runtime without attaching interactively.

Example 2

docker exec -it mycontainer /bin/bash

This command initiates an interactive bash shell (-it) in "mycontainer", allocating a pseudo-TTY for user input and output, facilitating manual debugging or configuration adjustments in an active environment.

Example 3

docker exec -d backgroundcont touch /tmp/file.txt

This command runs "touch /tmp/file.txt" in detached mode (-d) within "backgroundcont", creating a file without blocking the terminal, suitable for non-interactive background operations.

Example 4

docker exec -u root privilegedcont rm /protected/file

This command executes "rm /protected/file" as the root user (-u root) in "privilegedcont", enabling administrative actions that require elevated privileges.

Example 5

docker exec -e VAR=value envcont echo $VAR

This command sets an environment variable "VAR=value" (-e) and echoes it in "envcont", demonstrating dynamic variable injection for testing configurations.

Example 6

docker exec --privileged syscont sysctl -w net.ipv4.ip_forward=1

This command runs "sysctl -w net.ipv4.ip_forward=1" with privileged access (--privileged) in "syscont", modifying kernel parameters that necessitate extended capabilities.

Example 7

docker exec a1b2c3d4e5f6 cat /etc/hosts

This command uses a partial container ID "a1b2c3d4e5f6" to display the contents of "/etc/hosts", providing flexibility when container names are not readily available.

Example 8

docker exec -w /app workdircont npm install

This command sets the working directory to "/app" (-w) and runs "npm install" in "workdircont", ensuring commands execute in the appropriate path for application dependencies.

Example 9

docker exec -it dbcont mysql -u root -p

This command launches an interactive MySQL client (-it) in "dbcont", prompting for a password, which is essential for database administration tasks.

Example 10

docker exec logcont tail -f /var/log/app.log

This command follows the log file "/var/log/app.log" in "logcont", enabling real-time monitoring of application logs for troubleshooting.

Example 11

docker exec -u appuser usercont whoami

This command runs "whoami" as "appuser" (-u) in "usercont", verifying user identity in non-root executions for security compliance.

Example 12

docker exec -e DEBUG=true debugcont python script.py

This command injects "DEBUG=true" (-e) and executes "python script.py" in "debugcont", activating debug mode for script testing.

Example 13

docker exec --privileged devicecont lsblk

This command lists block devices with privileged access (--privileged) in "devicecont", allowing hardware inspection requiring system privileges.

Example 14

docker exec netcont ping google.com

This command performs a ping test in "netcont", assessing network connectivity from within the container's namespace.

Example 15

docker exec -d croncont crontab /new/cronjobs

This command installs a new crontab in detached mode (-d) in "croncont", updating scheduled tasks without foreground interaction.

Example 16

docker exec -it shellcont sh

This command starts an interactive sh shell (-it) in "shellcont", providing a lightweight alternative to bash for minimal environments.

Example 17

docker exec configcont sed -i 's/old/new/g' /config/file.conf

This command edits "/config/file.conf" in-place in "configcont", enabling runtime configuration changes via sed.

Example 18

docker exec -u 1000:1000 groupcont id

This command runs "id" as user/group 1000:1000 (-u) in "groupcont", confirming effective user and group identifiers.

Example 19

docker exec -e PATH=/custom/bin:$PATH pathcont which tool

This command modifies the PATH variable (-e) and locates "tool" in "pathcont", testing custom binary paths.

Example 20

docker exec --privileged mountcont mount /dev/sdb1 /mnt

This command mounts a device with privileged access (--privileged) in "mountcont", handling storage attachments.

Example 21

docker exec monitorcont top -b -n 1

This command runs a single iteration of top in batch mode in "monitorcont", capturing resource usage snapshots.

Example 22

docker exec -w /data datacont rm -rf tempfiles/

This command sets "/data" as working directory (-w) and deletes "tempfiles/" in "datacont", cleaning up data directories.

Example 23

docker exec -it pythoncont python -i

This command starts an interactive Python REPL (-it) in "pythoncont", for dynamic code testing.

Example 24

docker exec queuecont rabbitmqctl list_queues

This command lists queues in "queuecont" using rabbitmqctl, monitoring message queue states.

Example 25

docker exec -d updatecont apt update

This command runs "apt update" detached (-d) in "updatecont", performing package updates in the background.

Example 26

docker exec -u nobody securecont ls /secure

This command lists "/secure" as user "nobody" (-u) in "securecont", minimizing privileges for sensitive operations.

Example 27

docker exec -e TZ=UTC timecont date

This command sets timezone "TZ=UTC" (-e) and displays the date in "timecont", testing time configurations.

Example 28

docker exec --privileged netconfigcont ip link set dev eth0 up

This command activates a network interface with privileged access (--privileged) in "netconfigcont".

Example 29

docker exec reportcont generate-report.sh

This command executes a custom script "generate-report.sh" in "reportcont", automating report generation.

Example 30

docker exec -it editorcont vi /edit/file.txt

This command opens vi editor interactively (-it) in "editorcont" for "/edit/file.txt", allowing in-container file editing.