Is Xyp9x a robot?

If you tried to describe someone you know with three words and come up with “nice,” “funny,” and “smart” you either don’t know well or have simply have no facility with language. Yet, when it comes…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Creating a docker image with MongoDB and run it with Mongo Express

Hey there! I got myself with that task at work where I had to create an image with MongoDB and then run it with Mongo Express so I’ll describe here how we solved it and will try to be as clear as possible.

The Dockerfile

Ok, so let’s take a look on the Dockerfile as that’s where everything begins. What we’ll be doing here is basically create an image with MongoDB, add a couple of users and then set a schema validator to one of our collections.

So, what we’re doing on the Dockerfile basically is copying two files to the temp folder inside our docker. Why do we need that? Simple. We want to use both files inside our docker instance, which means that we need to have those files there and that’s why we’re copying them.

The second thing we’ll do is running the mongodb-setup.sh. That’s where all MongoDB stuff will go. We’ll create our users, collections, etc using this mongodb-setup.sh that will be executed inside our docker instance.

Ok, so that’s it’ for the MongoDB setup. But as you can see, on this file we refer to another file, which is the one that will create our collection.

Ok, so now let’s take a look on our schema-validator.js file.

Ok. Now you should be able to run Docker build and see your image being created.

One thing that you should have in mind if you’re a Windows user is that end of line on windows is different on UNIX and that might drive you crazy as the error is not quite clear about that. In my case it was saying that the file wasn’t being found. I just had to make sure that the end of line was pretty on the UNIX standards and the problem was gone.

To test what we’ve done so far you can just run:

This should generate your image and you should be able to run it:

And you should be able to reach Mongo Shell:

And once you’re on the Mongo Shell you should be able to:

And see that everything is matching what we’ve done on our mongodb-setup.sh file.

I suggest you to create another .sh file just to build your image (so you don’t need to manually run the docker build ./, create a tag, etc. And one more to deploy it, pushing your image to the docker registry.

The last step I had to do was really simple. It was basically writing a docker-compose.yml to pull the image I created and pushed to the registry, pull Mongo Express and run it.

Ok, so just a couple explanations on the docker-compose.yml. For mongo, we won’t be pulling the image from Docker Hub, because we want the image we created. So we need to pass the url where our image is located.

For MongoExpress we can’t get the latest one available on Docker Hub and the tricky part is where the MongoExpress will point to. And that is defined under the environment piece.

We need to define the admin credentials for the database and the url where the Mongo will be sitting. On this case here I’m assuming that you’ll create some kind of development environment where you’ll need to provision your MongoDB daily or something like this.

That’s it for our Docker image.

Just to be clear here. I’m not an expert so I’m not quite sure it was the best solution, but it was a solution that worked for us.

Add a comment

Related posts:

What can you do if a brand is using your images on Instagram?

In social media terms, Instagram is the home of photos. As a photographer, maybe you use Instagram to experiment, maybe you give people a behind-the-scenes peek into your professional shoots, maybe…

Mandalorian Hunts Moff Gideon

This is my new IronStudios 1/10 art scale statue photographed in my home studio. Here’s the behind the scenes look. I used the Impact Photo Pro LED Booth 400 — https://bhpho.to/3NTpLxQ as my base. I…

What Colors Look Good on Me?

Finding out which colors match your skin tone, hair, and eyes can help you choose the most flattering garments that will bring out your inherent beauty. You may create a wardrobe that complements…