Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing port in docker-compose.yml

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?

like image 778
Martin Delille Avatar asked Nov 11 '16 17:11

Martin Delille


People also ask

Can we change the port number of docker container?

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.

What is ports in Docker compose?

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.


1 Answers

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

like image 138
Kim T Avatar answered Oct 20 '22 04:10

Kim T