简体   繁体   中英

Running docker container inside docker container

I have built a docker image of my app (streamlit) and inside my image, I have another image which I want to run as it is a search engine inside my app.

I was doing this before (outside of dockerizing the app) via subprocess

filepath = '"C:/Users/k.queenan/Documents/wsearch/docker/search-engine:/home" '
p = subprocess.Popen ('docker run -v' + filepath + 'search-image' , stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate()

which worked fine. I am getting an error now saying that the filepath is not valid. How can I get around this inside the dockerized version?

There is a method called DinD (Docker in Docker), but it should be used for developing the docker itself.

By security perspective it is not secure, because your parent container needs privileged rights. (You also can control the docker daemon itself from a container by mounting the docker unix socket /var/run/docker.sock - but you need privileged rights too - so it depends on your use case, but its not recommended)

Use docker-compose instead.

A sample multi-container yaml file (this method completely matches your use case):

version: "3.7"

services:
  app:
    image: node:12-alpine
    command: sh -c "yarn install && yarn run dev"
    ports:
      - 3000:3000
    working_dir: /app
    volumes:
      - ./:/app
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: root
      MYSQL_PASSWORD: secret
      MYSQL_DB: todos

  mysql:
    image: mysql:5.7
    volumes:
      - todo-mysql-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: todos

volumes:
  todo-mysql-data:

Update

If you want to control the docker host from a container with python, you can do the following:

Map the docker socket for your container with (on windows):

docker run -v "//var/run/docker.sock://var/run/docker.sock" your_python_image

and use docker.py to control your docker host from inside a container (instead of subprocess):

>>> import docker
>>> c = docker.from_env()
>>> stdout = c.containers.run(image="search-image:latest",command="your_command", remove=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM