libresh / compose-matomo

Matomo docker-compose application for libre.sh-v1
GNU Affero General Public License v3.0
179 stars 67 forks source link

docker-compose.yml - change default ports #6

Closed keltik85 closed 7 years ago

keltik85 commented 8 years ago

First of all: I am quite new to docker and some basics may not be that clear to me.

When I run your docker-compose.yml just like it is written in the How to use this image:

git clone https://github.com/indiehosters/piwik.git
cd docker-piwik
# edit variables in the docker-compose file or pass them from your comamnd line
docker-compose up

I see with docker ps that the ports mapped by docker-compose.yml are:

[root@localhost piwik]# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                    NAMES
3975b32cd92c        piwik                    "bash -c 'bash -s <<E"   6 minutes ago       Up 6 minutes        9000/tcp                 piwik_cron_1
c64ceaa0eb4f        nginx                    "nginx -g 'daemon off"   6 minutes ago       Up 6 minutes        80/tcp, 443/tcp          piwik_web_1
822381268f73        piwik                    "/entrypoint.sh php-f"   6 minutes ago       Up 6 minutes        9000/tcp                 piwik_app_1
8bd7d007626c        mysql                    "/entrypoint.sh mysql"   7 minutes ago       Up 7 minutes        3306/tcp                 piwik_db_1

Basically 9000, 80, 3306 and 443 are the exposed ports. I tried to change them by supplying ports: inside the docker-compose.yml file:

[root@localhost ~]# cat /sdb1/repos/piwik/docker-compose.yml
db:
  image: mysql
  volumes:
    - ./mysql/runtime:/var/lib/mysql
  environment:
    - MYSQL_ROOT_PASSWORD=myrootpw
app:
  image: piwik
  ports:
    - "9001"
  links:
    - db
  volumes:
    - ./config:/var/www/html/config
    - ./ssmtp.conf:/etc/ssmtp/ssmtp.conf
    - ./revaliases:/etc/ssmtp/revaliases
web:
  image: nginx
  volumes:
    - ./nginx.conf:/etc/nginx/nginx.conf:ro
  links:
    - app
  volumes_from:
    - app
cron:
  image: piwik
  ports:
    - "9001"
  links:
    - db
  volumes_from:
    - app
  entrypoint: |
    bash -c 'bash -s <<EOF
    trap "break;exit" SIGHUP SIGINT SIGTERM
    while /bin/true; do
      su -s "/bin/bash" -c "/usr/local/bin/php /var/www/html/console core:archive" www-data
      sleep 3600
    done
    EOF'

But as it turns out this just creates additional ports, because docker inspect piwik_app_1 shows:

...
"NetworkSettings": {
            "Bridge": "",
            "SandboxID": "a68518308aaaeeee52848310637d45f8fd323e530c47074a7eacecca2156433d",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "9000/tcp": null,
                "9001/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "32777"
                    }
                ]
            },
...

And I can double check this again with docker ps:

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                               NAMES
7f0cbbc39c5f        piwik                    "bash -c 'bash -s <<E"   12 minutes ago      Up 12 minutes       9000/tcp, 0.0.0.0:32778->9001/tcp   piwik_cron_1
c9ffd24c6a82        nginx                    "nginx -g 'daemon off"   12 minutes ago      Up 12 minutes       80/tcp, 443/tcp                     piwik_web_1
0aecdb52d2c4        piwik                    "/entrypoint.sh php-f"   12 minutes ago      Up 12 minutes       9000/tcp, 0.0.0.0:32777->9001/tcp   piwik_app_1
bc39a2564166        mysql                    "/entrypoint.sh mysql"   12 minutes ago      Up 12 minutes       3306/tcp                            piwik_db_1

So my question is:
How to change the default ports (9000,3306,443, 80) of the containers which are created by your docker-compose.yml file, when calling docker-compose up?

stefancrain commented 8 years ago

I ran into a few things like this on another project. If you are changing your config but haven't removed the network from your pervious build the network can persist the next time you restart the container.

docker-compose stop doesn't remove its previous configuration, docker-compose down does See below for the help output.

$ docker-compose stop --help
Stop running containers without removing them.

They can be started again with `docker-compose start`.

Usage: stop [options] [SERVICE...]

Options:
  -t, --timeout TIMEOUT      Specify a shutdown timeout in seconds.
                             (default: 10)
$ docker-compose down –help
Stop containers and remove containers, networks, volumes, and images
created by `up`. Only containers and networks are removed by default.

Usage: down [options]

Options:
    --rmi type      Remove images, type may be one of: 'all' to remove
                    all images, or 'local' to remove only images that
                    don't have an custom name set by the `image` field
    -v, --volumes   Remove data volumes
$ docker-compose --version
docker-compose version 1.6.0, build d99cad6

Changing the internal ports can get messy as the services in these containers talk to each other on those ports, for example nginx would need to be configured to work on 9001 rather then 9000 See line 8 of the nginx config file for more info.

If you are trying to expose these ports to the outside world you're going to need to specify them as a pair (HOST:CONTAINER).

  ports:
    - "443:443"
    - "80:80"

For more info on setting up the port section, see https://docs.docker.com/v1.8/compose/yml/#ports

pierreozoux commented 7 years ago

Thanks @stefancrain for the answer. @keltik85 did it solve your issue?

pierreozoux commented 7 years ago

@keltik85

Actually after reading one more time, this was not really related. For you the issue is to understand what is an exposed port in a docker container.

I recommend you reading this: https://docs.docker.com/engine/userguide/networking/

Let me know if you still have doubts.