Cannot start a service based on custom image

Hello,

I’m having an issue creating a service using my own custom image.

Here are the things I did:

  1. Build a custom image

docker build -t mongo1_test .

Here is the docker file:

FROM thanh/mongo

RUN apt-get update

ADD startup.sh /tmp

CMD mongod --fork --logpath /data/db/mongo.log --bind_ip_all && /bin/bash

I can see the images in the list:

docker images

mongo1_test latest a1a25c6aca6a 2 days ago 492MB

  1. Create a service using swarm:

docker service create --replicas 1 --name mongo_service -p 10038:80 mongo1_test

image mongo1_test:latest could not be accessed on a registry to record
its digest. Each node will access mongo1_test:latest independently,
possibly leading to different nodes running different
versions of the image.

fjcpvj375g87lkhh02fys8sja
overall progress: 0 out of 1 tasks
1/1: starting [============================================> ]
^COperation continuing in background.
Use docker service ps fjcpvj375g87lkhh02fys8sja to check progress.
docker@docker:~/mongo_test$ ^C
docker@docker:~/mongo_test$ ^C
docker@docker:~/mongo_test$

docker@docker:~$ docker service ps fjcpvj375g87lkhh02fys8sja
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
z1el12ouvwx1 mongo_service.1 mongo1_test:latest docker Running Starting 6 seconds ago
mf1j24eb6q5y _ mongo_service.1 mongo1_test:latest docker Shutdown Complete 12 seconds ago
gb6hgui4rxg8 _ mongo_service.1 mongo1_test:latest docker Shutdown Complete 24 seconds ago
zmomw25hrq6z _ mongo_service.1 mongo1_test:latest docker Shutdown Complete 36 seconds ago
vcfv4xbbi3lb _ mongo_service.1 mongo1_test:latest docker Shutdown Complete 47 seconds ago

What I can see is that the container are running and then stops riught away.
No error in the logs.

If i do a docker run with this image, it starts without any problems.
I can even connect to my mongo DB.

So am i doing anything wrong with Swarm?

Thanks for your help.

Hi :slight_smile:

Its because it will run mongo in the background and run bash, the problem is that bash will not run forever, but stop right after the command is executed, and therfore the CMD defined, will finish, and the container will stop.

So, how do we fix this.
There are multiple ways;

I dont know what your requirements are, but you could check out the offcial mongodb image:
https://hub.docker.com/_/mongo/

Another way, would be to remove the “–fork” and " && /bin/bash" ( im not a mongo expert but i belive it would work )

Thank you :slight_smile:

The issue was with my image.

I removed “–fork” and " && /bin/bash" as you suggested and my service is now created.
I can also scale it :slight_smile:

But I don;t understand why it worked with the docker run command.