Reputation: 486
I have an application where i can create several wordpress project each one with a docker-compose.yml file to launch a local wordpress env.
I setted:
mysql: ports: - 8081:3306
wordpress ports: - 8080:80
I would like to each project has a unique port configuration to allow launch all if i need it or to dont use a used port and return an error.
So, what range of ports are available to use for mysql and wordpress?
Do you know if it is possible or is a bad idea?
Thanks you
Upvotes: 0
Views: 743
Reputation: 638
Totally possible! The trick is to only specify the port that is internal to the container. Then, when docker-compose boots up, it'll automatically assign a host port mapping to the internal container port.
For instance, if you define your docker-compose.yaml like so:
version: '3'
services:
mysql:
image: mysql
ports:
- 3306
environment:
- MYSQL_ROOT_PASSWORD=badpassworddontdothis
wordpress:
image: wordpress
ports:
- 80
And run docker-compose up
, we can look at the port mappings that were assigned:
❯ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------------
67077886_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:55362->3306/tcp, 33060/tcp
67077886_wordpress_1 docker-entrypoint.sh apach ... Up 0.0.0.0:55361->80/tcp
This shows that you can access the mysql container on host port 55362, and wordpress on port 55361.
If you need to programatically get the exposed port for a given service, you can run:
❯ docker-compose port wordpress 80
0.0.0.0:55361
and you'll get back the ip/port pair that will allow you to talk to wordpress container port 80:
❯ curl -v $(docker-compose port wordpress 80)
* Trying 0.0.0.0...
* TCP_NODELAY set
* Connected to 0.0.0.0 (127.0.0.1) port 55361 (#0)
> GET / HTTP/1.1
> Host: 0.0.0.0:55361
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 302 Found
< Date: Tue, 13 Apr 2021 20:09:03 GMT
< Server: Apache/2.4.38 (Debian)
< X-Powered-By: PHP/7.4.16
< Location: http://0.0.0.0:55361/wp-admin/setup-config.php
< Content-Length: 0
< Content-Type: text/html; charset=UTF-8
<
* Connection #0 to host 0.0.0.0 left intact
* Closing connection 0
(The 302 is because I didn't actually set up wordpress, but it's still a response from the server so it counts).
Upvotes: 1