1

I am trying to create 2 docker images simultaneously but images are creating one after another. Where am I going wrong here??

import docker
import multiprocessing as mp

class Docker():
    def __init__(self):
        self.client = docker.from_env()

    def get_all_containers(self):
        containers=self.client.containers.list(all=True)
        return (containers)

    def image_build(self,path,file,tag):
        self.client.images.build(path=path, dockerfile=file, tag=tag, rm=True, nocache=True)

    def initiate(self):
        print ("building image")
        t1=mp.Process(target=self.image_build(path="/home/rohith/Desktop/proj/poc/", file="login_service", tag="login_service"))
        print ("initiated t1 and now going to t2")
        t2=mp.Process(target=self.image_build(path="/home/rohith/Desktop/proj/poc/", file="login_mongo", tag="mongo_service"))
        t1.start()
        t2.start()
        print ("Done with the images for login_service now implement to run a container. First db has to be initiated and later login_service")


if __name__=="__main__":
    doc=Docker()
    doc.initiate()

Let me know if you need my docker files as well.

2 Answers2

1

From the docs, here

example

from multiprocessing import Process

def f(name):
    print('hello', name)

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

You don't actually want to call your methods as part of the target.

replace;

t1=mp.Process(target=self.image_build(path="/home/rohith/Desktop/proj/poc/", file="login_service", tag="login_service"))

with;

t1.mp.Process(target=self.image_build, args=("/home/rohith/Desktop/proj/poc/","login_service","login_service")

and, don't forget to join your processes;

t1.join()
t2.join()
SteveJ
  • 3,034
  • 2
  • 27
  • 47
  • Hi nicely done But I have a doubt. Why should we join processes? I'd like to know the background information on what's going on and why it is essential to join processes and also the code flow of what happens when we join processes? Will the code wait there until those t1 and t2 are completelt processesed? – rohit tadiparti May 09 '21 at 17:27
  • @rohittadiparti This might help; https://stackoverflow.com/questions/15085348/what-is-the-use-of-join-in-python-threading – SteveJ May 10 '21 at 14:44
0

i feel you are doing somethings wrongly... you have to import Process from the Multiprocessing package

import docker
from multiprocessing import Process

class Docker():

    # your code here.......
   
    def initiate(self):
        print ("building image")
        t1=mp.Process(target=self.image_build(path="/home/rohith/Desktop/proj/poc/", file="login_service", tag="login_service"))
        print ("initiated t1 and now going to t2")
        t1.start()

        t2=mp.Process(target=self.image_build(path="/home/rohith/Desktop/proj/poc/", file="login_mongo", tag="mongo_service"))
        t2.start()

        t1.join()  # join both processes here.
        t2.join()

        print ("Done with the images for login_service now implement to run a container. First db has to be initiated and later login_service")

if __name__=="__main__":
    doc=Docker()
    doc.initiate()

or you utilize the Ray library...

import ray
ray.init()

@ray.remote
def firstFunc():
  # first Image_Build Process here here... 

@ray.remote
def secondFunc():
  # second Image_Build Process here here... 
 
ray.get([firstFunc.remote(), secondFunc.remote()])   # run at the same time...

for more information about the Ray libray check here..

Ande Caleb
  • 1,163
  • 1
  • 14
  • 35
  • Hi nicely done But I have a doubt. Why should we join processes? I'd like to know the background information on what's going on and why it is essential to join processes and also the code flow of what happens when we join processes? Will the code wait there until those t1 and t2 are completelt processesed? – rohit tadiparti May 09 '21 at 17:28