Connect .NET Core Web API to MySQL on different docker container
![Multi tool use Multi tool use](http://sgv.ssvwv.com/sg/ssvwvcomimagb.png)
Multi tool use
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
add a comment |
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
Can you provide yourdocker run
commands and/or yourdocker-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
add a comment |
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
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
mysql .net docker .net-core asp.net-core-webapi
edited Nov 25 '18 at 9:11
MayBeNextTime
asked Nov 25 '18 at 8:36
MayBeNextTimeMayBeNextTime
313
313
Can you provide yourdocker run
commands and/or yourdocker-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
add a comment |
Can you provide yourdocker run
commands and/or yourdocker-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
add a comment |
2 Answers
2
active
oldest
votes
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>
add a comment |
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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>
add a comment |
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>
add a comment |
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>
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>
edited Nov 30 '18 at 13:11
![](https://i.stack.imgur.com/AdwXN.png?s=32&g=1)
![](https://i.stack.imgur.com/AdwXN.png?s=32&g=1)
Nic3500
3,35081829
3,35081829
answered Nov 30 '18 at 4:30
MayBeNextTimeMayBeNextTime
313
313
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
answered Nov 26 '18 at 7:07
![](https://i.stack.imgur.com/uf6qO.jpg?s=32&g=1)
![](https://i.stack.imgur.com/uf6qO.jpg?s=32&g=1)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Us,INdS0Uuaem 2mj,x6kS,U
Can you provide your
docker run
commands and/or yourdocker-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