Connect .NET Core Web API to MySQL on different docker container












-1















I have 2 docker containers running on same virtual machine (Ubuntu server 18.04 on VMWare workstation 12 Player). The first one is MySql Container, which is running on port 3306 and the second one is asp.net core (v2.0) web api (port 5000 on vm and export outside through nginx with port 80 ). My VM api is 192.168.20.197



project architecture image



My connection string on web api project is: optionsBuilder.UseMySQL("server=localhost;port=3306;database=mydatabase;user=root;CharSet=utf8");



My docker file content is



FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app


COPY *.csproj ./
RUN dotnet restore


COPY . ./
RUN dotnet publish -c Release -o out


FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DemoMySql.dll"]


I have tried to make a HTTP request to the web api on VM but server response error(500) when i tried to interact with the database (the web api is still work normally when i make it return a sample string such as 192.168.20.197/api/values/samplestring). So How can i connect the web api to mysql on different container ?



p/s: Sorry for my bad grammar










share|improve this question

























  • Can you provide your docker run commands and/or your docker-compose.yml file? What ever you use to run your containers. You are pointing your connection string to localhost, and in a container the localhost is the container, and not the computer that is running the container.

    – Nathan Werry
    Nov 29 '18 at 2:27













  • i changed my localhost to the vm ip address and it's work :) thks

    – MayBeNextTime
    Nov 30 '18 at 4:31
















-1















I have 2 docker containers running on same virtual machine (Ubuntu server 18.04 on VMWare workstation 12 Player). The first one is MySql Container, which is running on port 3306 and the second one is asp.net core (v2.0) web api (port 5000 on vm and export outside through nginx with port 80 ). My VM api is 192.168.20.197



project architecture image



My connection string on web api project is: optionsBuilder.UseMySQL("server=localhost;port=3306;database=mydatabase;user=root;CharSet=utf8");



My docker file content is



FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app


COPY *.csproj ./
RUN dotnet restore


COPY . ./
RUN dotnet publish -c Release -o out


FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DemoMySql.dll"]


I have tried to make a HTTP request to the web api on VM but server response error(500) when i tried to interact with the database (the web api is still work normally when i make it return a sample string such as 192.168.20.197/api/values/samplestring). So How can i connect the web api to mysql on different container ?



p/s: Sorry for my bad grammar










share|improve this question

























  • Can you provide your docker run commands and/or your docker-compose.yml file? What ever you use to run your containers. You are pointing your connection string to localhost, and in a container the localhost is the container, and not the computer that is running the container.

    – Nathan Werry
    Nov 29 '18 at 2:27













  • i changed my localhost to the vm ip address and it's work :) thks

    – MayBeNextTime
    Nov 30 '18 at 4:31














-1












-1








-1








I have 2 docker containers running on same virtual machine (Ubuntu server 18.04 on VMWare workstation 12 Player). The first one is MySql Container, which is running on port 3306 and the second one is asp.net core (v2.0) web api (port 5000 on vm and export outside through nginx with port 80 ). My VM api is 192.168.20.197



project architecture image



My connection string on web api project is: optionsBuilder.UseMySQL("server=localhost;port=3306;database=mydatabase;user=root;CharSet=utf8");



My docker file content is



FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app


COPY *.csproj ./
RUN dotnet restore


COPY . ./
RUN dotnet publish -c Release -o out


FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DemoMySql.dll"]


I have tried to make a HTTP request to the web api on VM but server response error(500) when i tried to interact with the database (the web api is still work normally when i make it return a sample string such as 192.168.20.197/api/values/samplestring). So How can i connect the web api to mysql on different container ?



p/s: Sorry for my bad grammar










share|improve this question
















I have 2 docker containers running on same virtual machine (Ubuntu server 18.04 on VMWare workstation 12 Player). The first one is MySql Container, which is running on port 3306 and the second one is asp.net core (v2.0) web api (port 5000 on vm and export outside through nginx with port 80 ). My VM api is 192.168.20.197



project architecture image



My connection string on web api project is: optionsBuilder.UseMySQL("server=localhost;port=3306;database=mydatabase;user=root;CharSet=utf8");



My docker file content is



FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app


COPY *.csproj ./
RUN dotnet restore


COPY . ./
RUN dotnet publish -c Release -o out


FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DemoMySql.dll"]


I have tried to make a HTTP request to the web api on VM but server response error(500) when i tried to interact with the database (the web api is still work normally when i make it return a sample string such as 192.168.20.197/api/values/samplestring). So How can i connect the web api to mysql on different container ?



p/s: Sorry for my bad grammar







mysql .net docker .net-core asp.net-core-webapi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 9:11







MayBeNextTime

















asked Nov 25 '18 at 8:36









MayBeNextTimeMayBeNextTime

313




313













  • Can you provide your docker run commands and/or your docker-compose.yml file? What ever you use to run your containers. You are pointing your connection string to localhost, and in a container the localhost is the container, and not the computer that is running the container.

    – Nathan Werry
    Nov 29 '18 at 2:27













  • i changed my localhost to the vm ip address and it's work :) thks

    – MayBeNextTime
    Nov 30 '18 at 4:31



















  • Can you provide your docker run commands and/or your docker-compose.yml file? What ever you use to run your containers. You are pointing your connection string to localhost, and in a container the localhost is the container, and not the computer that is running the container.

    – Nathan Werry
    Nov 29 '18 at 2:27













  • i changed my localhost to the vm ip address and it's work :) thks

    – MayBeNextTime
    Nov 30 '18 at 4:31

















Can you provide your docker run commands and/or your docker-compose.yml file? What ever you use to run your containers. You are pointing your connection string to localhost, and in a container the localhost is the container, and not the computer that is running the container.

– Nathan Werry
Nov 29 '18 at 2:27







Can you provide your docker run commands and/or your docker-compose.yml file? What ever you use to run your containers. You are pointing your connection string to localhost, and in a container the localhost is the container, and not the computer that is running the container.

– Nathan Werry
Nov 29 '18 at 2:27















i changed my localhost to the vm ip address and it's work :) thks

– MayBeNextTime
Nov 30 '18 at 4:31





i changed my localhost to the vm ip address and it's work :) thks

– MayBeNextTime
Nov 30 '18 at 4:31












2 Answers
2






active

oldest

votes


















1














Thanks for @Tao Zhou and @Nathan Werry advices, I solved the problem by replacing the localhost in connection string to the ip address of my virtual machine. Then I used docker --link tag (legacy feature of docker) to link the mysql container to the web api container.



docker run 
--name <webapi-container-name>
-p 8082:8081
--link <mysql-container-name>:<mysql-image>
-d <webapi-image>





share|improve this answer

































    0














    For connecting from web api to mysql, you could add Compose which will create a shared network for web api and mysql, then you could access mysql with service container name.



    version: "3"
    services:
    web:
    build: .
    ports:
    - "8000:8000"
    db:
    image: postgres
    ports:
    - "8001:5432"


    You could access db by postgres://db:5432, refer Networking in Compose.



    For another option, you could create your own Bridge networks to share the network between two containers. refer Bridge networks.






    share|improve this answer
























    • i have never worked with docker-compose so i use --link to connect web api container to mysql container. I will try the docker-compose later :)

      – MayBeNextTime
      Nov 30 '18 at 4:34











    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%2f53465883%2fconnect-net-core-web-api-to-mysql-on-different-docker-container%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Thanks for @Tao Zhou and @Nathan Werry advices, I solved the problem by replacing the localhost in connection string to the ip address of my virtual machine. Then I used docker --link tag (legacy feature of docker) to link the mysql container to the web api container.



    docker run 
    --name <webapi-container-name>
    -p 8082:8081
    --link <mysql-container-name>:<mysql-image>
    -d <webapi-image>





    share|improve this answer






























      1














      Thanks for @Tao Zhou and @Nathan Werry advices, I solved the problem by replacing the localhost in connection string to the ip address of my virtual machine. Then I used docker --link tag (legacy feature of docker) to link the mysql container to the web api container.



      docker run 
      --name <webapi-container-name>
      -p 8082:8081
      --link <mysql-container-name>:<mysql-image>
      -d <webapi-image>





      share|improve this answer




























        1












        1








        1







        Thanks for @Tao Zhou and @Nathan Werry advices, I solved the problem by replacing the localhost in connection string to the ip address of my virtual machine. Then I used docker --link tag (legacy feature of docker) to link the mysql container to the web api container.



        docker run 
        --name <webapi-container-name>
        -p 8082:8081
        --link <mysql-container-name>:<mysql-image>
        -d <webapi-image>





        share|improve this answer















        Thanks for @Tao Zhou and @Nathan Werry advices, I solved the problem by replacing the localhost in connection string to the ip address of my virtual machine. Then I used docker --link tag (legacy feature of docker) to link the mysql container to the web api container.



        docker run 
        --name <webapi-container-name>
        -p 8082:8081
        --link <mysql-container-name>:<mysql-image>
        -d <webapi-image>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 30 '18 at 13:11









        Nic3500

        3,35081829




        3,35081829










        answered Nov 30 '18 at 4:30









        MayBeNextTimeMayBeNextTime

        313




        313

























            0














            For connecting from web api to mysql, you could add Compose which will create a shared network for web api and mysql, then you could access mysql with service container name.



            version: "3"
            services:
            web:
            build: .
            ports:
            - "8000:8000"
            db:
            image: postgres
            ports:
            - "8001:5432"


            You could access db by postgres://db:5432, refer Networking in Compose.



            For another option, you could create your own Bridge networks to share the network between two containers. refer Bridge networks.






            share|improve this answer
























            • i have never worked with docker-compose so i use --link to connect web api container to mysql container. I will try the docker-compose later :)

              – MayBeNextTime
              Nov 30 '18 at 4:34
















            0














            For connecting from web api to mysql, you could add Compose which will create a shared network for web api and mysql, then you could access mysql with service container name.



            version: "3"
            services:
            web:
            build: .
            ports:
            - "8000:8000"
            db:
            image: postgres
            ports:
            - "8001:5432"


            You could access db by postgres://db:5432, refer Networking in Compose.



            For another option, you could create your own Bridge networks to share the network between two containers. refer Bridge networks.






            share|improve this answer
























            • i have never worked with docker-compose so i use --link to connect web api container to mysql container. I will try the docker-compose later :)

              – MayBeNextTime
              Nov 30 '18 at 4:34














            0












            0








            0







            For connecting from web api to mysql, you could add Compose which will create a shared network for web api and mysql, then you could access mysql with service container name.



            version: "3"
            services:
            web:
            build: .
            ports:
            - "8000:8000"
            db:
            image: postgres
            ports:
            - "8001:5432"


            You could access db by postgres://db:5432, refer Networking in Compose.



            For another option, you could create your own Bridge networks to share the network between two containers. refer Bridge networks.






            share|improve this answer













            For connecting from web api to mysql, you could add Compose which will create a shared network for web api and mysql, then you could access mysql with service container name.



            version: "3"
            services:
            web:
            build: .
            ports:
            - "8000:8000"
            db:
            image: postgres
            ports:
            - "8001:5432"


            You could access db by postgres://db:5432, refer Networking in Compose.



            For another option, you could create your own Bridge networks to share the network between two containers. refer Bridge networks.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 26 '18 at 7:07









            Tao ZhouTao Zhou

            6,97131332




            6,97131332













            • i have never worked with docker-compose so i use --link to connect web api container to mysql container. I will try the docker-compose later :)

              – MayBeNextTime
              Nov 30 '18 at 4:34



















            • i have never worked with docker-compose so i use --link to connect web api container to mysql container. I will try the docker-compose later :)

              – MayBeNextTime
              Nov 30 '18 at 4:34

















            i have never worked with docker-compose so i use --link to connect web api container to mysql container. I will try the docker-compose later :)

            – MayBeNextTime
            Nov 30 '18 at 4:34





            i have never worked with docker-compose so i use --link to connect web api container to mysql container. I will try the docker-compose later :)

            – MayBeNextTime
            Nov 30 '18 at 4:34


















            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%2f53465883%2fconnect-net-core-web-api-to-mysql-on-different-docker-container%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

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga