I saw you were setting up a Docker-compose file but it which creates 3 different containers but wanted to combine those 3 containers to a single container/image instead of setting it up as multiple containers at deployment system.
My current list of containers are as follow:
- my main container containing my code that I built using Docker File
- rest 2 are containers of Redis and Postress but wanted to combine them in 1.
Is there any way to do so?
2
Answers
Regarding Kubernetes, you can group your containers in a single pod, as a deployment unit.
A Pod is the smallest deployable units of computing that you can create and manage in Kubernetes.
It is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers.
A Pod’s contents are always co-located and co-scheduled, and run in a shared context.
That would be more helpful than trying to merge containers together in one container.
First of all, running
redis
,postgres
and your"main container"
in one container is NOT best practice.Typically you should have 3 separate containers (single app per container) communicating over the network. Sometimes we want to run two or more lightweight services inside the same container but
redis
andpostgres
aren’t such services.I recommend reading: best practices for building containers.
However, it’s possible to have multiple services in the same docker container using the supervisord process management system.
I will run both
redis
andpostgres
services in one docker container (it’s similar to your issue) to illustrate you how it works. It’s for demonstration purposes only.This is a directory structure, we only need
Dockerfile
andsupervisor.conf
(supervisord
config file):First, I created a
supervisord
configuration file withredis
andpostgres
services defined:Next I created a simple
Dockerfile
:And then I built the docker image:
Finally I ran and tested docker container using the image above:
As you can see it is possible to have multiple services in a single container but this is a NOT recommended approach that should be used ONLY for testing.