MongoDB docker container "Failed to unlink socket file"
Asked Answered
A

5

5

I have MongoDB as docker container When I restarted the server and tried to start the container it kept restarting forever.

I got the container log as follows

enter image description here

The main problem is Failed to unlink socket file /tmp/mongodb-27017.sock Operation not permitted

I found many solutions to this problem, but all of them are based on the idea that I'm using the mongod service, which isn't my case.

I'm using a docker container and I can't run commands on this container until it is running. Sadly this error doesn't let the container start and keeps restarting.

I found the issue on the official bug tracker of MongoDB since version 3.6, but also it doesn't have any solution.

I also found a similar question on DigitalOcean support with no luck to find a solution.

Azaleah answered 30/3, 2021 at 13:4 Comment(0)
S
5

Warning!! When you create a MogoDB container, You should always link the directory /data/db inside your container to be on the host using Volumes.

In case /data/db isn't linked to a volume, you will lose your data.

Try to stop the container using docker stop <container>

then remove it completely using docker rm <container>

then re-build using docker build or docker-compose build depends on your usage

Note: Whenever you remove the container it's data will be removed along with it. so make sure that you have a recent backup with the database.

Subacute answered 30/3, 2021 at 13:30 Comment(1)
Thank you @Abdelrahman Abdelhamed this solution worked for me.Azaleah
K
2

I'm wondering how after almost 2 years there is no good answer except "remove and create again".

Assume that your service name for the MongoDB container is mongo and the image tag you're using is 4.4.18.

Create directory docker/mongo under your project's path where your docker-compose.yml is placed.

Create executable docker/mongo/entrypoint.sh with contents:

#!/bin/sh

if [ -z "$@" ];
then
    echo "Remove leftover socket file"
    rm /tmp/*.sock

    echo "Running mongo"
    mongod
else
    eval $@
fi

Create docker/mongo/Dockerfile with contents:

FROM mongo:4.4.18

COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

Modify your docker-compose.yml file, remove image: and add build: instead:

services:
  mongo:
    build: docker/mongo

Launch docker-compose up and you're done.

Kinakinabalu answered 28/12, 2022 at 13:44 Comment(2)
I had to make the /entrypoint.sh file executable. I added this line after COPY entrypoint.sh /entrypoint.sh line: RUN chmod +x /entrypoint.shIntuition
Thank you for the tip. I've added info that entrypoint.sh should be executable. I think there is no need to add additional layer to the container just to make a single file executable.Kinakinabalu
C
0

I had this problem and i'd solved with docker-compose down and then docker-compose up. This problem is probably caused because your docker container is broken

Colchicum answered 17/9, 2021 at 19:2 Comment(2)
Warning! You will lose your data if you use docker-compose downBartholemy
I will only lose the data if I hadn't link a volume from outside to inside the docker container (what is a terrible mistake if the data were important). But it's a pretty good warning, to check if the link has been done. @EdBarahonaColchicum
V
0

I ran into this problem when running Overleaf with docker compose. For me the problem was not solved by taking the containers down an bringing them back up again.

What worked for me instead was deleting the mongod.lock file and then restarting. With Overleaf, this file is stored in ./overleaf-toolkit/data/mongo/mongod.lock.

Vitriform answered 23/12, 2022 at 9:16 Comment(0)
T
-1

The problem is that mongo listens on unix socket by default and macos kernel has different idea about unix socket, not like sane kernels out there. So you need specifically disable it:

command: >
  --nounixsocket
  --bind_ip 127.0.0.1
Trentontrepan answered 9/5, 2024 at 14:36 Comment(1)
This isn't MacOS issue, I faced the issue on a MongoDB docker container running on Ubuntu.Azaleah

© 2022 - 2025 — McMap. All rights reserved.