Docker shared container within multiple docker compose projects












0















I have a docker-compose.yml which get's 2 services up (I have left out all irrelevant data from it).



  app:
build:
context: .
dockerfile: ./docker/app/Dockerfile
image: ...
container_name: app-${ENV}
depends_on:
- db
expose:
- 80

db:
image: ...
container_name: my-cool-db
ports:
- "3306:3306"


The point to see here is that app is getting a container name depending on the parameter. So basically I have a shell script running the following:



ENV=$1 docker-compose -p $1 up -d


So in short, whatever I forward as parameter, the new app container should be brought up. For example if I do sh initializer.sh first I will get app-first container. -p parameter is specified so I can have multiple instances of same container classified as a different project.



If I have a single container this works great, and I end up with say:



app-first
app-second
app-third


What I would like to achieve is to have all containers use the same DB. But when I do a docker-compose my DB container still wants to be brought up independent of his existence already.



Is it an issue that it tries to create a DB under different project name, but with same container name so it causes the collision?



Can this be made without bringing up 2 separate DB containers?










share|improve this question





























    0















    I have a docker-compose.yml which get's 2 services up (I have left out all irrelevant data from it).



      app:
    build:
    context: .
    dockerfile: ./docker/app/Dockerfile
    image: ...
    container_name: app-${ENV}
    depends_on:
    - db
    expose:
    - 80

    db:
    image: ...
    container_name: my-cool-db
    ports:
    - "3306:3306"


    The point to see here is that app is getting a container name depending on the parameter. So basically I have a shell script running the following:



    ENV=$1 docker-compose -p $1 up -d


    So in short, whatever I forward as parameter, the new app container should be brought up. For example if I do sh initializer.sh first I will get app-first container. -p parameter is specified so I can have multiple instances of same container classified as a different project.



    If I have a single container this works great, and I end up with say:



    app-first
    app-second
    app-third


    What I would like to achieve is to have all containers use the same DB. But when I do a docker-compose my DB container still wants to be brought up independent of his existence already.



    Is it an issue that it tries to create a DB under different project name, but with same container name so it causes the collision?



    Can this be made without bringing up 2 separate DB containers?










    share|improve this question



























      0












      0








      0








      I have a docker-compose.yml which get's 2 services up (I have left out all irrelevant data from it).



        app:
      build:
      context: .
      dockerfile: ./docker/app/Dockerfile
      image: ...
      container_name: app-${ENV}
      depends_on:
      - db
      expose:
      - 80

      db:
      image: ...
      container_name: my-cool-db
      ports:
      - "3306:3306"


      The point to see here is that app is getting a container name depending on the parameter. So basically I have a shell script running the following:



      ENV=$1 docker-compose -p $1 up -d


      So in short, whatever I forward as parameter, the new app container should be brought up. For example if I do sh initializer.sh first I will get app-first container. -p parameter is specified so I can have multiple instances of same container classified as a different project.



      If I have a single container this works great, and I end up with say:



      app-first
      app-second
      app-third


      What I would like to achieve is to have all containers use the same DB. But when I do a docker-compose my DB container still wants to be brought up independent of his existence already.



      Is it an issue that it tries to create a DB under different project name, but with same container name so it causes the collision?



      Can this be made without bringing up 2 separate DB containers?










      share|improve this question
















      I have a docker-compose.yml which get's 2 services up (I have left out all irrelevant data from it).



        app:
      build:
      context: .
      dockerfile: ./docker/app/Dockerfile
      image: ...
      container_name: app-${ENV}
      depends_on:
      - db
      expose:
      - 80

      db:
      image: ...
      container_name: my-cool-db
      ports:
      - "3306:3306"


      The point to see here is that app is getting a container name depending on the parameter. So basically I have a shell script running the following:



      ENV=$1 docker-compose -p $1 up -d


      So in short, whatever I forward as parameter, the new app container should be brought up. For example if I do sh initializer.sh first I will get app-first container. -p parameter is specified so I can have multiple instances of same container classified as a different project.



      If I have a single container this works great, and I end up with say:



      app-first
      app-second
      app-third


      What I would like to achieve is to have all containers use the same DB. But when I do a docker-compose my DB container still wants to be brought up independent of his existence already.



      Is it an issue that it tries to create a DB under different project name, but with same container name so it causes the collision?



      Can this be made without bringing up 2 separate DB containers?







      docker docker-compose dockerfile






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 15:31









      Siyu

      3,20411231




      3,20411231










      asked Nov 16 '18 at 11:16









      NorgulNorgul

      1,64011845




      1,64011845
























          1 Answer
          1






          active

          oldest

          votes


















          1














          A hacky solution:



          change your compose file to



          services:
          app:
          image: ...
          container_name: app-${ENV}
          networks:
          - shared
          expose:
          - 80

          db:
          image: ...
          container_name: my-cool-db
          networks:
          - shared
          ports:
          - "3306:3306"

          networks:
          shared:
          external: true


          Then first create the network docker network create shared



          Bring up db: docker-compose up -d db



          First app: ENV=first docker-compose -p first up -d app



          Second app: ENV=second docker-compose -p second up -d app






          share|improve this answer


























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336787%2fdocker-shared-container-within-multiple-docker-compose-projects%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            A hacky solution:



            change your compose file to



            services:
            app:
            image: ...
            container_name: app-${ENV}
            networks:
            - shared
            expose:
            - 80

            db:
            image: ...
            container_name: my-cool-db
            networks:
            - shared
            ports:
            - "3306:3306"

            networks:
            shared:
            external: true


            Then first create the network docker network create shared



            Bring up db: docker-compose up -d db



            First app: ENV=first docker-compose -p first up -d app



            Second app: ENV=second docker-compose -p second up -d app






            share|improve this answer






























              1














              A hacky solution:



              change your compose file to



              services:
              app:
              image: ...
              container_name: app-${ENV}
              networks:
              - shared
              expose:
              - 80

              db:
              image: ...
              container_name: my-cool-db
              networks:
              - shared
              ports:
              - "3306:3306"

              networks:
              shared:
              external: true


              Then first create the network docker network create shared



              Bring up db: docker-compose up -d db



              First app: ENV=first docker-compose -p first up -d app



              Second app: ENV=second docker-compose -p second up -d app






              share|improve this answer




























                1












                1








                1







                A hacky solution:



                change your compose file to



                services:
                app:
                image: ...
                container_name: app-${ENV}
                networks:
                - shared
                expose:
                - 80

                db:
                image: ...
                container_name: my-cool-db
                networks:
                - shared
                ports:
                - "3306:3306"

                networks:
                shared:
                external: true


                Then first create the network docker network create shared



                Bring up db: docker-compose up -d db



                First app: ENV=first docker-compose -p first up -d app



                Second app: ENV=second docker-compose -p second up -d app






                share|improve this answer















                A hacky solution:



                change your compose file to



                services:
                app:
                image: ...
                container_name: app-${ENV}
                networks:
                - shared
                expose:
                - 80

                db:
                image: ...
                container_name: my-cool-db
                networks:
                - shared
                ports:
                - "3306:3306"

                networks:
                shared:
                external: true


                Then first create the network docker network create shared



                Bring up db: docker-compose up -d db



                First app: ENV=first docker-compose -p first up -d app



                Second app: ENV=second docker-compose -p second up -d app







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 16 '18 at 13:36

























                answered Nov 16 '18 at 13:27









                SiyuSiyu

                3,20411231




                3,20411231
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336787%2fdocker-shared-container-within-multiple-docker-compose-projects%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    List item for chat from Array inside array React Native

                    Thiostrepton

                    Caerphilly