Docker Config Reference (docker cp)
We go over using the docker cp and give many examples of its usage!
Example 1
docker cp mycontainer:/app/config.ini .
This command copies the file "config.ini" from the "/app" directory inside the running container "mycontainer" to the current directory on the host machine, which is useful for extracting configuration files for review or backup.
Example 2
docker cp /host/path/data.txt mycontainer:/data/
This command transfers the file "data.txt" from the host's "/host/path" to the "/data/" directory within the container "mycontainer", enabling the injection of external data for processing inside the container.
Example 3
docker cp mycontainer:/logs/error.log ~/logs/
This command copies "error.log" from the container's "/logs" to the host's "~/logs/" directory, facilitating log analysis on the host system without entering the container.
Example 4
docker cp script.sh mycontainer:/usr/local/bin/
This command copies the local script "script.sh" to the container's "/usr/local/bin/" path, allowing the addition of custom executables for runtime modifications.
Example 5
docker cp mycontainer:/etc/hosts hosts_backup.txt
This command retrieves the "/etc/hosts" file from "mycontainer" and saves it as "hosts_backup.txt" on the host, supporting network configuration backups.
Example 6
docker cp -a mycontainer:/app/ ./app_backup/
This command copies the entire "/app/" directory from "mycontainer" to "./app_backup/" on the host with archive mode (-a), preserving permissions and ownership for complete directory replication.
Example 7
docker cp update.patch a1b2c3d4e5f6:/patches/
This command copies "update.patch" to the "/patches/" directory in a container identified by its partial ID "a1b2c3d4e5f6", useful when container names are unknown.
Example 8
docker cp mycontainer:/output/results.csv results.csv
This command extracts "results.csv" from the container's "/output" to the host as "results.csv", ideal for retrieving computational outputs from data processing containers.
Example 9
docker cp cert.pem mycontainer:/etc/ssl/certs/
This command transfers "cert.pem" to the container's "/etc/ssl/certs/" for SSL certificate updates, enhancing security in web service containers.
Example 10
docker cp mycontainer:/var/lib/db/data.db local_db.db
This command copies a database file "data.db" from the container to "local_db.db" on the host, enabling offline analysis or backups of persistent data.
Example 11
docker cp -L mycontainer:/symlink/file.txt .
This command copies the file referenced by "/symlink/file.txt" in "mycontainer" to the current directory, following symbolic links (-L) to retrieve the actual content.
Example 12
docker cp config.yml stoppedcontainer:/app/
This command copies "config.yml" to the "/app/" directory in a stopped container "stoppedcontainer", allowing modifications even when the container is not running.
Example 13
docker cp mycontainer:/tmp/tempfile.tmp /backup/
This command retrieves a temporary file "tempfile.tmp" from the container's "/tmp" to the host's "/backup/", useful for capturing transient data.
Example 14
docker cp assets/ mycontainer:/assets/
This command copies the entire local "assets/" directory to the container's "/assets/", supporting bulk asset transfers for media or static files.
Example 15
docker cp mycontainer:/proc/cpuinfo cpuinfo.txt
This command copies the "/proc/cpuinfo" system file from "mycontainer" to "cpuinfo.txt" on the host, for inspecting container hardware emulation.
Example 16
docker cp license.key licensedcont:/licenses/
This command injects "license.key" into "licensedcont"'s "/licenses/", enabling software licensing in restricted containers.
Example 17
docker cp mycontainer:/home/user/.ssh/id_rsa id_rsa_backup
This command extracts a private SSH key from the container to "id_rsa_backup" on the host, for key management or recovery.
Example 18
docker cp env-vars.env mycontainer:/env/
This command copies an environment file "env-vars.env" to the container's "/env/", for dynamic environment configuration.
Example 19
docker cp mycontainer:/var/www/html/index.html .
This command copies a web page "index.html" from the container to the current directory, allowing host-side editing of web content.
Example 20
docker cp -a /host/scripts/ scriptcont:/scripts/
This command archives and copies the "/host/scripts/" directory to "scriptcont"'s "/scripts/", preserving file attributes for executable scripts.
Example 21
docker cp mycontainer:/etc/nginx/nginx.conf nginx.conf
This command retrieves the Nginx configuration from the container to "nginx.conf" on the host, for external tuning.
Example 22
docker cp plugin.jar plugincont:/plugins/
This command transfers "plugin.jar" to "plugincont"'s "/plugins/", extending application functionality with plugins.
Example 23
docker cp mycontainer:/mnt/volume/data /host/volume_backup/
This command copies data from a mounted volume in "mycontainer" to the host, for volume backups.
Example 24
docker cp report.pdf reportcont:/reports/
This command copies "report.pdf" to "reportcont"'s "/reports/", injecting documents for processing or generation.
Example 25
docker cp mycontainer:/usr/bin/customtool customtool
This command extracts a custom binary "customtool" from the container to the host, for tool sharing or analysis.
Example 26
docker cp -L symlinkcont:/linked/file realfile.txt
This command follows a symlink in "symlinkcont" to copy the target "file" as "realfile.txt" on the host.
Example 27
docker cp init.sql dbcont:/init/
This command copies an SQL initialization script "init.sql" to "dbcont"'s "/init/", for database setup.
Example 28
docker cp mycontainer:/opt/app/logs/app.log /logs/
This command copies an application log from the container to the host's "/logs/", for centralized logging.
Example 29
docker cp template.html templatecont:/templates/
This command transfers "template.html" to "templatecont"'s "/templates/", for template-based rendering.
Example 30
docker cp -a mycontainer:/etc/ full_etc_backup/
This command archives and copies the entire "/etc/" directory from "mycontainer" to "full_etc_backup/" on the host, for system configuration backups.