docker-compose up 도중 “host” network_mode is incompatible with port_bindings 오류 해결하기

제공

docker-compose 를 사용하던 도중 up 커맨드 실행시 아래와 같은 오류가 발생한 경우 해결 방법입니다.

# docker-compose up -d
Creating docker-compose-name_1 ... 

ERROR: for docker-compose-name_1 "host" network_mode is incompatible with port_bindings

ERROR: for php  "host" network_mode is incompatible with port_bindings
Traceback (most recent call last):
  File "/snap/docker/1779/bin/docker-compose", line 33, in <module>
    sys.exit(load_entry_point('docker-compose==1.29.2', 'console_scripts', 'docker-compose')())
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/cli/main.py", line 81, in main
    command_func()
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/cli/main.py", line 203, in perform_command
    handler(command, command_options)
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/metrics/decorator.py", line 18, in wrapper
    result = fn(*args, **kwargs)
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/cli/main.py", line 1186, in up
    to_attach = up(False)
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/cli/main.py", line 1182, in up
    attach_dependencies=attach_dependencies,
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/project.py", line 702, in up
    get_deps,
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/parallel.py", line 108, in parallel_execute
    raise error_to_reraise
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/parallel.py", line 206, in producer
    result = func(obj)
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/project.py", line 688, in do
    override_options=override_options,
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/service.py", line 564, in execute_convergence_plan
    override_options=override_options
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/service.py", line 480, in _execute_convergence_create
    "Creating"
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/parallel.py", line 108, in parallel_execute
    raise error_to_reraise
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/parallel.py", line 206, in producer
    result = func(obj)
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/service.py", line 478, in <lambda>
    lambda service_name: create_and_start(self, service_name.number),
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/service.py", line 457, in create_and_start
    container = service.create_container(number=n, quiet=True)
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/service.py", line 334, in create_container
    previous_container=previous_container,
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/service.py", line 941, in _get_container_create_options
    one_off=one_off)
  File "/snap/docker/1779/lib/python3.6/site-packages/compose/service.py", line 1073, in _get_container_host_config
    cpu_rt_runtime=options.get('cpu_rt_runtime'),
  File "/snap/docker/1779/lib/python3.6/site-packages/docker/api/container.py", line 596, in create_host_config
    return HostConfig(*args, **kwargs)
  File "/snap/docker/1779/lib/python3.6/site-packages/docker/types/containers.py", line 337, in __init__
    'network_mode', 'host', 'port_bindings'
docker.errors.InvalidArgument: "host" network_mode is incompatible with port_bindings

이 때 docker-compose.yml 설정파일의 내용은 아래와 같았습니다.

version: '2'

services:
  php:
    restart: always
    network_mode: "host"
    build: ./php
    ports:
      - "5600:5600"
    volumes:
      - ./var/www/html:/var/www/html

이전까지는 그다지 문제없이 작동했었는데요. 이번에 새로 우분투 22.04 서버 새로 추가하고 이 환경에서 docker-compose 하면서 문제가 생기네요. 아무래도 버전에 따라 달라지는 변경점이 있는 것 같습니다.

일단 network_mode 를 다른걸로는 바꿀수가 없는 터라 “host” 모드로 동작시키려면 호스트 모드에서는 포트 맵핑이 의미가 없기 때문에 포트 설정을 오류로 인식하는 것 같습니다.

ports 설정 부분을 제거하고 docker-compose.yml 파일을 아래와 같이 수정합니다.

version: '2'

services:
  php:
    restart: always
    network_mode: "host"
    build: ./php
    volumes:
      - ./var/www/html:/var/www/html

파일을 저장하고 아래와 같이 실행하면 이제 정상적으로 모듈이 뜨는 것을 확인할 수 있습니다. ^^

# docker-compose up -d

참고 문서 : https://www.clien.net/service/board/cm_nas/16308739