In this post I will write about one possible problem which you may face with when try to run MongoDB docker container. Env used in this article: docker installed on Ubuntu running on WSL2 on Windows 11.
Let's start from beginning. If we want to run MongoDB docker container we first need to download it:
docker pull mongo
If everything went well you should see something like that:
Then we may want to run it with ports forwarding (MongoDB by default uses port 27017). At first I tried to do it like that:
docker run -d --name mongo-test mongo:latest -p 27017:27017
It didn't return errors and just echoed container id:
But when I tried to connect to container with bash
docker exec -it mongo-test bash
it failed with the following error:
Error response from daemon: Container {id} is not running
Checking the docker logs for mongo-test container helped to found the reason:
docker logs mongo-test
Error parsing command line: unrecognised option '-p'
The problem was that port forwarding option was specified at the end while according to documentation of docker run command it should be specified before image name:
When I changed the command like that:
docker run -d -p 27017:27017 --name mongo-test mongo:latest
container successfully ran and I was able to connect to it via bash:
So it is important to check options order when you run docker container. Although there are no error messages it won't work as expected otherwise.