I run an algorithm using the command "docker-compose up" on a cloud server. One of the services (productionbackend) writes logs as part of its execution, in a location inside its container (./logs/Log_< UTCTIMENOW >.txt).
I know I can use the 'docker logs' command to inspect logs when the service runs, but once it is stopped using 'docker-compose down', logs are lost forever (as the container is destroyed).
So I guess I would like to 'mount' a volume to my docker compose file, and redirect the output of that log.txt file to the ~ directory, outside the container. This is what my directory tree looks like:
I tried all kind of combinations in the docker compose file, without success.
For instance, adding the below failed.
volumes:
- type: volume # Also tried `bind` type and other syntaxes
- ./logs:/productionbackend/logs
.
├── docker-compose.yaml #THIS IS THE DOCKER-COMPOSE FILE STARTING MY SERVICES
├── productionfrontend
│ ├── Dockerfile
│ ├── app
│ │ ├── database.py
│ │ ├── main.py
│ │ └── requirements.txt
│ └── docker-compose.yaml
└── productionbackend
├── Dockerfile
├── backend.py
├── logs
│ ├── Log_03_Jun_14.18.19_GMT.txt
├── requirements.txt
├── settings
│ └── somefile.py
........
Thank you.
For next:
volumes:
- type: volume # Also tried `bind` type and other syntaxes
- ./logs:/productionbackend/logs
I don't know if it's your typo or others, but it seems this grammar is strange, see official grammar
Next I give a minimal example which could work for your reference:
docker-compose.yaml:
version: '3'
services:
backend:
image: alpine
container_name: my_try
volumes:
- ./log_on_host:/productionbackend/logs
command: sh -c 'echo "hi" > /productionbackend/logs/my_log.txt'
And next is the execution history:
shubuntu1@shubuntu1:~/abc$ ls
docker-compose.yaml
shubuntu1@shubuntu1:~/abc$ docker-compose up -d
Creating network "abc_default" with the default driver
Creating my_try ... done
shubuntu1@shubuntu1:~/abc$ docker-compose down
Removing my_try ... done
Removing network abc_default
shubuntu1@shubuntu1:~/abc$ ls
docker-compose.yaml log_on_host
shubuntu1@shubuntu1:~/abc$ cat log_on_host/my_log.txt
hi
From above, you can see although the container my_try has been destroyed, we still can see the log my_log.txt which has the content hi on host. You need to modify yours similar like above minimal example, just FYI.