1

Massive Docker noob here in dire need of help. There are two docker containers: simple-jar and elk. simple-jar produces log files in /logs directory within its container, and another application, elk, needs to access these log files to do some processing on them.

  1. How can I share the /logs directory so that elk docker container can access it?

This is the Dockerfile for simple-jar:

FROM openjdk:latest
COPY target/pulsar_logging_consumer-1.0-SNAPSHOT-jar-with-dependencies.jar /usr/src/pulsar_logging_consumer-1.0-SNAPSHOT-jar-with-dependencies.jar
EXPOSE 6650
CMD java -jar /usr/src/pulsar_logging_consumer-1.0-SNAPSHOT-jar-with-dependencies.jar 

docker-compose.yml:

version: '3.2'

services:
  elk:
    build:
      context: elasticsearch/
      args:
        ELK_VERSION: $ELK_VERSION
    volumes:
      - type: bind
        source: ./elasticsearch/config/elasticsearch.yml
        target: /usr/share/elasticsearch/config/elasticsearch.yml
        read_only: true
      - type: volume
        source: elasticsearch
        target: /usr/share/elasticsearch/data
    ports:
      - "9200:9200"
      - "9300:9300"

  simple-jar:
    build:
      context: pulsar_logging_consumer/
    volumes:
      - type: bind
        source: ./pulsar_logging_consumer/logs
        target: /usr/share/logs
        read_only: true
    ports:
      - "6500:6500"
    networks:
      - elk
    depends_on:
      - elasticsearch

networks:
  elk:
    driver: bridge

volumes:
  elasticsearch:
doctopus
  • 5,349
  • 8
  • 53
  • 105

2 Answers2

0

You have a couple of options,

1. Create an external named volume, this needs to be created by you (the user) otherwise fails, using the following command

docker volume create --driver local \
--opt type=none \
--opt device=/var/opt/my_data_logs \
--opt o=bind logs_data

Select the volume type that fits, there are different types like nfs, ext3 and 3rd party plugins.

In your docker-compose.yml file

version '3'

volumes:
  logs_data:
    external: true

services:
  app:
    image: yourimage:latest
    ports:
      - 80:80
    volumes:
      - logs_data:/your/path
  1. Share volumes: Start a container using volumes defined by another, (top-level volumes)

    version '3'

     volumes:
       logs_data:
         external: true
    
     services:
       app1:
         image: appimage:latest
         ports:
           - 80:80
         volumes:
           - logs_data:/your/path:ro
       app2:
         image: yourimage:latest
         ports:
           - 8080:80
         volumes:
           - logs_data:/your/path:ro
    
abestrad
  • 898
  • 2
  • 12
  • 23
-1

You can do this by using --link See how to link container in docker?

An better way is to use volumes https://docs.docker.com/storage/volumes/

  • Links never had anything to do with filesystem sharing. They're obsolete in modern Docker. This answer would be more informative if it included a description of how to use volumes to address the problem in the question, more than just a link to the Docker documentation. – David Maze Jun 26 '20 at 10:43