dockerdocker-compose

Docker compose starts container without open port


After I started all containers using docker compose, some containers did not expose ports, such as mysql. I'm using mac which is m1 chip. This is the docker compose command:

docker compose up -d --build

This is the part of docker-compose.yaml

version: "3.5"

services:
  mall4cloud-mysql:
    platform: linux/x86_64
    image: registry.cn-hongkong.aliyuncs.com/mall4j-images/mysql:8.0.35
    container_name: mall4cloud-mysql
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=xx
    network_mode: "host"
    expose:
      - 3306
    volumes:
      - ./mysql/data:/var/lib/mysql
      - ./mysql/conf.d:/etc/mysql/conf.d
      - ./mysql/initdb:/docker-entrypoint-initdb.d

The docker ps command shows like this: no expose port in the result

I copy the command from docker desktop:

docker run --hostname=docker-desktop --env=MYSQL_ROOT_PASSWORD=80jpnH4.r5g --env=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin --env=GOSU_VERSION=1.16 --env=MYSQL_MAJOR=8.0 --env=MYSQL_VERSION=8.0.35-1.el8 --env=MYSQL_SHELL_VERSION=8.0.35-1.el8 --volume=/Users/cgc/Desktop/project/doc/mysql/data:/var/lib/mysql:rw --volume=/Users/cgc/Desktop/project/doc/mysql/conf.d:/etc/mysql/conf.d:rw --volume=/Users/cgc/Desktop/project/doc/mysql/initdb:/docker-entrypoint-initdb.d:rw --volume=/var/lib/mysql --network=host --restart=always --label='com.docker.compose.config-hash=c2efe968197fdbbab58c728eac730162cc579d7c3f9ced38dc1b986fbf089c8c' --label='com.docker.compose.container-number=1' --label='com.docker.compose.depends_on=' --label='com.docker.compose.image=sha256:77f16659c1292812cabe673478e558c1e57a7df5710c232d825e49faa15e7b40' --label='com.docker.compose.oneoff=False' --label='com.docker.compose.project=docker-compse' --label='com.docker.compose.project.config_files=/Users/cgc/Desktop/project/doc/docker-compose.yaml' --label='com.docker.compose.project.working_dir=/Users/cgc/Desktop/project/doc' --label='com.docker.compose.service=mall4cloud-mysql' --label='com.docker.compose.version=2.30.3' --runtime=runc -d registry.cn-hongkong.aliyuncs.com/mall4j-images/mysql:8.0.35

There is a parameter "--network=host " in the command. This setting option should ensure port mapping, but neither the docker ps command to view the port nor the connection through jdbc can connect to mysql.


Solution

  • From the Compose File Reference:

    expose defines the (incoming) port or a range of ports that Compose exposes from the container. These ports must be accessible to linked services and should not be published to the host machine. Only the internal container ports can be specified.

    ...

    The ports is used to define the port mappings between the host machine and the containers. This is crucial for allowing external access to services running inside containers. It can be defined using short syntax for simple port mapping or long syntax, which includes additional options like protocol type and network mode.

    You're expecting to be able to access your container over a specified port number from your host machine, which means you want to be using ports, not expose:

    services:
      mall4cloud-mysql:
        platform: linux/x86_64
        image: registry.cn-hongkong.aliyuncs.com/mall4j-images/mysql:8.0.35
        container_name: mall4cloud-mysql
        restart: always
        environment:
          - MYSQL_ROOT_PASSWORD=xx
        network_mode: "host"
        ports:
          - 3306
        volumes:
          - ./mysql/data:/var/lib/mysql
          - ./mysql/conf.d:/etc/mysql/conf.d
          - ./mysql/initdb:/docker-entrypoint-initdb.d