Getting Logs from a Docker Container

We’re looking for a way to get the logs back from a docker container run. We deploy the main application container then run some functional tests against it from another container. Is there a way to get those docker logs back into the octopus log?

I can’t see a way to run a container without the ‘-d’ flag so I can’t grab them with ‘-it’.

The only way I can think of is to run a second command “docker logs <conatiner-id/name>” after the test run to get the logs… But that presents more questions.
How do I know the container id/name from the previous step?
How do I know how long to wait? I guess I can put the logs command in a loop, so that I wait for an ‘exited’ status.

Before I go through the headache of that solution, I would like to know if there’s a better way that I’m just missing.

Hi Joshua, thanks for reaching out.

You can get the details of a Docker container deployed in a previous step through the [Octopus.Action[The step name goes here].Output.Docker.Inspect] variable.

In the screenshot below I have used the instructions at https://octopus.com/docs/support/debug-problems-with-octopus-variables#DebugproblemswithOctopusvariables-Writethevariablestothedeploymentlog to print the variables to the log in a script step that is run after the Docker run step. You can see that the highlighted variable is a JSON object that contains the details of the container that was run.

Using this information you can interact with the container to view the log files and perform whatever other processing you need to do.

Regards
Matt C

Thanks for the information. If anyone wants the script I used…

CID=#{Octopus.Action[Run tests].Output.Docker.Inspect.Id}
STATUS='running' 
while [ $STATUS = "running" ]; do
  sleep 1
  STATUS=$(docker inspect --format='{{.State.Status}}' $CID)
done
docker logs $CID > test-results.log
new_octopusartifact "$(pwd)/test-results.log" $(hostname)-test-results.log