Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple threads inside docker container

I need to spawn N threads inside a docker container. I am going to receive a list of elements, then divide it in chunks and each thread will process each chunk.

So I am using a docker container with one process and N threads. Is it good practice in docker? I think so, because we have, e.g, apacha webserver that handle connections spawining threads.

Or it will be better to spawn N container each one for each chunk? If it is, what is the correct way to do this?

like image 612
p.magalhaes Avatar asked Jun 06 '16 12:06

p.magalhaes


People also ask

Can a Docker container use multiple threads?

Think of a Docker container as a lightweight isolated environment, akin to a virtual environment, where you can run a program/service. This service can run multiple threads, all launched from the parent program - it is still one service running on a single Docker container.

Can Docker container have multiple processes?

It's ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application. You can connect multiple containers using user-defined networks and shared volumes.

How do I view threads in a container?

the correct (approximate) command is 'docker stats' or 'docker container stats' specifically the PID column; it includes all processes and threads.

How do I run multiple processes in a container?

Use a process manager which can run multiple processes: You can set the container's entrypoint to a specialised program which is capable of running and managing multiple processes. One example of this is supervisord. You can use supervisord as your container entrypoint, which will then load the services that you need.


1 Answers

A container as such has nothing to do with the computation you need to perform. The question you are posting is whether I should have multiple processes doing my processing or multiple threads spawned by the same process doing the processing ?

A container is just a platform for running your application in the environment you want. Period. It means, you would be running a process inside a container to run your business logic. Multiple containers simply means multiple processes and as it is advised, you should go for multiple threads rather than multiple processes as spawning a new process (in your case, as container) would eat up more resources and would also require more memory etc. So it is better to have just one container which will spawn multiple threads to do the job for you.

However, it also depends upon the configuration of the underlying machine on which the container is started. If it makes sense to spawn multiple containers with multiple threads because of the multicore capabilities of the underlying hardware, you should do that as well.

like image 175
Sachin Malhotra Avatar answered Sep 19 '22 15:09

Sachin Malhotra