2

I have a docker compose that runs many containers including a container with .Net core code and a dd-agent container.

I have noticed that dd-agent container uses a volume and I need to access files in that volume using code running in the other container.

I have tried to use:

var path = System.Reflection.Assembly.GetExecutingAssembly().Location;
var root = Path.GetPathRoot(path);
var files = Directory.GetFiles(root, "*.log", SearchOption.AllDirectories);

but the files list doesn't contain the files I can see in volume when running something like:

docker run --rm -i -v=myvolume:/tmp/myvolume busybox find /tmp/myvolume

Any ideas on how I can get the volume files?

Docker-compose:

version: "3.7"
volumes:
  logs:
services:
  localstack:
    image: localstack/localstack
    container_name: localstack
    environment:
      - SERVICES=s3,dynamodb
      - AWS_DEFAULT_REGION=eu-west-1
      - HOSTNAME_EXTERNAL=localstack
      - HOSTNAME=localstack
      - AWS_ACCESS_KEY_ID=test
      - AWS_SECRET_ACCESS_KEY=test
    ports:
      - "4572:4572"
      - "4569:4569"
  aws-cli:
    container_name: aws-cli
    image: mesosphere/aws-cli
    volumes:
      - ./../script:/script
    environment:
      - AWS_DEFAULT_REGION=eu-west-1
      - AWS_SECRET_ACCESS_KEY=test
      - AWS_ACCESS_KEY_ID=test
    entrypoint: /script/aws.sh
    depends_on:
      - localstack
  dd-agent:
    container_name: dd-agent
    image: datadog/agent:latest
    ports:
      - "8126:8126"
    environment:
      - DD_APM_ENV=test
      - DD_API_KEY={}
      - DD_PROCESS_AGENT_ENABLED=true
      - DD_DOGSTATSD_NON_LOCAL_TRAFFIC=true
      - DD_APM_ENABLED=true
      - DD_APM_NON_LOCAL_TRAFFIC=true
      - DD_LOG_LEVEL=info
      - DD_HOSTNAME=datadoghq
    volumes:
      - logs:/var/log/datadog
  myapi:
    container_name: myapi
    restart: unless-stopped
    build:
      target: final
      args:
        RELEASETYPE: "Debug"
      context: ./../
      dockerfile: path/Dockerfile
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - DD_AGENT_HOST=dd-agent
      - DD_ENV=test
      - DD_LOGS_INJECTION=true
      - DD_SERVICE_NAME=test
      - DD_TRACE_AGENT_PORT=8126
      - DD_TRACE_AGENT_URL=http://dd-agent:8126
      - DD_TRACE_DEBUG=true
      - DD_TRACE_ENABLED=true
      - DD_TRACE_LOG_PATH=/var/log/datadog/dotnet-profiler.log
      - FLUENTD_PORT=24224
      - FLUENTD_RETRIES=3
    volumes:
      - logs:/var/log/datadog
      - type: bind
        source: ../src
        target: /src/src
    ports:
      - 1990:80
    depends_on:
      - aws-cli
1
  • You need to mount the same volume into both containers. If you're trying to do things via direct docker run commands note that Compose will rename things like volumes and networks so you'll need a command like docker volume ls to find the actual volume name. Commented Jan 17, 2020 at 11:28

1 Answer 1

1

I don't think you can access files of one container from another container. Even with docker cp you can only copy files from the container to the host. Copying from container to container is also not allowed. I would suggest mounting a common volume for the files you want to access. That way you can access them using the mounted folder in the host as an intermediary

service:
    
    service1:
        volumes:
            - ./temp_on_host:/path-to-the-directory-you-want-to-access-through-code
    
   service2:
        volumes:
           - ./temp_on_host:/any_path_in_the_container_which_your_code_can_access

Now the code in your service2 can indirectly access the files in the specified directory in service1

Sign up to request clarification or add additional context in comments.

7 Comments

when using shared volume, dd-agent container still writes files to a random volume it creates, can I get that shared?
yes, just mount some common volume on host and connect the two directories in the respective containers. Not sure if the best way to do it, but its one way.
post your docker-compose for more details
I have posted my docker-compose, I see that logs volume is shared and linked to by 2 containers, but it is empty and dd-agent is using different auto created volume
what is logs here? try creating a file in logs and see if its reflected inside the mapped directory of the container. Tell me if that works. I think you should give the path like ./logs or ../logs or something like that
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.