Home > Net >  How to run a docker image from within a docker image?
How to run a docker image from within a docker image?

Time:10-17

I run a dockerized Django-celery app which takes some user input/data from a webpage and (is supposed to) run a unix binary on the host system for subsequent data analysis. The data analysis takes a bit of time, so I use celery to run it asynchronously. The data analysis software is dockerized as well, so my django-celery worker should do os.system('docker run ...'). However, celery says docker: command not found, obviously because docker is not installed within my Django docker image. What is the best solution to this problem? I don't want to run docker within docker, because my analysis software should be allowed to use all system resources and not just the resources assigned to the Django image.

CodePudding user response:

I don't want to run docker within docker, because my analysis software should be allowed to use all system resources and not just the resources assigned to the Django image.

I didn't catch the causal relationship here. In fact, we just need to add 2 steps to your Django image:

  1. Follow Install client binaries on Linux to download the docker client binary from prebuilt, then your Django image will have the command docker.

  2. When starting the Django container, add /var/run/docker.sock bind mount, this allows the Django container to directly talk to the docker daemon on the host machine and start the data-analysis tool container on the host. As the analysis container does not start in Django container, they can have separate system resources. In other words, the analysis container's resources do not depend on the resource of the Django image container.

Samples with one docker image which already has the docker client in it:

root@pie:~# ls /dev/fuse
/dev/fuse
root@pie:~# docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock docker /bin/sh
/ # ls /dev/fuse
ls: /dev/fuse: No such file or directory
/ # docker run --rm -it -v /dev:/dev alpine ls /dev/fuse
/dev/fuse

You can see, although the initial container does not have access to the host's /dev folder, the docker container whose command initialized from the initial container could really have separate resources.

If the above is what you need, then it's the right solution for you. Otherwise, you will have to install the analysis tool in your Django image

  • Related