19.2.5.4 Exec Admin Command
A focused guide to Exec Admin Command, connecting core concepts with practical Docker and container operations.
Administrative commands executed via docker exec allow operators to perform maintenance, configuration, and management operations inside a running container without stopping it, modifying the image, or creating a new container. These operations range from database administration to process management, configuration reloading, user management, and service health checks — all carried out within the live container environment.
Running as Root for Admin Operations
Many administrative operations require root privileges. If the container's primary process runs as a non-root user, the -u root flag grants root access for the exec'd command only:
docker exec -u root my-container bash
Or for a single command:
docker exec -u root my-container chown -R appuser:appgroup /data
The container continues running as its normal user after the admin command exits.
Database Administration
PostgreSQL
# Connect to the database interactively
docker exec -it postgres-container psql -U postgres
# Run a specific SQL command
docker exec postgres-container psql -U postgres -d mydb -c "VACUUM ANALYZE;"
# Create a database backup
docker exec postgres-container pg_dump -U postgres mydb > backup.sql
# Check database size
docker exec postgres-container psql -U postgres -c "\l+"
# Reload configuration without restart
docker exec postgres-container psql -U postgres -c "SELECT pg_reload_conf();"
MySQL/MariaDB
# Connect interactively
docker exec -it mysql-container mysql -u root -pSECRET
# Run a query
docker exec mysql-container mysql -u root -pSECRET mydb -e "SHOW TABLE STATUS;"
# Create a backup
docker exec mysql-container mysqldump -u root -pSECRET mydb > backup.sql
Redis
# Connect to Redis CLI
docker exec -it redis-container redis-cli
# Run a single command
docker exec redis-container redis-cli DBSIZE
docker exec redis-container redis-cli FLUSHDB
# Check memory usage
docker exec redis-container redis-cli INFO memory
MongoDB
# Connect interactively
docker exec -it mongo-container mongosh
# Run a single operation
docker exec mongo-container mongosh mydb --eval "db.users.countDocuments()"
Configuration Reload
Reloading a service's configuration file without restarting the container avoids downtime:
nginx
# Test configuration validity
docker exec nginx-container nginx -t
# Reload configuration (gracefully rotates workers)
docker exec nginx-container nginx -s reload
Signal-based reload
Many services reload their configuration when they receive SIGHUP. This can be triggered via docker kill --signal SIGHUP or from inside a shell session:
docker exec my-container sh -c "kill -HUP 1"
File and Permission Management
# Change ownership of a directory
docker exec -u root my-container chown -R 1001:1001 /data/uploads
# Change permissions
docker exec -u root my-container chmod 755 /app/scripts/start.sh
# Create a directory
docker exec my-container mkdir -p /app/cache/sessions
# Remove a lock file left by a crashed process
docker exec -u root my-container rm -f /tmp/app.lock
Log Rotation and Maintenance
# Truncate an application log file that has grown too large
docker exec -u root my-container truncate -s 0 /app/logs/debug.log
# Compress old log files
docker exec -u root my-container sh -c "gzip /app/logs/*.log.1"
# Remove logs older than 7 days
docker exec my-container find /app/logs -name "*.log" -mtime +7 -delete
User and Group Management
# Create a new user inside the container
docker exec -u root my-container adduser --disabled-password --gecos "" newuser
# Add a user to a group
docker exec -u root my-container usermod -aG docker newuser
Running Database Migrations
Many deployment workflows run migrations from inside the application container:
docker exec my-app-container ./manage.py migrate # Django
docker exec my-app-container node migrate.js up # Node.js
docker exec my-app-container rails db:migrate # Rails
docker exec my-app-container php artisan migrate # Laravel
Application Cache Clearing
docker exec my-app-container php artisan cache:clear
docker exec my-app-container rails runner "Rails.cache.clear"
docker exec my-app-container python3 manage.py clearcache
Health Verification
# Check the application responds correctly
docker exec my-container curl -s http://localhost:8080/health
# Check application version
docker exec my-container cat /app/VERSION
# Verify a service is listening on the expected port
docker exec my-container sh -c "ss -tlnp | grep 8080"
Service Process Management Inside the Container
Some containers run multiple processes managed by a supervisor. Administrative commands can manage these:
# With supervisord
docker exec my-container supervisorctl status
docker exec my-container supervisorctl restart worker
# With s6 overlay
docker exec my-container s6-svstat /var/run/s6/services/myapp
Scripted Administrative Workflows
For repeatable administrative operations:
#!/bin/bash
CONTAINER="my-database"
echo "Running maintenance on $CONTAINER..."
docker exec $CONTAINER psql -U postgres -c "VACUUM FULL ANALYZE;" -d mydb
docker exec $CONTAINER psql -U postgres -c "REINDEX DATABASE mydb;" -d mydb
echo "Maintenance complete."
This script can be scheduled as a cron job to run regular database maintenance against the running container without any downtime.