Print timestamps in Docker Compose logs - docker

I am seeing my web logs with docker-compose logs web, but I would like to add timestamps to those messages, because now they're quite out of context (don't know when did the event happen). I tried docker-compose logs -t web, but it seems Docker Compose is unaware of this flag.
Do you have any idea how can I make timestamps appear in Docker Compose logs?

docker-compose version 1.8.0 supports logs, you can use:
docker-compose logs -t

docker-compose now supports the -t argument, as pointed out by Ittiel.

Related

How to reduce output in `docker-compose` up command

I'm looking for a way to reduce the output generated by docker compose up.
When running in CI all the "interactive" output for download and extract progress is completely useless and generate lots of useless text.
docker has --quiet but I don't see the same for docker compose.
There is a --quiet-pull option that lets you reduce the output generated docker compose up and docker compose run
docker compose up --quiet-pull
You can always run the docker compose in a detached mode with the -d parameter and then check logs of the service/container you want with docker logs --follow <container>
There was an option to set the log-level with --log-level [DEBUG, INFO, WARNING, ERROR, CRITICAL] but it is deprecated from version 2.0.

Docker-compose not show logs

I'm runing my api of express and mongo with docker-compose using the command docker-compose up, all fine but when i try show the logs have the next output error:
With docker-compose logs you need to use the name of the service in the docker-compose.yaml not the name of the container.
You ran docker-compose logs -f backend_api_1, which is the name of the container. If your docker-compose file does not contain any special renaming, the following should work: docker-compose logs -f backend_api (assuming the service is called backend_api)
This is a common confusion point with docker-compose orchestration. Docker compose deals with services, which then can start one or more containers for a service.
You can clarify this for yourself by looking at the manual page for whatever command you plan to use, as it will tell you whether it requires a service name or a container name.
For docker-compose logs the manual shows:
Usage: logs [options] [SERVICE...]
Since we don't have your docker-compose.yaml to refer to, we can only infer that you may have named the service backend_api. I'm just repeating the answer provided by Dennis van de Hoef, which is a reasonable guess based on how docker will name containers for you.
docker-compose logs -f backend_api
The docker logs command can be used to look at the logs of a container.
docker logs -f backend_api_1

How to log a given instance of a scaled service with docker-compose?

I have a docker-compose.yml file which contains, among others, an application service app.
When I start this service using the up --scale app=N option (with N>1) I can monitor what's happening in the service using docker-compose logs app but this list all the instances of the app service, namely, app_1, app_2, ..., app_N.
Let's say I'd like to only see the logs of the app_4. How could I achieve that?
The help doesn't help me much on that:
ubuntu-host-machine$ docker-compose logs --help
View output from containers.
Usage: logs [options] [--] [SERVICE...]
Options:
--no-color Produce monochrome output.
-f, --follow Follow log output.
-t, --timestamps Show timestamps.
--tail="all" Number of lines to show from the end of the logs
for each container.
I naturally tried docker-compose logs app_4 but without much success, as it says:
ERROR: No such service: app_4
The same error appears if I want, e.g. to stop the app_4 using docker-compose stop app_4 and will probably for other similar commands. So the question may also apply to these similar situations.
Versioning information:
ubuntu-host-machine$ docker-compose --version
docker-compose version 1.27.4, build 40524192
ubuntu-host-machine$ docker --version
Docker version 20.10.8, build 3967b7d
ubuntu-host-machine$ uname -mor
5.4.0-81-generic x86_64 GNU/Linux

Is there a difference in docker-compose up --build vs. docker-compose build && docker-compose up

We were trying to build and run docker-compose project on remote host. I tried using:
docker-compose -H 'ssh://remote_address' up --build
And got
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
So we tried:
docker-compose -H 'ssh://remote_address' build
docker-compose -H 'ssh://remote_address' up
Which worked fine. My problem is I can't find evidence in docs for this to be correct behaviour. Is this a bug in docker-compose, a feature, or a bug in my environment?
I'm not sure of the error you got for the first command, I mean docker-compose -H 'ssh://ip' up --build as it may be really a but, but the three mentioned commands have surely differences. I'll try to explain in my simple way:
First command is docker-compose up --build.
This command finds docker-compose file and rebuilds the image then make it running. Suppose you have made some changes into your docker-compose file, so when you only run docker-compose, you'll get a warning that image is not rebuilt, you should run docker-compose up --build to rebuild it and make everything be built again (despite something done before and present in cache).
Second command is docker-compose build.
This command only builds your image based on docker-compose, but does not run it. You can see the built image by docker image ls or docker images. Also executing docker ps -a should not see your recent built image running.
Third and the last command is docker-compose up.
If this command is entered for the first time, it tries to run everything in Dockerfile if exists and download base image, etc. Then makes the image and runs the container.
If the image has been built before, it just runs it.
Unlike the first command, the third one only runs the latest build of that image, but would not build it again.

Containers start with docker-compose up, are listed with docker ps, then don't appear with docker-compose

I've got an issue on ubuntu 18.04 where occasionally running docker-compose up results in the containers starting, the networking between them behaving as expected yet according to docker-compose they aren't there.
docker ps shows the containers exist.
UPDATE: after some comments:
docker-compose ps shows nothing. Also, the problem is intermittent meaning any example is hard to come by unfortunately.
Do you see them in docker ps -a ? You should add docker-compose -d up for detached mode.
Try running
docker-compose ps
from where your .yml file is located. It will show you the list of all docker containers generated from that .yml file

Resources