skip to Main Content

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:

  1. my main container containing my code that I built using Docker File
  2. rest 2 are containers of Redis and Postress but wanted to combine them in 1.

Is there any way to do so?

2

Answers


  1. 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.

    https://d33wubrfki0l68.cloudfront.net/fe03f68d8ede9815184852ca2a4fd30325e5d15a/98064/docs/tutorials/kubernetes-basics/public/images/module_03_pods.svg

    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.

    Login or Signup to reply.
  2. 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 and postgres 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 and postgres 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 and supervisor.conf (supervisord config file):

    $ tree example_container/
    example_container/
    ├── Dockerfile
    └── supervisor.conf
    

    First, I created a supervisord configuration file with redis and postgres services defined:

    $ cat example_container/supervisor.conf
    [supervisord]
    nodaemon=true
    
    [program:redis] 
    command=redis-server # command to run redis service
    autorestart=true
    stderr_logfile=/dev/stdout
    stderr_logfile_maxbytes = 0
    stdout_logfile=/dev/stdout
    stdout_logfile_maxbytes = 0
    
    
    [program:postgres]
    command=/usr/lib/postgresql/12/bin/postgres -D /var/lib/postgresql/12/main/ -c config_file=/etc/postgresql/12/main/postgresql.conf # command to run postgres service
    autostart=true
    autorestart=true
    stderr_logfile=/dev/stdout
    stderr_logfile_maxbytes = 0
    stdout_logfile=/dev/stdout
    stdout_logfile_maxbytes = 0
    user=postgres
    environment=HOME="/var/lib/postgresql",USER="postgres"
    

    Next I created a simple Dockerfile:

    $ cat example_container/Dockerfile
    FROM ubuntu:latest
    ARG DEBIAN_FRONTEND=noninteractive
    
    # Installing redis and postgres
    RUN apt-get update && apt-get install -y supervisor redis-server postgresql-12
    
    # Copying supervisor configuration file to container
    ADD supervisor.conf /etc/supervisor.conf
    
    # Initializing redis and postgres services using supervisord
    CMD ["supervisord","-c","/etc/supervisor.conf"]
    

    And then I built the docker image:

    $ docker build -t example_container:v1 .
    

    Finally I ran and tested docker container using the image above:

    $ docker run --name multi_services -dit example_container:v1
    472c7b2eac7441360126f8fcd0cc80e0e63ac3039f8195715a3a400f6288a236
    
    
    $ docker exec -it multi_services bash
    root@472c7b2eac74:/# ps aux   
    USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root           1  0.7  0.1  27828 23372 pts/0    Ss+  10:04   0:00 /usr/bin/python3 /usr/bin/supervisord -c /etc/supervisor.conf
    postgres       8  0.1  0.1 212968 28972 pts/0    S    10:04   0:00 /usr/lib/postgresql/12/bin/postgres -D /var/lib/postgresql/12/main/ -c config_file=/etc/postgresql/12/main/postgresql.conf
    root           9  0.1  0.0  47224  6216 pts/0    Sl   10:04   0:00 redis-server *:6379
    ...
    
    root@472c7b2eac74:/# netstat -tulpn
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
    tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      9/redis-server *:6
    tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      8/postgres
    tcp6       0      0 :::6379                 :::*                    LISTEN      9/redis-server *:6
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search