Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building an application stack using Docker

Tags:

node.js

docker

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.

like image 910
ChrisRich Avatar asked Mar 10 '26 08:03

ChrisRich


2 Answers

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.

like image 200
Dan Prince Avatar answered Mar 12 '26 00:03

Dan Prince


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.

like image 36
k0pernikus Avatar answered Mar 11 '26 23:03

k0pernikus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!