Containers
Copy files to and from a container
- Copy an
nginx.conf
file from the host current directory to/etc/nginx
directory innginx-container
:
docker cp nginx.conf nginx-container:/etc/nginx/nginx.conf
/etc/nginx/nginx.conf
file in nginx-container
will be owned by the container root
user, root
group. If /etc/nginx/nginx.conf
file exists in nginx-container
, it will be overwritten.
- Copy an
nginx.conf
file, owned bytest
user,test
group from the host current directory to/etc/nginx
directory innginx-container
, setting its ownership totest
user,test
group:
docker cp -a nginx.conf nginx-container:/etc/nginx/nginx.conf
- There is a symbolic link
nginx.conf-link
in the current directory, which points to thenginx.conf
file. Copy thenginx.conf-link
symbolic link to/etc/nginx
directory innginx-container
:
docker cp nginx.conf-link nginx-container:/etc/nginx/nginx.conf-link
The nginx.conf-link
symbolic link is copied.
- Following the example 3, copy the
nginx.conf
file via thenginx.conf-link
symbolic link:
docker cp -L nginx.conf-link nginx-container:/etc/nginx/nginx.conf
With -L
option indicated, docker cp
copies the nginx.conf
pointed to by the nginx.conf-link
symbolic link.
- Copy
html
directory to/usr/share/nginx
directory innginx-container
. An elegant solution usingtar
:
tar -cf - html | docker exec -i nginx-container tar -C /usr/share/nginx -xf -
Explanation:
tar -cf -
, so-
is used as the archive name, means that the created archive is written to standard output,tar -xf -
means that the archive is read from the standard input,tar -C /usr/share/nginx
means that the archive is extracted to the/usr/share/nginx
directory.
Return to the host terminal without exiting container
If you have created a container by executing a docker run
command, for example:
docker run -it nginx:alpine ash
After you type exit
, the container main process terminates and the container status changes to Exited(0)
.
If you want to leave the container running, but return to the host machine terminal, type Ctrl+pq
.
Execute docker ps
and you will see that the container is still running.
Run a script in container and redirect output to a file on the host machine
Execute scriptInContainer.sh and redirect output to the script.out file:
docker exec testContainer bash -c ". /tmp/scriptInContainer.sh" > script.out
More details on this example: Run a script in container and redirect output to a file on the host machine