I have Dockerfile
FROM mongo
RUN mkdir -p /app/data
WORKDIR /app/data
COPY cars.csv /app/data
RUN mongod --fork --syslog
CMD ["mongoimport", "-d", "Gene", "-c", "Genes", "--type", "csv", "--file", "cars.csv", "--headerline"]
EXPOSE 27017:27017
I build and start the docker file using the following commands
docker build -t "mtest:1" .
docker run mtest:1
the run command gives me the following error
2022-01-07T19:01:12.784 000 error connecting to host: could not connect to server: server selection error:
server selection timeout, current topology: { Type: Single, Servers: [{ Addr: localhost:27017,
Type: Unknown, Last error: connection() error occured during connection handshake:
dial tcp 127.0.0.1:27017: connect: connection refused }, ] }
the output of docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e623f431b9db mtest:1 "docker-entrypoint.s…" 2 minutes ago Exited (1) About a minute ago cranky_satoshi
When I try to do inside the container using docker exec -it e623f431b9db bash and run mongod I get the following
MongoDB shell version v5.0.5
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
exception: connect failed
exiting with code 1
I'm new to this and I'm not sure what I'm doing wrong.
CodePudding user response:
You've overwritten the mongo startup procedure with your own CMD, such that the Mongo server doesn't start anymore. Therefore, mongoimport will fail, and your container will exit.
Instead, just run the original Mongo image by itself with a volume mount, then exec into it, and run your import function. Afterwards, it'll still be running, and you can connect to it.
By the way, EXPOSE 27107:27107 isn't a valid expression.
