amazon-web-servicesamazon-ec2dockermodeshape

EC2 with Docker and EBS volume, mount EBS volume inside container during init


I actually trying to achieve something with Docker but I'm stuck, there is my problem.

I have my container hosted on EC2 with my web app inside it. My webapp use as database a JCR repository which is basically a file stored where you want. So each time my web app start, if the repository doesn't exist, it create it otherwise it use the existing.

My current docker file look like this https://gist.github.com/agonist/7cab7358379e9dd6e812 ./chameleon.sh start just start my webapp. In this app, I configured where the repository file is located.

Now I creted a EBS volume attached and mounted in my EC2 instance. This volume will be dedicated to store the repository. So basicaly in my app I configure my repository path to /mnt/repository/ where repository is the directory which will contain my repository file created by my web app. But I don't know how I can mount this volume to my container before the ./chameleon.sh start in the Dockefile. As I see during my research

docker run -v /mnt/repository:/mnt/repository aws_beanstalk/current-app

is not executable from the Dockefile.

I also found stuff about data only container which share a volume with another container, but still the same probleme if I have to run

sudo docker run -d --volumes-from dbdata

after my container started


Solution

  • Short version: This is not an answer, just a little help towards it with clarification on how Docker works.

    Not directly related, but your Dockerfile should probably look like this:

    FROM dockerfile/java:oracle-java8
    
    # Expose the port 9000
    EXPOSE 9000
    
    # Volumes
    VOLUME /root/wisdom/logs
    VOLUME /root/wisdom/application
    
    # Change workdir.
    WORKDIR /root/wisdom
    
    RUN touch /root/wisdom.log
    
    # Add the wisdom distribution
    ADD . /root/wisdom
    
    # For easier handling, we dump the log, so `docker logs containerId` displays
    # the log.
    CMD ./chameleon.sh start && tail -F logs/wisdom.log
    

    That way, all your layers before ADD are cached. Whereas before, if you change anything in your directory, all the layers are recreated. You chameleon.sh should be chmod on your local directory, not at runtime. ADD conserve permissions. the stop is not necessary as when you start your container, you have no process running.

    The -v is indeed not executable from a Dockerfile. This is expected as Dockerfile are meant to be portable. If you tie it to your local machine, then you lose this feature.

    When you do your docker run -v /mnt/repository:/mnt/repository aws_beanstalk/current-app, the mount is done before executing the command. You can double check this by running docker run -v /mnt/repository:/mnt/repository aws_beanstalk/current-app mount

    When using --volumes-from, it is the same thing. The volumes are mounted before executing the command.

    Why then does it fail? I am unsure, it would be interesting to inspect the mount result and do some manual testing with your EBS. I have some containers mounting EBS volumes and it works fine.