DEV Community

Cover image for Wait for MongoDB, Postgres or MySql to start on Docker

Wait for MongoDB, Postgres or MySql to start on Docker

Hugo Dias on June 30, 2018

Originally posted in my blog Quick tip here. I was struggling to get this thing working so I need to share with you guys. Assuming that you hav...
Collapse
 
seankilleen profile image
Sean Killeen

Hey! I've noticed that in this post you use "guys" as a reference to the entire community, which is not made up of only guys but a variety of community members.

I'm running an experiment and hope you'll participate. Would you consider changing "guys" to a more inclusive term? If you're open to that, please let me know when you've changed it and I'll delete this comment.

For more information and some alternate suggestions, see dev.to/seankilleen/a-quick-experim....

Thanks for considering!

Collapse
 
warthog618 profile image
Kent Gibson

I for one hope that Hugo leaves his post as is - nice job Hugo - I've found it helpful.

The term "guys" in the context it is used refers to "persons of either sex" (ref Collins English Dictionary). That is the common usage.
You chose to take offence by insisting that the term is exclusive when there is no indication that any such thing was intended. The offence here is requesting that Hugo reword his post to suit your incorrect interpretation of the language. Stop it.

Thanks for considering!

Collapse
 
seankilleen profile image
Sean Killeen

For the record, I didn't take offense.

The defense of the dictionary term doesn't speak to what I'm speaking to, which is a feeling of inclusion. I am not suggesting that Hugo change anything except a small phrase, and I don't believe it would impact the helpfulness of the post at all.

As I explain in the linked post, it seemed like a minor change that could be made easily that my anecdotal evidence suggests would make others feel slightly more welcome, so seemed like a win to me. If you or he disagree, we'll have to leave it there.

Thread Thread
 
warthog618 profile image
Kent Gibson

I disagree, but unlike you I'm open to continuing the discussion.

The point of the dictionary defense is that the language is being used appropriately. You have no grounds for your request.

If there are anecdotal others that find "guys" in this context exclusive then the problem lies with them - they take offence when none if offered. You should be focusing your attention on educating them, rather than trying to rework the use of language to fit within your arbitrary definition of what they may consider to be inclusive.

Thread Thread
 
seankilleen profile image
Sean Killeen

I mostly didn't want to force someone to have a debate on the topic, given that I was dropping in to your post to make an ask of you.

My point is that while the dictionary definition means one thing, the perception of such wording in a specific context -- for example, the male-heavy tech industry -- is different than the dictionary definition. So while you're technically correct, your point is not the point I'm trying to converse around.

Yes, the dictionary definition is technically correct, and we can use that reason if we want. My experiment is that, based on my own anecdotal evidence from doing so, we can make the experience that much more welcoming for others with almost zero effort.

Thread Thread
 
warthog618 profile image
Kent Gibson

Firstly, not my post, so you aren't making an ask of me - I just happened to be reading it about the time you added your experiment, and I'm tired of this social justice go out of your way try to make everyone feel warm and fuzzy rubbish.

Secondly, you are concerned enough that you don't want to force a debate, yet you are happy to shut it down unilaterally? That is inconsistent. More likely you don't want to hear a different opinion.

My point is that your approach is wrong headed. You are trying to change the wrong thing, the author and their post, when the actual problem lies in the reader. Your approach is the easy one, as you can easily identify and make a request of the author, but that is not an excuse for trying to fix your anecdotal problem the wrong way.

Might there be readers who find "all" more inclusive than "guys"? Sure. Should we be changing our interpretation and usage of the language to suit THEIR interpretation? No - that is a slippery Orwellian slope.

Collapse
 
anselmobattisti profile image
Anselmo Battisti

Now there is another way to do it!

You can use healthcheck!

services:
app:
build: .
command: npm start
ports:
- "8080:8080"
volumes:
- .:/usr/app
networks:
- express-mongo-network
healthcheck:
test: ["CMD", "curl", "-f", "mongo:27017"]
interval: 30s
timeout: 10s
retries: 5

Collapse
 
kferrone profile image
Kelly Ferrone

Hey thanks its a nice one. I just want to tweak a bit your Dockerfile to make the image smaller and cleaner. Check this out:

FROM node:latest

RUN mkdir /src

WORKDIR /src
ADD app/package.json /src/package.json

## THE LIFE SAVER
RUN npm install && \
    curl -L https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait \
    -o ./node_modules/.bin/wait && \
    chmod +x ./node_modules/.bin/wait

EXPOSE 3000

ENTRYPOINT [ "npm" ]

## Launch the wait tool and then your application
CMD [ "run", "start:docker" ]

in your package.json scripts make a script like so

"start:docker": "wait && npm start"

Any executable in the node_modules/.bin folder is automatically added to your path when running thorugh npm. Enjoy!

Collapse
 
inspiraller profile image
steve

So how does this work if you want to include other variables in environment?

eg

    environment:
      - WAIT_HOSTS: mongo:27017 
      - ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
      - ME_CONFIG_BASICAUTH_USERNAME=${MONGO_USER}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dmfay profile image
Dian Fay

Very nice! I've used wait-for-it.sh but the environment variable parsing makes this that little bit more convenient.

Collapse
 
wkrea profile image
wkrea

Hi.

Please let me know, are you using Docker for Windows? Or Linux?

Thanks a lot

Collapse
 
dmfay profile image
Dian Fay

Linux; I haven't used Windows for several years. If your Mongo container is constantly restarting like you mentioned below, that sounds like a problem internal to it. Try starting it outside the docker-compose environment so you can diagnose the problem without all the other stuff getting in the way. Good luck!

Collapse
 
greening profile image
Dan Greening

Just a note for others: There are security risks in using the application as described, because the author (or someone who breaks into the author's account) could change the application to do other things, like crash or worse. So use the application as described for tests, but not production. Or clone the repository, examine and build it yourself (what I plan to do).

As a developer, I've watched other developers take security lightly, and this can lead to dramatic news articles about millions of user accounts being broken into, SO, don't be the developer that caused that.

Collapse
 
sarasagunawardhana profile image
Sarasa Gunawardhana

Thank you brother.. Very helpful

Collapse
 
svenvarkel profile image
Sven Varkel

Thanks for good writeup and tip on "wait". This helped me solve startup sequence problems with my docker stack (a node.js API, mongob, rabbitmq and redis) where rabbitmq is quite lazy to start.

Collapse
 
naveensadhana profile image
NaveenSadhana • Edited

Hi Mr. Dias,

Can you explain how do we write CMD command for java application and wait function

Collapse
 
selahattinunlu profile image
Selahattin Ünlü • Edited

It's really "life saver" as you mentioned it. Thanks for sharing, you've saved my day!

Collapse
 
wkrea profile image
wkrea

Hi Hugo

I have some troubble to replicate your example using Docker for Windows.

Every i run docker-compose up -d. I find that Mongodb container it's restarting. :'(