🐳Docker Shocker

Problem: My logging files are not there in the docker container on stage

Sometimes, the answer is right in front of you. But you search for it for 2 weeks.

This was one such situation. Like many other situations I have encountered in my portal journey. True, every such situation helps me learn something new. Like I would learn about react lifecycles, or in this case, docker containers and volumes.

I was given a task 2 months ago, to capture the error logs into a file. It did get modified over time to capture all logs into a file, not just error. It was a nice and easy one compared to the pinging or data recovery done by my peers.

Yes, it was given 2 months ago. I'm embarrassed really, at my capabilities, but let us focus on my problem at hand rather than going into an existential crisis on my productivity and if I am made for this anyway.

The PROBLEM

My logging files are not there in the docker container on stage

I can see my logs in a file when running on localhost. But since our portal is running on a docker container, it was uncharted territories. I didn't know that my files won't be there like I thought it would.

The MISUNDERSTANDING

I did not know about volumes.

Volumes are used because things in our docker container will not persist on doing a docker stop or compose down. Because our containers change on each docker-compose Our databases should be stored in these volumes. And ideally logs.

Volumes are usually defined in docker-compose.yml file. They have two parts, [host-path]:[container-path]. host-path means the path where you want to store the persistent data, inside your host. It can be your root directory in your machine, or server machine, wherever you have hosted the application. In my case, it was a linux machine hosted on fyre.

So my left side was the place inside the repository directory where I wanted logs. It was easy because our lead had already configured it for databases and backend. Ha!

The difficult part (or it seemed to me) was to find the container-path. This is because

  1. I didn't know the exact path where the container was stored. I assumed it was the same as local, but this is containers we're talking about, so the paths are different.

  2. I didn't see any logs being generated inside the container.

I can forgive 2. I do get confused by stuff. When I navigated inside my containers using docker exec -it <container-name> shI almost never found my logs. But 1, oh how after 4 years of CS degree, I couldn't fix myself on a path.

Solution

Anyway, moral of the story is ->

  1. navigate to your container and find the logs where it is stored. it will be stored somewhere, especially the place where you have set in the .env file. or logger. Assuming you set the logger correctly.

  2. do a pwd in the logs directory. Copy the path

  3. Paste it on the right side of the colon in the volume.

That is it, that is what I was struggling with for 2 weeks. It is okay, I mean, I learnt a lot. I learnt to run a local docker container, and so many docker commands, and could see how a container works up close. But yeah. Don't take 2 weeks. It is embarrassing.

Last updated