KatharaFramework / Kathara

A lightweight container-based network emulation system.
https://www.kathara.org/
GNU General Public License v3.0
452 stars 63 forks source link

Unable to start Kathara Containers from another container #167

Closed zhangineer2 closed 2 years ago

zhangineer2 commented 2 years ago

Hello

I have a use case where I need to start kathara containers by executing commands from another container.

I would first like to know if this is indeed supported. When I run the code directly on the host, it works fine. But when I try to run it from inside a container, I get the following error

2022-05-14 22:40:54,685 __main__ ERROR    500 Server Error for http+docker://localhost/v1.41/plugins/kathara/katharanp:amd64/enable?timeout=0: Internal Server Error ("failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting "/run/containerd/io.containerd.runtime.v2.task/plugins.moby/d0a3a786df338e18ac8fd2d90a6984e0661560f0284d55150e8ef98df70669b2" to rootfs at "/var/run/xtables.lock" caused: mount through procfd: not a directory: unknown")
2022-05-14 22:40:54,685 root INFO     Starting Kathara lab ..........
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/docker/api/client.py", line 268, in _raise_for_status
    response.raise_for_status()
  File "/usr/local/lib/python3.9/site-packages/requests/models.py", line 960, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: http+docker://localhost/v1.41/plugins/kathara/katharanp:amd64/enable?timeout=0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/aidn/routing_ai/routing_ai/deploy_dt.py", line 155, in <module>
    main()
  File "/aidn/routing_ai/routing_ai/deploy_dt.py", line 150, in main
    Kathara.get_instance().deploy_lab(lab)
  File "/usr/local/lib/python3.9/site-packages/Kathara/manager/Kathara.py", line 29, in get_instance
    Kathara()
  File "/usr/local/lib/python3.9/site-packages/Kathara/manager/Kathara.py", line 39, in __init__
    self.manager: IManager = ManagerFactory().create_instance(module_args=(manager_type,),
  File "/usr/local/lib/python3.9/site-packages/Kathara/foundation/factory/Factory.py", line 29, in create_instance
    return self.get_class(module_args, class_args)()
  File "/usr/local/lib/python3.9/site-packages/Kathara/decorators.py", line 12, in exec_with_privileges
    result = method(*args, **kw)
  File "/usr/local/lib/python3.9/site-packages/Kathara/manager/docker/DockerManager.py", line 47, in check_docker
    method(*args, **kw)
  File "/usr/local/lib/python3.9/site-packages/Kathara/manager/docker/DockerManager.py", line 79, in __init__
    docker_plugin.check_and_download_plugin()
  File "/usr/local/lib/python3.9/site-packages/Kathara/manager/docker/DockerPlugin.py", line 47, in check_and_download_plugin
    plugin.enable()
  File "/usr/local/lib/python3.9/site-packages/docker/models/plugins.py", line 70, in enable
    self.client.api.enable_plugin(self.name, timeout)
  File "/usr/local/lib/python3.9/site-packages/docker/utils/decorators.py", line 34, in wrapper
    return f(self, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/docker/api/plugin.py", line 86, in enable_plugin
    self._raise_for_status(res)
  File "/usr/local/lib/python3.9/site-packages/docker/api/client.py", line 270, in _raise_for_status
    raise create_api_error_from_http_exception(e)
  File "/usr/local/lib/python3.9/site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
    raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 500 Server Error for http+docker://localhost/v1.41/plugins/kathara/katharanp:amd64/enable?timeout=0: Internal Server Error ("failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting "/run/containerd/io.containerd.runtime.v2.task/plugins.moby/d0a3a786df338e18ac8fd2d90a6984e0661560f0284d55150e8ef98df70669b2" to rootfs at "/var/run/xtables.lock" caused: mount through procfd: not a directory: unknown")

The above error was very similar to an issue described here https://github.com/docker/for-win/issues/9282. They appear to be the same error message, but different cause.

Thank you for all your help Peter

Skazza94 commented 2 years ago

Hi @zhangineer, I can't understand what you're trying to achieve, could you please explain and post some examples?

zhangineer2 commented 2 years ago

Hi @Skazza94 basically I am just trying to run a simple Kathara lab, but from inside a container https://github.com/KatharaFramework/Kathara/wiki/Kathara-Python-API

I am trying to create the kathara containers on the host, while using the above example

Thank you

Skazza94 commented 2 years ago

Since Kathará relies on Docker, you need a Docker container with Docker and Kathará installed in it. We're working on supporting this feature, but in the meanwhile you can use the following Dockerfile to build the aforementioned image. It contains both the Kathará binary and the PyPI package.

KatharaDocker.zip

zhangineer2 commented 2 years ago

@Skazza94 I can definitely add docker to the image that I am using, however, wouldn't that spin up Kathara inside the docker container rather than on the host ?

This picture illustrates that I am trying to do.

image
Skazza94 commented 2 years ago

Hi @zhangineer, now I understand your request. I edited the previous Dockerfile to work with this setup: DockerHost.zip

After building it (e.g. with hostdocker image name), you have to mount the host Docker socket inside the container. Through this socket, Kathará will create the containers on the host:

docker run -v /var/run/docker.sock:/var/run/docker.sock -v /var/run/docker.pid:/var/run/docker.pid -ti --privileged hostdocker /bin/bash

P.S.: I also installed both the Kathará binary and the PyPI package, and the container automatically opens in the Python3.9 venv.

Hope this is useful! Mariano.