3

I'm a Docker newbie and trying to wrap my head around building an application stack.

For a Node app running on Linux using Nginx, MongoDB and Redis how do you compose the stack?

On DockerHub I can see images for Node, MongoDB and Redis so do you need an image for each component in the stack?

Do you chain all of this together in the Dockerfile and then put the individual config settings in docker-compose.yaml like this gist?

Just trying to get the general idea.

2 Answers 2

3

The best practice for Docker is to run one process per container, deliberately making it hard to write Dockerfiles that run multiple services.

Docker Compose solves the problem by giving you the tools you need to run multiple containers (running one service each) rather than a single container running multiple services.

You might use Compose to set up a stack like this:

version: '2'
services:

  redis:
    image: 'redis'
    ports:
      - '6379:6379'

  nginx:
    image: 'nginx'
    ports:
      - '80:80'

  mongodb:
    image: 'mongo'
    ports:
      - '27017:27017'

  app:
    build: .
    ports:
      - '3000:3000'

After running docker-compose up the library images for redis, nginx and mongo will be pulled from Docker hub.

Then it will try and build a container from the Dockerfile in the same directory as your docker-compose.yml file.

If you're building a node app, it might look something like this:

FROM node

COPY . /app
RUN npm install
CMD ["npm", "start"]

Docker Compose also configures the network so that each of your composed services gets a descriptive host mapped to the container's ip addresses.

So for example, if you were connecting to Redis from your node app, you won't find it running on localhost (because that's your container). Instead, it'll be at redis:6379.

It's written for Python, but the getting started article is very good.

Sign up to request clarification or add additional context in comments.

1 Comment

I've been playing around with this on the weekend and have made good progress. It seems the hardest part is finding the best images for your particular use case and tie it all together in the docker-compose file
2

With each dockerfile, you only build an app and it is indeed a good practice to base them on other images. Be specific, you don't need an Ubuntu if you want to run node. Just chose the official node image as a starting point, e.g. node/6.9.

(Or even look into the alpine-based images for a small memory footprint.)

If you need a mysql use mysql:8, if you need nginx, use nignx:stable.

One of the first mistakes is to make a generic "to rule them all container". Beginners like to base an image on Ubuntu and then install all their tools via apt-get or running install scripts. This is too much manual labor.

While it is still necessary to finetune your images sometimes, you benefit more from the docker ecosystem by using specific images as a starting point. Often, it suffices to only configure an image's container via environment variables.

Linking with just using docker is possible yet rather difficult. docker-compose is not a necessary tool to built a stack, yet a handy one.

Chaining containers together does not happen in the Dockerfile but rather in docker-compose.yml.

docker-compose is a tool to orchestrate an entire stack and link different images, either built or just pulled, together. It's how to make your nginx and nodejs image talk to one another.

1 Comment

Thanks for the explanation. I was able to build a stack using Node, Nginx, Redis and Mongo using docker-compose.yaml. It was not 100% hassle free but after some trial and error I got the stack running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.