How to expose docker container port into localhost port with docker-compose up?

Viewed 483

I want to expose docker container port into localhost port. I know that it is possible with docker run -it --expose 8010 -p 8010:8010 myContainer or with --net=host but I want to use docker-compose up command and docker-compose file, and when I used network_mode: host my Spring app (which is service on docker compose file) don't want to connect with db (which is another service on container) and also I used expose: 8010 but this doesn't work, so the question is how to expose port from docker container to e.g. localhost:8080 with docker-compose?

Here is my docker-compose file:

version: '3.9'
services:
  db:
    container_name: mysql-db
    image: mysql:5.7
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_USER=my_user
      - MYSQL_PASSWORD=my_pass
      - MYSQL_DATABASE=my_db
    ports:
      - "3307:3306"
    volumes:
      - mysql:/var/lib/mysql
    healthcheck:
      test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
      timeout: 20s
      retries: 10

  app-backend:
    container_name: my-app
    image: app
    restart: on-failure
    environment:
      - _JAVA_OPTIONS=-Xmx512m -Xms256m
      - SERVER_PORT= 8010
      - SPRING_PROFILES_ACTIVE=prod
      - SPRING_DATASOURCE_URL=jdbc:mysql://db:3306/my_db?useLegacyDatetimeCode=false&serverTimezone=UTC
    links:
      - db
    ports:
      - "8010:8010"
    build:
    context: .
    dockerfile: "Dockerfile"
    depends_on:
      db:
        condition: service_healthy

volumes:
  mysql:
0 Answers
Related