0

We have two docker containers. The first container is backend_device_service which we access using the command -> docker exec -it backend_device_service bash.

On this backend_device_container we write our python code. Now I am writing an API which needs to access a file which is on another container rsyslog (server is same just the container is different).

We access this container using the command -> docker exec -it rsyslog bash

In this container at the path -> /var/log/CPE my file contains which I need to access.

However I can access any path in the same container using the following piece of code:-

class SysLogReader(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]

def get(self, request):
    content = os.popen('cat /app/sdwan_device_service/devices/views').read().strip('\n').lower()
    logger.info(f"\ncontent of the file:\n {content}\n")
    dir_path = os.path.dirname(os.path.realpath(__file__))

    resp = {"success":True, "message" : f"Directoty path is {dir_path}"}

    return Response(resp, status=status.HTTP_200_OK)

While my problem is how to access the file which is in another container rsyslog.

Kapil Sharma
  • 146
  • 2
  • 12
  • What about a shared volume between your two containers in which you place the CPE file? – Martin Tovmassian Jul 19 '22 at 07:18
  • @MartinTovmassian Shared volume? – Kapil Sharma Jul 19 '22 at 07:35
  • Yes, I think it is a good solution to [share data between containers](https://stackoverflow.com/a/37001156/5472004). – Martin Tovmassian Jul 19 '22 at 07:48
  • Containers are by design isolated from each other; they can't normally access each other's files and it's a good idea to design around needing to do this. Can you use another mechanism like an HTTP server to transfer files? Can you set up the system so that it runs without requiring the `docker exec` debugging tool? – David Maze Jul 19 '22 at 10:21
  • (Imagine asking this same question, but about plain Python instead of Docker; instead of "file" you have "local variable", and instead of `docker exec` you have `python -m pdb`. You wouldn't normally need the debugger and you wouldn't expect to be able to access the private state of the other process/container.) – David Maze Jul 19 '22 at 10:23
  • Can you please describe the solutions in detail @DavidMaze ? – Kapil Sharma Jul 19 '22 at 10:24
  • So for example in one container, use a Web server like Flask, and in the other use an HTTP client like Requests. Make an HTTP GET call from one container to the other to retrieve the files. Set up the two images' Dockerfiles so their `CMD`s start the respective applications. – David Maze Jul 19 '22 at 10:48

0 Answers0