Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose - Expose linked service port

I'm trying to setup a SonarQube container backed by a MySQL database container. My docker-compose.yml:

sonar:
  environment:
    - SONARQUBE_USER=sonar
    - SONARQUBE_PASSWORD=sonar
    - SONARQUBE_DATABASE=sonar
    - SONARQUBE_JDBC_URL=jdbc:mysql://db:3306/sonar?useUnicode=true&characterEncoding=utf8
  build: .
  ports:
    - "19000:9000"
    - "19306:3306"
  links:
    - db
db:
  environment:
    - MYSQL_ROOT_PASSWORD=root-secret
    - MYSQL_USER=sonar
    - MYSQL_PASSWORD=sonar
    - MYSQL_DATABASE=sonar
  image: mysql

In the ports section I'm trying to expose both port 9000 from SonarQube (web interface) and port 3306 (MySQL connection).

Is there any way to expose a port from a linked service (such as db in this case) from the "main" container e.g. sonar?

EDIT: Just to better explain my needs, I want to expose both ports to my localhost. I need access to both ports from my machine, as I SonarQube runner needs access to the database and I want to run some queries in the database too, from my machine, not inside another container.

like image 459
resilva87 Avatar asked Dec 17 '15 12:12

resilva87


1 Answers

You don't need to: an EXPOSE port from one service is directly visible from another (linking to the first).

No port mapping necessary (as you do for 9000 from SonarQube and 3306)
Port mapping is necessary for accessing a container from the host.
But from container to a (linked) container (both managed by the same docker daemon), any port declared in EXPOSE in its Dockerfile is directly accessible.

I want to expose both ports to my localhost. I need access to both ports from my machine, as I SonarQube runner needs access to the database

Well then,... the db section should have its own port mapping section:

db:
  ports:
    - "xxx:yyyy"
like image 61
VonC Avatar answered Nov 09 '22 18:11

VonC