Reputation: 762
I am having trouble wrapping my head around how to configure what I consider to be a pretty basic network setup in Docker.
Containers:
I've got the Wordpress & MariaDB containers up and running and chatting with eachother, but for some reason my Node.js container is unable to perform a basic HTTP call out to the Wordpress server. I immediately receive an Error: connect ECONNREFUSED 192.168.128.4:3000
error from my Node script.
I am running Docker Desktop Community on Mac OS v2.3.0.3.
My docker-compose.yml
file looks like this:
services:
db:
image: mariadb:10.5.4-focal
container_name: db
volumes:
- ./cms/conf/mysql/data:/var/lib/mysql
- ./cms/sql:/docker-entrypoint-initdb.d
environment:
- MYSQL_ROOT_PASSWORD=******
- MYSQL_USER=******
- MYSQL_PASSWORD=******
- MYSQL_DATABASE=wordpress
restart: always
cms.mysite.local:
container_name: cms.mysite.local
build:
context: ./cms
image: mysite/cms:5.4.2
ports:
- '3000:80'
volumes:
- ./cms/conf/php/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
- ./cms/conf/nginx:/etc/nginx/conf.d
- ./cms/logs/nginx:/var/log/nginx
- ./cms/app/public:/var/www/html
environment:
- WORDPRESS_DB_NAME=wordpress
- WORDPRESS_TABLE_PREFIX=wp_
- WORDPRESS_DB_HOST=db
- WORDPRESS_DB_USER=root
- WORDPRESS_DB_PASSWORD=password
depends_on:
- db
restart: always
node:
container_name: node
build:
context: ./www
image: mysite/gatsby:2.23.12
user: 'node'
working_dir: /home/node/www
volumes:
- ./www:/home/node/www
- /home/node/www/node_modules
ports:
- '8000:8000'
depends_on:
- cms.mysite.local
restart: "always"
The cms.mysite.local
container has an open port specified at 3000
. I modified my local Mac OS machine's /etc/hosts
file to add a 127.0.0.1 cms.mysite.local
entry, and I am able to hit the Wordpress site in my browser and get a valid response. So it looks like everything is working there.
Then I have the following script set up in the node
container:
const axios = require('axios')
const url = 'http://cms.mysite.local:80'
console.log('URL=' + url)
axios.get(url).then( (response) => {
console.log('response=' + JSON.stringify(response.data))
})
.catch(function (error) {
console.log('error=' + error)
})
I have this script set up to execute when the container starts. Everytime it runs, I get the error: Error: connect ECONNREFUSED 192.168.128.4:3000
. Sure enough, when I try to access 192.168.128.4:3000
manually in my MacOS browser, I do indeed get no connection.
Why is the Node.js script trying to access http://192.168.128.4:3000
instead of my written http://cms.mysite.local:3000/
URL?
How can I force it to use the URL that maps correctly to the Wordpress docker container? Or how can I set up my URL so that my node
container can make HTTP calls to my cms.mysite.local
container?
[Edit #1] I have also tried updating my Node.js script to call out to http://cms.mysite.local:80/
. I added some logging to make the output a little more clear.
> node ./test.js
URL=http://cms.mysite.local:80
error=Error: connect ECONNREFUSED 192.168.128.4:80
[Edit 2: Urg. I did not appropriately stop & rebuild my docker image for edit. I do see the appropriate ports being used after restarting. #1]
[Solve: Thanks to Fernando and Shizzen83 for the port callout!]
Here's what was happening. I had my Wordpress site configured for port 3000 so that I could access it on that port through the browser. But I had a nginx config serving the site on port 80 inside of Docker. So Wordpress was issuing 301 redirects on port 80 requests to port 3000 because the internal Docker port didn't match the external Wordpress port. I switched out nginx to listen on port 3000 directly, and updated my docker-compose file to map 3000:3000
instead of 3000:80
. And now things all line up. Thanks all!
Upvotes: 1
Views: 4282
Reputation: 3529
Your mistake comes from the port used by Node to reach Wordpress. When you set 3000:80
in ports
section of a container, it means nothing more than a query sent onto host on port 3000 will be redirected to internal port 80 of Wordpress container. Though your Node container is not the host, so you have to directly reach port 80 instead.
Upvotes: 1
Reputation: 134
Seems to be the port of cms.mysite.local
you are exposing
ports:
- 3000:80
which means that this container is mapped to port 3000
to outside (your host) but inside the docker network the container still have the 80
for incoming connections.
For your node
container must be something like
axios.get('http://cms.mysite.local:80').then( (response) => {
console.log('response=' + response.data)
})
.catch(function (error) {
// handle error
console.log('error=' + error);
})
And node
shows you the IP beacause he is resolving the DNS of your wordpress container inside the docker network
Upvotes: 3