0

I am trying to create a Healthcheck for my MongoDB container configured in my Dockerfile:

FROM ubuntu

RUN apt-get update && apt-get install -y gnupg2

RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' > tee /etc/apt/sources.list.d/mongodb.list

RUN apt-get update

RUN apt-get install -y mongodb

RUN mkdir -p /data/db

EXPOSE 27017

**HEALTHCHECK --interval=5s --timeout=3s CMD /etc/init.d/mongodb status || exit 1**

CMD ["usr/bin/mongod", "--smallfiles"]

But when I build the image and run a container, after running docker ps it shows Up 20 seconds (unhealthy) in the status column.

Going into the container with bash, when I try running service mongodb start it fails.

In the log file (/var/log/mongodb/mongodb.log) it says Failed to set up listener: SocketException: Address already in use

But there is no other container with MongoDB running.

What could be causing this?

rohanmehto2
  • 910
  • 1
  • 7
  • 19
  • How are you running your container? Can you include your `docker run` command? – Robert Seaman Feb 21 '20 at 00:19
  • Also, have you considered using the official MongoDB docker images from dockerhub? https://hub.docker.com/_/mongo – Robert Seaman Feb 21 '20 at 00:22
  • Docker run -d -p 2717:27017 --name example my-mongo-app. Also I can't connect to the app through http (http://localhost:2717) like I would be able to on a working db, I imagine that's part of the same problem – Benny Mestel Feb 21 '20 at 07:11
  • Commands like `service` and `/etc/init.d` scripts don't really work in Docker. They're also not what you really want here; definitionally, if the container is running, the `mongod` process is too, but it's not necessarily accepting connections. – David Maze Feb 21 '20 at 11:26

1 Answers1

0

Maybe a mistake on last line

Change this line CMD ["usr/bin/mongod", "--smallfiles"]

to

CMD ["/usr/bin/mongod", "--smallfiles"]

Update1:

change this line **HEALTHCHECK --interval=5s --timeout=3s CMD /etc/init.d/mongodb status || exit 1**

to

HEALTHCHECK --interval=5s --timeout=3s CMD /etc/init.d/mongodb status || exit 1

J-Jacques M
  • 978
  • 6
  • 16
  • now going into the container itself it doesnt let me write any commands. it just gets stuck in the CLI. "docker exec -it example bash" – Benny Mestel Feb 21 '20 at 07:45
  • maybe it has to do with the order of my commands in my dockerfile...like expose should be a line lower or something like that – Benny Mestel Feb 21 '20 at 08:09