This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 1 year ago.
I have containerized a simple PHP app. It needs to connect to the host's database (localhost) and do some operations. However, while running the app, I have the following error:
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory ...
My docker-compose file looks like this:
version: "3.9"
services:
app:
build: .
stdin_open: true
tty: true
network_mode: "host"
restart: on-failure
Looks like dockerized app can't connect with localhost database. I added network_mode: "host", but it didn't help.
I use Docker Desktop and Mac OS.
Instead of localhost I had to use docker.for.mac.localhost in my PHP script.
Related
This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 10 months ago.
I have a host that runs a native mysql installation (not a container).
From a docker container I now want to connect from a java spring-boot application to that port (3306 by default).
But it does not work:
docker-compose.yml:
version: '3.7'
services:
customer-app:
ports:
- "3306:3306"
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://localhost:3306/db
Result from docker-compose up:
Cannot start service customer-app: driver failed programming external connectivity on endpoint:
Error starting userland proxy: listen tcp4 0.0.0.0:3306: bind: address already in use
This is probably not a question directly to a java application, but more general:
How can I access a port on the host system from inside a docker container?
I added the following to docker-compose.yml:
extra_hosts:
- "host.docker.internal:host-gateway"
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://host.docker.internal:3306/db
This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Communication between multiple docker-compose projects
(20 answers)
Closed 1 year ago.
I have two apps (microservices) in separate docker composes
app1.yml
version: "3.4"
services:
app1:
image: flask-app1
environment:
- APP2_URL=http://localhost:8000
ports:
- 5000:8000
volumes:
- "../:/app/"
depends_on:
- db_backend1
restart: on-failure
db_backend1:
...
app2.yml
version: "3.4"
services:
app2:
image: flask-app2
ports:
- 8000:8000
volumes:
- "..:/app"
restart: on-failure
of course they have other dependecies (database server, etc)
i need to run both of them locally, each of them can run well locally, but in this case app1 need to fetch data from app2 by sending a http get request, so i set the app2 url (http://localhost:8000) as an environment variable (just for dev purposes). but the always get requests exception error saying the connection end closed.
So, it will be great if anyone knows how to sort it out.
The container is a “device” so it has it’s own “localhost” so when you set the url as is, it’s called internally which is not what you want.
The solution is to create a network between the composes so you can refer to the specific container as “containerName:port”.
You can refer to :
Communication between multiple docker-compose projects
This question already has answers here:
How to get Docker containers to talk to each other while running on my local host?
(4 answers)
Closed 2 years ago.
I want my Docker containers to work on the same IP. Is it possible? I want them to have the same IP address so that they can link to each other through it.
Have a look at https://docs.docker.com/compose/networking/ to learn how containers are made accessible with docker-compose.
The gist of it is that you access containers by the name you've given them in the compose-file. So in that example
version: "3.9"
services:
web:
build: .
ports:
- "8000:8000"
db:
image: postgres
ports:
- "8001:5432"
you can address the hosts as web and db.
I am trying to link my api with my webapp but is doesn't seem to work.
I have this error
[HPM] Error occurred while trying to proxy request /users/me from
localhost:3000 to http://localhost:8080 (ECONNREFUSED)
(https://nodejs.org/api/errors.html#errors_common_system_errors)
When I try to sign in, it doesn't find the users.
Here is the contents of my docker-compose.yml file
version: '3'
services:
api:
build: ./web3-2019-api
ports:
- "8080:8080"
webapp:
build: ./web3-2019-webapp
ports:
- "3000:3000"
links:
- api
Try to connect via docker host api:8080 instead of localhost.
If you connect via localhost from webapp it expects 8080 to be running in webapp docker, but api is another docker and you should connect via api:8080. Though both are running in same machine they are virtual machines and you should connect via respective docker name within docker network
I checked many forum entries (e.g. in stackoverflow too) but I still cannot figure out what the problem is with my docker-compose file.
So when I start my application (content-app) I got the following exception:
Failed to obtain JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=content-database)(port=3306)(type=master) : Connection refused (Connection refused)
My application is a Spring boot app that tries to connect to the database, the JDBC URL is
url: jdbc:mariadb://content-database:3306/contentdb?autoReconnect=true
The Spring Boot app works fine as locally (when no docker is used) can connect to the local mariadb.
So the content-app container don't see the content-database container. I read that if I specify a network and I assign the containers to the network then they should be able to connect to each other.
When I connect to the running content-app container then I can telnet to content-database
root#894628d7bdd9:/# telnet content-database 3306
Trying 172.28.0.3...
Connected to content-database.
Escape character is '^]'.
n
5.5.5-10.4.3-MariaDB-1:10.4.3+maria~bionip/4X#wW/�#_9<b[~)N.:ymysql_native_passwordConnection closed by foreign host.
My docker-compose yaml file:
version: '3.3'
networks:
net_content:
services:
content-database:
image: content-database:latest
build:
context: .
dockerfile: ./database/Dockerfile
networks:
- net_content
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
content-redis:
image: content-redis:latest
build:
context: .
dockerfile: ./redis/Dockerfile
networks:
- net_content
content-app:
image: content-app:latest
build:
context: .
dockerfile: ./content/Dockerfile
networks:
- net_content
depends_on:
- "content-database"
Any hint please?
Thanks!
I guess MariaDB is listening on default port 3307, this means your application has to connect to this port as well. I guess this is the case as you are mapping the port 3307 of your container to "the outside".
Change the port in your connection string:
url: jdbc:mariadb://content-database:3307/contentdb?autoReconnect=true
You have to expose the port on which content-database is listening in the Dockerfile at ./database/Dockerfile