Running several services in a single container.
Docker was idealized for running a single service at once. The principal advantage of that is the isolation: you can keep several applications isolated one from the other.
But, it is possible to run several services in a single container with the supervisor. Supervisor is a client/server system to control processes in *nix-like systems.
The following example creates a single container with two services different from each other (the code is the same) controlled by supervisor.
You can download the code at: https://github.com/t3rcio/docker-supervisor
Dockerfile
#Dockerfile FROM python:3.9 ENV PYTHONUNBUFFERED=1 RUN apt-get update && apt-get install -y supervisor RUN mkdir -p /var/log/supervisor WORKDIR /code COPY . /code COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf RUN mkdir logs RUN supervisord -c /etc/supervisor/conf.d/supervisord.conf &
docker-compose.yml
#docker-compose.yml version: "3.9" services: supervisor: build: . restart: always command: /usr/bin/supervisord volumes: - .:/code
Put the code above in a directory, and execute:
user@user-pc:~$ docker-compose -f docker-compose.yml -up
With that, you just made the container.
To verify whether the services are running, execute:
user@user-pc:~$ tail -f out1.log (...) user@user-pc:~$ tail -f out2.log
That’s it :-) With this technique, you can run several services in a single container; you can, for example, have a task scheduler and a task consumer.
References
https://docs.docker.com/config/containers/multi-service_container/
Leave a Reply