Scalability Ultra level pro max ? 1000 Docker containers start with one command

Guneet Singh
6 min readSep 6, 2022

--

Scalable Docker

Hey, I am assuming you are currently knowing the basics of Docker and NodeJS as this story is not going to focus on that but to show the pro level of scalability.

To get all of us on the same ground level I have created a GitHub repository that you can clone, It has the basic code for all of us to get started together and bring us to the same ground level.

Time to get your guns ready…

Let’s start with the basic Dockerfile as a brushup. You can skip it if you are confident about it.

FROM node:14
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
CMD ["npm", "start"]
EXPOSE 3000

FROM This pulls up a base image of the node from the docker hub which we are going to work on.

WORKDIR Create a folder named app inside the container where we are going to work on our app.

COPY Simply copy the package.json folder from the host(your computer) to the app folder inside the container.

RUN This will run the command mentioned, in the container to install all the dependencies to get our app running

COPY Again copy . (dot) the first dot tells the current location of your host pc and the second tells the location of your WORKDIR. Simply copy each and every folder from the host to the container.

CMD Command to start the application

EXPOSE it will expose a port from the container to the host.

Let’s start with one container

To build the image using dockerfile you can use the following command

docker build --tag nodeImage .

After you are done building the image now start the container using the image we build, with the command

docker run -p 3000:3000 -d --name nodeContainer nodeImage

To check if everything went well go to localhost:3000/ if you see the response as Hey I know you, You are —

Wooh everything went well.

Let’s work with multiple container now

You have already seen that to start one single container 2 steps are required, so to spin up 1000 containers I am surely not typing 2000 commands.

To make working with multiple containers much easier I would use docker-compose (an easy way to manage to make an image + to start a container )

Make a file named docker-compose.yaml. The file would look like this

Code Snippet for docker-compose

Now, docker-compose is going to take the headache of building and running the container. node mentioned under the service is the name of the service you can name it whatever you want.

Let’s try to start the start a container using it

docker-compose up -d

Again go to localhost:3000/ to check if a container is running. If it’s working time to scale it.

Let’s Scale

First bring down the old container using

docker-compose down -v

Told you docker-compose is ❤️. Now for the starter just scale it to 2 containers. You can start 2 containers using the — scale flag and specifying the number of containers for the service name mentioned in docker-compose.

docker-compose up -d --scale node=2

Don’t go ahead without trying out this and if you have already tried it congrats.

It was fun, I know you faced an error. But you already might have guessed what the problem is.

2 docker-container cannot have the same name as we have specified in the docker-compose file so let docker name our containers whatever it likes I personally like the interesting names of docker containers which it names itself.

Just comment on the line specifying the name container_name:nodeContainer.

Bring down the container if any is already working and Let’s try the thing again.

docker-compose up -d --scale node=2

Again an error 😂😂 Now think again how can 2 different containers be connected to the same 3000 port of your host. You might have experienced the same error if you try to run 2 applications when developing without docker.

Solution to this

So we need a middleman who works on one port with the host and handles all the other node containers themselves.

Nginx as reverse Proxy

Yes, We need Nginx to work as a reverse proxy server and load balance. So Nginx will be the middle man between our node server and host.

Don’t worry if you haven’t worked with Nginx before try to make it your first time.

So time to set up one more service for Nginx and you might have guessed it right it’s one more service in the docker-compose.yaml file.

Now Nginx container will be connected to our host machine through one single port 3000 port as mentioned in 3000:3000, and the next 3000 is telling that Nginx has opened a port 3000 that connects the node container.

nginx port config

Now it’s time to configure our Nginx, Don’t get too much into configuration if you are working with it for the first time most probably you might get a little confused.

Make a folder named conf.d and inside that make a file named nginx_conf.conf and copy the following lines or write on your own.

But to send this file in your docker Nginx container we will be needing a volume to attach our local folder conf.d with it. That’s the purpose of volumes in docker-compose.

Now we are ready, Let’s spin up 2 containers.

docker-compose up -d --scale node=2

Now go to localhost:3000/ to check if everything is working you will be able to see something like Hey I know you, You are <containerID>

if you refresh it now you will be seeing the different container id of the second node container as we we have scaled it to 2 containers. and load balancer make the request balance equally on each container.

Time for some real fun

For running 1000 containers make sure you have the fire extinguisher ready with you just in case. I am surely not going to take any chances but you can scale up to 1000 containers using

docker-compose up -d --scale node=1000

If your computer survived, Congrats.

I hope you enjoyed it and learned a lot from this story, if you don’t wanna miss anytime I publish another story make sure to follow me and subscribe to get email updates.

You can follow me on Twitter and LinkedIn wherein I share much more information.

If you think you have a better or different perspective about this article don’t forget to comment I may include it in this with your name.

--

--

Guneet Singh

I am a software developer also a student, Helping other developers in there development journey. You can help me by following me here.