1

I've got following problem: Need to create mongodb docker image with some initial data and config, so I started writing Dockerfile:

Dockerfile:

FROM mongo:latest
EXPOSE 27017
ADD ./scripts /scripts
RUN mongo PBM /scripts/indexes

/scripts/indexes:

db.events.createIndex( {'x1':1}, {expireAfterSeconds: 0})

And here I've occured first error, that I cannot connect to db:

Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused

Any ideas ?

SOLUTION

need to use ENTRYPOINT against RUN, because when using the second one, db does not exist.

Armatorix
  • 1,083
  • 10
  • 23
  • There is a way of making this work, that is have a docker image with some preinserted data, although it is not the prettiest. I wrote an answer (for elasticsearch) here: https://stackoverflow.com/questions/35526532/how-to-add-an-elasticsearch-index-during-docker-build/39873112#39873112 This will work best if you need a database with fixed data, because if you try to use a volume after this procedure it will break. – Erpheus Oct 29 '18 at 23:19

1 Answers1

-1

Reason might be that you only exposeing port inside dockerfile, you need use -p 27017:27017 while running image but don't remove EXPOSE port inside Dockerfile,

  • EXPOSE is used to expose port and can access in others containers
  • EXPOSE and -p used to access port in both host m other containers
tgogos
  • 23,218
  • 20
  • 96
  • 128
sanath meti
  • 5,179
  • 1
  • 21
  • 30
  • where did you mentioned that its building error? you clearly mentioned its "Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused" at the end. So i have answered for first error which you got – sanath meti Nov 30 '17 at 18:29