Interactive Use of Docker

While Docker is primarily intended as a management system for containerized services, it can also be used for interactive utilities.


Ephemeral Containers

To run an ephemeral container interactively, try:

docker run --interactive --tty --rm alpine:latest

The container will be cleaned up once it stops running.


Running Containers

To open a shell with a running container, try:

docker exec --interactive --tty my-container /bin/sh

A different shell binary can be used, but many images are purposefully slimmed down and will not have anything other than sh(1) installed.


Copying Files

It is possible to pass data in and out of containers with pipes.

Some documents recommend using something like the below for copying files from a container.

docker run --rm httpd:2.4 cat /usr/local/apache2/conf/httpd.conf > my-httpd.conf

Instead, consider using docker cp.

docker cp my-container:/path/to/container/file /path/to/destination/file
docker cp /path/to/host/file my-container:/path/to/destination/file

docker cp generally behaves like cp would, but it should be noted that it internally uses a tar(1) stream.

docker cp my-container:/path/to/container/file /path/to/destination/directory/
docker cp my-container:/path/to/container/directory/ /path/to/destination/directory/
docker cp my-container:/path/to/container/file - | tar x --to-stdout | grep "ERROR"

Note how - is used to indicate that STDOUT is the destination.

The former strategy can still be useful in certain circumstances:

docker exec my-container tar -C /path/to/container/data/directory -czf directory-to-be-archived > /path/to/backup.tar.gz
docker exec my-container tar -C /path/to/container/data/directory -czf directory-to-be-archived | gpg --symmetric --output /path/to/backup.tar.gz.gpg


CategoryRicottone

Docker/InteractiveUse (last edited 2023-04-04 21:47:40 by DominicRicottone)