1

I have a docker container running mongoDB that is failing to start. I've searched and realised that this is caused due to an issue with mongoDB failing to start in the container.

After searching, the solution to make mongoDB work again seems to be quite simple, as seen in this response: MongoDB Failing to Start - ***aborting after fassert() failure. Apparently, all I have to do is delete the file that is causing mongoDB to crash: /tmp/mongodb-27017.sock.

I've seen responses where you can access the shell in the container docker exec -t -i mycontainer /bin/bash, however the container has to be running.

Is there a way to delete the specific file if the container isn't running?

Oliver Wagner
  • 170
  • 2
  • 10
  • 1
    Can you not just start a new container? Editing existing containers is a bad habit. – John Kugelman Jul 23 '20 at 15:42
  • It is a container with mongoDB and data in it, so starting a new container would imply I lose the data stored in mongoDB, no? – Oliver Wagner Jul 23 '20 at 16:32
  • You'll eventually be forced to delete and recreate the container. This is required to change a number of startup-time options, or to upgrade to a newer version of MongoDB, or to take security updates in the underlying image. – David Maze Jul 23 '20 at 17:06

1 Answers1

0

To run a command into a stopped container what you can do is:

  • commit the container in its current state to a new image
  • start a new container with the command you like

But technically this is a bad practice. Container are supposed to be immutable you could just recreate your container. If the file in in a persistant volume, the docker commit solution would still work through, as well as mounting the volume on another container and work from there.

So let's do it:

#vibrant_tu being the name of a stopped container I have
$ docker commit vibrant_tu
sha256:3381382dc88be0955b76439a74e7d6c41b41ae7217a807d20c4bc92d55e76093
$ docker run -ti 3381382dc88be0955b76439a74e7d6c41b41ae7217a807d20c4bc92d55e76093 sh
# #Do what you want to do in the shell like deleting the file then exit

Please notice that the modification are done only in the new container if you don't have a volume:

$ docker container ls -a
CONTAINER ID        IMAGE                                 COMMAND                     CREATED             STATUS                     PORTS               NAMES
30c3bee77ed2        3381382dc88b                          "sh"                        2 minutes ago       Exited (0) 2 minutes ago                       epic_williamson    
$ docker start 30c3bee77ed2

But this doesn't really make sense at all to do that if the plan was not to modify volume data you could just have created another container.

Still the trick is usefull when you need to inspect a stopped container with a shell.

Nicolas Bousquet
  • 3,990
  • 1
  • 16
  • 18
  • 1
    From the docker documents: "_The commit operation will not include any data contained in volumes mounted inside the container._". I followed what you suggested, but could not find the file to delete inside of the /tpm folder. The whole issue is that I have data inside the container, that I would like to keep. Otherwise I could just create another container with the mongoDB image. – Oliver Wagner Jul 23 '20 at 16:45
  • And why not run your container and mounting the volume when you run it? Of course if you mounted a volume for your previous container, mount it again... – Nicolas Bousquet Jul 23 '20 at 16:49
  • I understand what I did wrong. Ideally I have to use volumes if I want to persist data. This way, I would only have to discard my non working container, create a new one and attach the volume to it and everything would be fine, correct? – Oliver Wagner Jul 23 '20 at 19:43
  • Correct. If you don't use volume, my solution would allows you to see the file, delete it and run your app on the new version of that container. If you use them mount the volume on any container and delete the file. – Nicolas Bousquet Jul 23 '20 at 21:00