Domenico Rossi
Domenico Rossi

Reputation: 33

How to package several services in one docker image?

I have a docker compose application, which works fine in local. I would like to create an image from it and upload it to the docker hub in order to pull it from my azure virtual machine without passing all files. Is this possible? How can I do it?

I tried to upload the image I see from docker desktop and then pull it from the VM but the container does not start up.

Here I attach my .yml file. There is only one service at the moment but in the future there will be multiple microservices, this is why I want to use compose.

    version: "3.8"
    services:
      dbmanagement:
        build: ./dbmanagement
        container_name: dbmanagement
        command: python manage.py runserver 0.0.0.0:8000
        volumes:
          - ./dbmanagement:/dbmandj
        ports:
          - "8000:8000"
        environment:
          - POSTGRES_HOST=*******
          - POSTGRES_NAME=*******
          - POSTGRES_USER=*******
          - POSTGRES_PASSWORD=*******

Thank you for your help

Upvotes: 3

Views: 2858

Answers (1)

JRichardsz
JRichardsz

Reputation: 16505

The answer is: yes, you can but you should not

According to the Docker official docs:

It is generally recommended that you separate areas of concern by using one service per container

Also check this:

docker-compose is enough

docker-compose exist just for that: Run several services with one click (minimal configurations) and commonly in the same server.

foreground process

In order to works a docker container needs a foreground process. To understand what is this, check the following links. As a extremely summary we can said you that a foreground process is something that when you launch it using the shell, the shell is taken and you can and you cannot enter more commands. You need to press ctrl + c to kill the process and get back your shell.

The "fat" container

Anyway, if you want to join several services or process in one container (previously an image) you can do it with supervisor.

Supervisor could works a our foreground process. Basically you need to register one or many linux processes and then, supervisor will start them.

how to install supervisor
sudo apt-get install supervisor

source: https://gist.github.com/hezhao/bb0bee800531b89d7be1#file-supervisor_cmd-sh

add single config: /etc/supervisor/conf.d/myapp.conf
[program:myapp]
autostart = true
autorestart = true
command = python /home/pi/myapp.py
environment=SECRET_ID="secret_id",SECRET_KEY="secret_key_avoiding_%_chars"
stdout_logfile = /home/pi/stdout.log
stderr_logfile = /home/pi/stderr.log
startretries = 3
user = pi

source: https://gist.github.com/hezhao/bb0bee800531b89d7be1

start it
sudo supervisorctl start myapp
sudo supervisorctl tail myapp
sudo supervisorctl status

In the previous sample, we are used supervisor to start a python process.

multiple process with supervisor

You just need to add more [program] sections to the config file:

[program:php7.2]
command=/usr/sbin/php-fpm7.2-zts
process_name=%(program_name)s
autostart=true
autorestart=true

[program:dropbox]
process_name=%(program_name)s
command=/app/.dropbox-dist/dropboxd
autostart=true
autorestart=true

Here some examples, just like your requirement: several process in one container:

Also you could configure a web dashboard:

Another samples with docker + supervisor:

Upvotes: 5

Related Questions