I'm learning how to use docker-compose following the official documentation: https://docs.docker.com/compose/gettingstarted/
When browsing to http://myserver.com:5000 I have the expected result:
Hello World! I have been seen 1 times.
I would like to change the listening port to 5001 modifying the docker-compose.yml file as follow:
version: '2'
services:
web:
build: .
ports:
- "5001:5001"
volumes:
- .:/code
depends_on:
- redis
redis:
image: redis
Unfortunately, after stop and removing the container (with 'docker-compose down') and start it again (with 'docker-compose up -d'), the connection to http://myserver.com:5001 is refused.
Any idea?
You can change the port mapping by directly editing the hostconfig. json file at /var/lib/docker/containers/[hash_of_the_container]/hostconfig. json or /var/snap/docker/common/var-lib-docker/containers/[hash_of_the_container]/hostconfig.
Ports is defined as:Either specify both ports (HOST:CONTAINER), or just the container port (a random host port will be chosen). Ports mentioned in docker-compose. yml will be shared among different services started by the docker-compose. Ports will be exposed to the host machine to a random port or a given port.
You should change the external port only (the first port number in xxxx:xxxx
)
version: '2'
services:
web:
build: .
ports:
- "5001:5000"
volumes:
- .:/code
depends_on:
- redis
redis:
image: redis
Link to documentation: https://docs.docker.com/compose/compose-file/compose-file-v3/#ports
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With