Reputation: 2481
I am trying to copy files from a host that runs Debian/GNU Linux v9 to a docker container's working directory. The container is balenalib/beaglebone-black with some additions.
From this question, I understand the syntax to copy via docker cp
. I have tried this: docker cp hostfile container_id:~/destinationfile
. However, that obviously doesn't work because home directory may not be root directory.
How do I specify to copy the file into the docker container's working directory?
Upvotes: 2
Views: 2920
Reputation: 39294
TL;DR;
docker cp hostFile containerId:$(docker inspect --format='{{.Config.WoringkDir}}' containerId)/hostFile
Here is what I found and how I found it:
As my comment stated it, I was pretty sure that docker inspect
would have that kind of information.
From my experience, I know docker inspect
gives out some JSON data and that there is a way to format that output.
So I launched a httpd
container (because I know it has a working dir set), and just ran a unfiltered docker inspect
on it.
Getting the container in a detached state
$ docker run -d httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
f7e2b70d04ae: Pull complete
84006542c688: Pull complete
dae6fe3c5e81: Pull complete
33fc493aff90: Pull complete
9a4113020573: Pull complete
Digest: sha256:20ead958907f15b638177071afea60faa61d2b6747c216027b8679b5fa58794b
Status: Downloaded newer image for httpd:latest
5d85e4926b578e15abdb92650a56f6507d01f04a31de33f179583eccf0a3a6c3
Finding its friendly name
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d8e4926b57 httpd "httpd-foreground" 12 seconds ago Up 11 seconds 80/tcp friendly_murdock
A quick grep to insure what we are looking for is there*
$ docker inspect friendly_murdock | grep Work
"WorkDir": "/var/lib/docker/overlay2/c3b938dfd53dd42370953ac6bfc04bce444588ca449a43b922e0ba41b189102b/work"
"WorkingDir": "/usr/local/apache2",
Getting the full information to see where the info is in the JSON hierarchy; that points at the fact that WorkingDir
is under Config
(scroll around 3/4 of the JSON to discover it by yourself)*
$ docker inspect friendly_murdock
[
{
"Id": "5d85e4926b578e15abdb92650a56f6507d01f04a31de33f179583eccf0a3a6c3",
"Created": "2019-03-13T20:37:31.489661896Z",
"Path": "httpd-foreground",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2051,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-03-13T20:37:31.961474223Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:2d1e5208483c26822b518c4ffa34ce1cd960f3e90e9be6ffe4c52cc6f5d5492c",
"ResolvConfPath": "/var/lib/docker/containers/5d85e4926b578e15abdb92650a56f6507d01f04a31de33f179583eccf0a3a6c3/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/5d85e4926b578e15abdb92650a56f6507d01f04a31de33f179583eccf0a3a6c3/hostname",
"HostsPath": "/var/lib/docker/containers/5d85e4926b578e15abdb92650a56f6507d01f04a31de33f179583eccf0a3a6c3/hosts",
"LogPath": "/var/lib/docker/containers/5d85e4926b578e15abdb92650a56f6507d01f04a31de33f179583eccf0a3a6c3/5d85e4926b578e15abdb92650a56f6507d01f04a31de33f179583eccf0a3a6c3-json.log",
"Name": "/friendly_murdock",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/c3b938dfd53dd42370953ac6bfc04bce444588ca449a43b922e0ba41b189102b-init/diff:/var/lib/docker/overlay2/bffe6a5f69cd186be0fbe6ac4d1a18c9f33a59eb1d2dfc4704fc0b5d9202d565/diff:/var/lib/docker/overlay2/13828a5afe316d7a37ded8f3c42e05ceb715ac494773ebe285e0a458edc6f5eb/diff:/var/lib/docker/overlay2/9e4ef1c4b0bdb2bd958bceefe3ca911ec70613445dc58e600237110ecd938226/diff:/var/lib/docker/overlay2/31b84ae126e4483a5dea9476d076a6a3dd21833f4e27777512a5302157519a3c/diff:/var/lib/docker/overlay2/982c67754fe156ee05e0675889cd648f1c4f8c4a115cf0ab0c83bdb457228845/diff",
"MergedDir": "/var/lib/docker/overlay2/c3b938dfd53dd42370953ac6bfc04bce444588ca449a43b922e0ba41b189102b/merged",
"UpperDir": "/var/lib/docker/overlay2/c3b938dfd53dd42370953ac6bfc04bce444588ca449a43b922e0ba41b189102b/diff",
"WorkDir": "/var/lib/docker/overlay2/c3b938dfd53dd42370953ac6bfc04bce444588ca449a43b922e0ba41b189102b/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "5d85e4926b57",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/apache2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"HTTPD_PREFIX=/usr/local/apache2",
"HTTPD_VERSION=2.4.38",
"HTTPD_SHA256=7dc65857a994c98370dc4334b260101a7a04be60e6e74a5c57a6dee1bc8f394a",
"HTTPD_PATCHES=",
"APACHE_DIST_URLS=https://www.apache.org/dyn/closer.cgi?action=download&filename= \thttps://www-us.apache.org/dist/ \thttps://www.apache.org/dist/ \thttps://archive.apache.org/dist/"
],
"Cmd": [
"httpd-foreground"
],
"ArgsEscaped": true,
"Image": "httpd",
"Volumes": null,
"WorkingDir": "/usr/local/apache2",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "971f52a7ce81d0c5ceb1e501101ea852d5a9898c57c5f6bbe4b96c9efcc016db",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": null
},
"SandboxKey": "/var/run/docker/netns/971f52a7ce81",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "07e63ff9d1c53ad639a9d2bc065cd901afb00024395d22527a2d0297a98691fb",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "8c894781eecadaf63f14ad1da23c26abb42ef3316f7d6ebd1c89b3adac96d99c",
"EndpointID": "07e63ff9d1c53ad639a9d2bc065cd901afb00024395d22527a2d0297a98691fb",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
Finding the right command with the help of the documentation examples
$ docker inspect --format='{{.Config.WorkingDir}}' friendly_murdock
/usr/local/apache2
Combine it whit what we know already*
docker cp hostFile containerId:$(docker inspect --format='{{.Config.WoringkDir}}' containerId)/hostFile
Upvotes: 3
Reputation: 1752
You can use a volume to copy a file from host to container as follows:
docker run -it --volume $PWD/directory:/tmp/dir balenalib/beaglebone-black
Note that you cannot use relative paths to mount volumes on cli form. To do that use docker-compose instead:
version: "3"
services:
beaglebone:
image: balenalib/beaglebone-black
volumes: "../dir:/tmp/dir"
...
Then run docker-compose:
docker-compose up
Check the docs on volume types.
Upvotes: 1