Run MySQL 8 on Port 33308 Using Docker Compose

In some cases, we would like to run MySQL image on a different PORT other than the default one 3306. Suppose we have deployed your back-end application on a server and you find out that there is another service running on port 3306.

The solution is just to use the docker-compose supported environment variable
MYSQL_TCP_PORT: 33308 under the MYSQL image environments: the section as shown below:

mysql8:
    container_name: mysql8
    hostname: mysql8
    image: mysql:8.0.23
    restart: always
    working_dir: /code
    volumes:
      - "./:/code"
      - $PWD/docker/mysql8/initdb:/docker-entrypoint-initdb.d
      - $PWD/docker/mysql8/data:/var/lib/mysql
      - $PWD/docker/log/mysql:/var/log/mysql/
    environment: 
      MYSQL_TCP_PORT: 33308
    env_file:
      - .env
    ports:
      - 33308:33308
    depends_on:
      - mysql

Here is the example for back-end service with CakePHP 4 framework:

export DATABASE_URL="mysql://User:Password@mysql8:33308/database?encoding=utf8&timezone=UTC&cacheMetadata=true&quoteIdentifiers=false&persistent=false"