I asked something similar the other time and a very nice person helped me, but another problem arose. I have my Dockerfile. Written as follows:
###Download MONGO (with Ubuntu)
FROM mongo:latest
###Create an App directory
RUN mkdir /app
###Nos situamos en el directorio
WORKDIR /app
###Copiamos el binario 'general' dentro del directorio.
COPY general .
###Update the OS ubuntu image
#RUN apt-get -y update
###Install packages
###Instalo curl para poder hacer peticiones get dentro de la consola,
###Posteriormente expondré el puerto y las haré en Postman.
#RUN apt-get -y install curl
###NECESITO QUE CARGUE MONGO Y EJECUTAR EL BINARIO: ./general
#ENTRYPOINT [ "./general" ]
#CMD ["./general"]
#RUN ./general
What appears there runs perfectly and does what I expect: it copies the binary into a directory for me. Running the container runs Mongo.
If I access the ubuntu console provided by Mongo, I can run and execute the binary file without problem , make get and post requests (which modify the database), etc. (DO NOT CREATE DATA PERSISTENCE YET)
THE PROBLEM COMES WHEN I WANT TO RUN THE BINARY FILE AUTOMATICALLY. In the Dockerfile, it shows that in lines 23, 24 and 25, I tried several ways. With Entrypoint and CMD, it throws me the error that I want to solve. (with RUN the container is not created)
###NECESITO QUE CARGUE MONGO Y EJECUTAR EL BINARIO: ./general
#ENTRYPOINT [ "./general" ]
#CMD ["./general"]
#RUN ./general
When building the container and running it with Entrypoint or CMD it throws the following error:
2022-03-23T12:31:26.929Z
ERROR [email protected]/mongo.go:36 cannot PING the mongo server:
server selection error: server selection timeout, current topology: { Type: Unknown,
Servers: [{ Addr: localhost:27017, Type: Unknown, Last error: connection() error occured
during connection handshake: dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
IT DOES NOT SHOW THAT THE DATABASE IS STARTED, it directly wants to run the binary and it will throw that error because it depends on the existence of an active connection to mongo.
Thank you very much.
When you declare your CMD or ENV you are overriding the original behavior of the image you are inheriting from (mongo), which is precisely to create and listen to the MongoDB base. You can see it here in his Dockerfile.
If you want to keep that database running and also run your file, it occurs to me that you can declare a CMD to run mongo in the background (this is achieved using the --fork and --syslog options ):
Now, keep in mind that since the last process is now executing your file, the container will stop as soon as it is finished.
I hope this helps you.