Lin Lee
Lin Lee

Reputation: 253

How to keep log files after deleting docker containers?

I want to collect docker container logs, By default, log files will be deleted when removing container. It cause several logs lost each time i update my service. How to keep log files after removing containers? Or, Is there another way to collect all logs from containers without losing?

Upvotes: 5

Views: 3077

Answers (1)

K8sCat
K8sCat

Reputation: 108

There will be two situations:

  • If your logs are the stdout or stderr, you can save them before removing the container:
docker logs CONTAINER_ID > container.log
  • If your logs are stored in some files, in this case, you can copy them out or mount a directory for them while running the container:
# Copy the logs out to the host
docker copy CONTAINER_ID:/path/to/your/log_file /host/path/to/store

# Mount a directory for them
docker run -d \
-v /host/path/to/store/logs:/container/path/stored/logs \
your-image

Upvotes: 5

Related Questions