I am using docker-compose to set up a NextJS-app that fetches data from the Wordpress REST-API running in separate containers.
Problem is, I get ECONNREFUSED when I try to fetch or WGET the wordpress-container http://wordpress:8000 from the NextJS-container. I can ping wordpress:8000 without any problems.
If I use Postman or try to fetch the REST-API from another host (i.e. not the machine running docker-compose) using the public ip, it works perfectly.
I'm suspecting some docker configuration issue, but I'm quite lost as the pinging works but not the wget.
Anyone with an idea on what the culprit could be?
My docker-compose.yml:
version: '2'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: ***REMOVED***
MYSQL_DATABASE: ***REMOVED***
MYSQL_USER: ***REMOVED***
MYSQL_PASSWORD: ***REMOVED***
networks:
- back
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- ./wp-content:/var/www/html/wp-content
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: ***REMOVED****
networks:
- back
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8080:80
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: ***REMOVED***
networks:
- back
next-app:
depends_on:
- wordpress
build:
context: ./next-app
dockerfile: Dockerfile
volumes:
- './next-app:/usr/src/app'
- '/usr/src/app/node_modules'
ports:
- '80:3000'
networks:
- back
express-server:
build:
context: ./express-server
dockerfile: Dockerfile
ports:
- '3001:3001'
networks:
back:
driver: bridge
volumes:
db_data:
From inside the NextJS-container you need to use port 80. Port 8000 it's from your (host) machine. So use http://wordpress:80 from inside the docker containers.
ports:
- "8000:80"
In you docker-compose file just says: "Map my local (host machine) port 8000 to containers ports 80", but inside the docker network, it's still port 80
You can ping, because ping doesn't use ports. Ports that we are talking about are TCP/UDP ports, see https://en.wikipedia.org/wiki/Port_(computer_networking). But ping uses ICMP (Internet Control Message Protocol), which doesn't use ports at all, see https://en.wikipedia.org/wiki/Ping_(networking_utility)
Related
mysql by default listens to ports 3306 and 33060.
since im launching multiple instances, i have to provide alternative ports.
i managed to map the first one <DB_HOST: 8002> to <3306> but i cannot find any documentation for the second wordpress parameter name, i just found DB_HOST.
do you know how i can also map the second port?
version: "3.9"
services:
db:
image: mysql:latest
volumes:
- wordpress_db_data:/var/lib/mysql
ports:
- "8002:3306"
- "8003:33060"
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_data:/var/www/html
ports:
- "80:80"
restart: unless-stopped
environment:
DB_HOST: 8002
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
wordpress_db_data: {}
wordpress_data: {}
On the bridge network that docker-compose creates, you use the container ports directly. You only use the mapped ports when you connect to a container from the host computer.
Since WordPress and MySQL are on the same network, you should use port 3306. You only need to map the MySQL ports to host ports if you intend to connect to the database from the host.
Also, WordPress doesn't connect to MySQL using port 33060, so that's why there's no parameter for it on the WordPress side.
Here's a docker-compose file that should work
version: "3.9"
services:
db:
image: mysql:latest
volumes:
- wordpress_db_data:/var/lib/mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_data:/var/www/html
ports:
- "80:80"
restart: unless-stopped
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
wordpress_db_data: {}
wordpress_data: {}
Note that I've removed the port mappings on the database, since they're not needed for WordPress to work. If you need to connect to the database from the host, you can just add them back. Usually port 3306 is enough. 33060 is rarely used, AFAIK.
I trying to figure out how docker network and that kind of stuff works.
So here is my setup, I have 2 docker containers with WordPress and mysql. And theese two needs to be able to talk to each other thru direct database connections. And I have domain pointed to them also.
So I tried to set it up with an docker nginx proxy, but when i'm trying to visit that site thru the domain i'm getting 502 bat gateway.
So my docker ngxinx proxy compose file looks like this, but with different ports:
version: "3.1"
services:
nginx-proxy:
image: jwilder/nginx-proxy:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./certs:/etc/nginx/certs
- /var/run/docker.sock:/tmp/docker.sock:ro
restart: unless-stopped
networks:
default:
external:
name: nginx-proxy
And both of my WordPress containers look like this:
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
VIRTUAL_HOST: wp1.local
VIRTUAL_PORT: 3000
volumes:
- ./wp:/var/www/html
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
expose:
- 3000
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql
ports:
- "8086:3306"
mailhog:
image: mailhog/mailhog
ports:
- "1025:1025" # smtp server
- "8025:8025" # web ui
networks:
default:
external:
name: nginx-proxy
volumes:
db:
And I have added 127.0.0.1 wp1 to /etc/hosts
But when I start the proxy and one of the WP containers I'm getting bad gateway. And I have no clue how to move forward. And maybe this is not even the right way todo it. Becuase the problem i'm trying to solve is that wp2 needs to be able to connect to wp1 database thru custom pdo connection.
I tried to explain as good as I can, but this is a new area for me when it comes to docker networks. Also i'm running docker for windows with wsl2
You should add
networks:
- default
to each service definition in docker-compose.
version: '3.1'
services:
wordpress:
image: wordpress
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
VIRTUAL_HOST: wp1.local
VIRTUAL_PORT: 3000
volumes:
- ./wp:/var/www/html
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
expose:
- 3000
networks:
- default
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql
ports:
- "8086:3306"
networks:
-default
mailhog:
image: mailhog/mailhog
ports:
- "1025:1025" # smtp server
- "8025:8025" # web ui
networks:
- default
networks:
default:
external:
name: nginx-proxy
volumes:
db:
I have an Ubuntu 16 machine running docker and docker-compose
the docker-compose YML looks like this:
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: abware
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data:
when I run: docker-compose up -d
it installs MySql+WP images and runs the containers and everything is just fine.
then, when I try to access my web site by navigating to: www.my-host.com:8000
it shows the WordPress website but when I navigate to the same URL with port 80 it's not working...
How do I make WordPress to work on the default port 80?
The section:
ports:
- "8000:80"
in the YML maps the host port 8000 to the container port 80. Changing that to 80:80 and rebuilding the container (as mentioned by #Kilan) should resolve the issue.
when you do this ports:
- "8000:80"
you mapped the port 8000 of your host on the port 80 of your container. It's normal if you can't navigate from your host with port 80.
Replace with 80:80, but before this, be sure that this port is free on your host.
I want to map some random port on my computer e.g. localhost:7006 to my WordPress docker container's port 80.When I change the port of WordPress from 80:80 to 7006:80 it's not only stops working on localhost(port 80) but also don't respond on localhost:7006.
docker-compose.yml file looks like this:
version: '3'
services:
wordpress:
depends_on:
- db
image: wordpress:4.7.1
restart: always
volumes:
- ./wp-content:/var/www/html/wp-content
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: p4ssw0rd!
ports:
- 80:80 # Expose http and https
- 8443:443
networks:
- wp_nwk
db:
image: mysql:5.7
restart: always
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: p4ssw0rd!
networks:
- wp_nwk
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 7005:80
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: p4ssw0rd!
networks:
- wp_nwk
networks:
wp_nwk:
volumes:
db_data:
After a bit of research I found out that the WordPress container sets it's ports once since it needs to save the URLs(localhost:7006) in the db because I am persisting the db data.
I ran the docker-compose up once with the default port 80:80 configuration which caused the localhost:80 or localhost to be saved in the db. So when I changed the ports again and ran docker-compose up, I actually messed up the URLs that are stored in the linked mysql db container with my WordPress container.
I ran docker-compose down --volumes (this causes the persisted data destruction)
and then changed the ports of my WordPress container in docker-compse.yml. Running the following command again created my WordPress container live on port 7006 (localhost:7006).
docker-compose up
wordpress:
depends_on:
- db
image: wordpress:4.7.1
restart: always
volumes:
- ./wp-content:/var/www/html/wp-content
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: p4ssw0rd!
ports:
- 7006:80 # Expose http and https
- 8443:443
networks:
- wp_nwk
IMPORTANT: I am just playing with docker, so I don't want to save my
volumes data. Anyone wanting to keep their data must not use the
docker-compose down --volumes
It's running on the desired port now
I|f you want to change the port, you need to do the follow step. I successfully changed my WordPress port
run WordPress with default docker-compose.yml
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
login wordpress and change site url and home in setting to you want
use follow command in wordpress container
docker exec -it *wordpres_container_id* bash
add the follow line to wp_config.php
define( 'WP_HOME', 'http://example.com' );
define( 'WP_SITEURL', 'http://example.com' );
change docker-compose.yml ports to 80
restart container use follow command
docker-compose down
docker-compose up -d
you will need to change the [WordPress Address (URL) and Site Address (URL)] from wordpress admin, and then change the port in docker-compose without destroying the data in the volume.
I have to start up 3 containers, nginx, wordpress and wordpress2. The problem is the nginx container starts at 172.17.0.2, wordpress at 172.18.0.3 and wordpress2 at 172.19.0.4.
The wordpress containers starts with their docker-compose.yml configured as i show:
For wordpress:
version: '2'
services:
db:
image: mysql:5.7
volumes:
- "./.data/db:/var/lib/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
links:
- db
ports:
- "8000:80"
networks:
- ipv4_address: 172.17.0.5
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: wordpress
For wordpress2:
version: '2'
services:
db:
image: mysql:5.7
volumes:
- "./.data/db:/var/lib/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
links:
- db
ports:
- "8001:80"
networks:
- ipv4_address: 172.17.0.6
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: wordpress
As you can see, i put at networks field the ipv4_address but when i start up the docker-compose.yml it shows error at this line. I thought that by default, all containers usually stars at same network but not in this case. Can you help me to put all containers at same subnet?
Each docker-compose.yml will create it's own subnet. So your 3 containers can't see each other.
Networks created by other docker-compose are said to be external so you have to declare them as such. You can see these networks using docker network ls.
After declaring the external networks in your external networks in your docker-compose.yml file, you will have to define what networks should be using by the nginx service. You should keep the default network.
Usually the default name for your docker-compose network is <current_dir>_default. So suppose your current directory is called yourproject1 the network name is going to be yourproject1_default.
And then you will be able to access your wordpress containers using external_links (better than ip addresses IMO)
Here what I would do for nginx's docker-compose.yml file to make him able to see wordpress containers :
version: '2'
services:
nginx:
...
external_links:
- your_project1_wordpress1_1:wordpress1
- your_other_project2_wordpress2_1:wordpress2
networks:
- default
- your_project1_default
- your_other_project2_defaul
networks:
your_project1_default:
external: true
your_other_project2_default:
external: true