3

I'm trying to change the ownership of a mounted volume at startup so that a user in the container can write to it and the host can read from it.

    volumes:
      - '../local/userData:/userData'

seems to be accepted - a folder is created in local/userData, but as root. sudo chown -R user:user /userData in the entryPoint command seems to fail and hang without an error. How can one resolve this?

...
services:
  myservice:
    build:
      context: ..
      dockerfile: myservice-container/Dockerfile
    image: myservice:latest
    container_name: local_myservice_1
    networks:
      - local_default
    volumes:
      - '../local/userData:/userData'
    environment:
      DEPLOY_TYPE: local
      AWS_ENVIRONMENT: NOT_AWS
    cap_add:
      - NET_ADMIN
    stdin_open: true
    tty: true
    # Make /userData writeable to user.
    entrypoint: sh -c "(ls -la /userData) && (sudo chown -R user:user /userData) && (echo OK)"

The dockerfile successfully created the user already:

RUN useradd -ms /bin/bash user

The whole process hangs with:

Recreating myservice_1 ... done
Attaching to myservice_1
myservice_1  | total 8
myservice_1  | drwxr-xr-x 2 root root 4096 Jul 29 15:20 .
myservice_1  | drwxr-xr-x 1 root root 4096 Jul 29 15:27 ..
compose.cli.verbose_proxy.proxy_callable: docker events -> <docker.types.daemon.CancellableStream object at 0x7f7b815604f0>
....<hangs here> ....
^C
compose.parallel.feed_queue: Pending: set()
compose.parallel.feed_queue: Pending: set()
compose.parallel.feed_queue: Pending: set()
compose.parallel.feed_queue: Pending: set()
^Ccompose.cli.verbose_proxy.proxy_callable: docker containers <- (all=False, filters={'label': ['com.docker.compose.project=local', 'com.docker.compose.oneoff=False']})
compose.cli.verbose_proxy.proxy_callable: docker containers -> (list with 1 items)
compose.cli.verbose_proxy.proxy_callable: docker inspect_container <- ('7f39ec0d3a5019b72ff42b13eb7a0353c420c35738c0be5d6a4f80b95fde8102')
compose.cli.verbose_proxy.proxy_callable: docker inspect_container -> {'AppArmorProfile': 'docker-default',
 'Args': ['-c',
          '(ls -la /userData) && (sudo chown -R user:user /userData) && (echo '
          'OK)'],
 'Config': {'AttachStderr': False,
            'AttachStdin': False,
            'AttachStdout': False,
            'Cmd': None,
            'Domainname': '',
            'Entrypoint': ['sh',
...
Killing myservice_1    ... 
compose.parallel.feed_queue: Pending: {<Container: local_ai_1 (7f39ec)>}
compose.parallel.feed_queue: Starting producer thread for <Container: myservice_1 (7f39ec)>
compose.cli.verbose_proxy.proxy_callable: docker kill <- ('7f39ec0d3a5019b72ff42b13eb7a0353c420c35738c0be5d6a4f80b95fde8102')
compose.cli.verbose_proxy.proxy_callable: docker inspect_container <- ('7f39ec0d3a5019b72ff42b13eb7a0353c420c35738c0be5d6a4f80b95fde8102')
compose.cli.verbose_proxy.proxy_callable: docker inspect_container -> {'AppArmorProfile': 'docker-default',
 'Args': ['-c',
          '(ls -la /userData) && (sudo chown -R user:user /userData) && (echo '
          'OK)'],
 'Config': {'AttachStderr': False,
            'AttachStdin': False,
            'AttachStdout': False,
            'Cmd': None,
            'Domainname': '',
            'Entrypoint': ['sh',
...
compose.parallel.feed_queue: Pending: set()
compose.cli.verbose_proxy.proxy_callable: docker wait <- ('7f39ec0d3a5019b72ff42b13eb7a0353c420c35738c0be5d6a4f80b95fde8102')
compose.cli.verbose_proxy.proxy_callable: docker inspect_container <- ('7f39ec0d3a5019b72ff42b13eb7a0353c420c35738c0be5d6a4f80b95fde8102')
compose.parallel.feed_queue: Pending: set()
compose.parallel.feed_queue: Pending: set()
compose.parallel.feed_queue: Pending: set()
compose.parallel.feed_queue: Pending: set()
compose.cli.verbose_proxy.proxy_callable: docker kill -> None
compose.cli.verbose_proxy.proxy_callable: docker stop -> None
Killing myservice_1    ... done
compose.cli.verbose_proxy.proxy_callable: docker wait -> {'Error': None, 'StatusCode': 137}
compose.cli.verbose_proxy.proxy_callable: docker inspect_container -> {'AppArmorProfile': 'docker-default',
 'Args': ['-c',
          '(ls -la /userData) && (sudo chown -R user:user /userData) && (echo '
          'OK)'],
 'Config': {'AttachStderr': False,
            'AttachStdin': False,
            'AttachStdout': False,
            'Cmd': None,
            'Domainname': '',
            'Entrypoint': ['sh',
...
compose.parallel.feed_queue: Pending: set()
compose.cli.verbose_proxy.proxy_callable: docker inspect_container <- ('7f39ec0d3a5019b72ff42b13eb7a0353c420c35738c0be5d6a4f80b95fde8102')
ERROR: compose.cli.main.exit_with_metrics: 2
compose.cli.verbose_proxy.proxy_callable: docker inspect_container -> {'AppArmorProfile': 'docker-default',
 'Args': ['-c',
          '(ls -la /userData) && (sudo chown -R user:user /userData) && (echo '
          'OK)'],
 'Config': {'AttachStderr': False,
            'AttachStdin': False,
            'AttachStdout': False,
            'Cmd': None,
            'Domainname': '',
            'Entrypoint': ['sh',

"OK" is not printed. I'm running docker-compose --verbose up --build


Did not solve: docker-compose up hangs forever. How to debug? , docker-compose up error without information , Docker compose & docker-entrypoint , docker-compose up hangs forever. How to debug?

user48956
  • 14,850
  • 19
  • 93
  • 154
  • 1
    I wonder if it's waiting for you to type in a `sudo` password. There probably is no correct password, and also there is no way to enter it. You shouldn't usually need `sudo` in Docker; specify `user: root` in the Compose file if you need that level of permission and the image sets a different user by default. – David Maze Jul 29 '22 at 20:10
  • That's a good thought. "sudo ls" also fails in the same way. However "chown" without sudo results in operation not permitted. – user48956 Jul 29 '22 at 20:33
  • The container does create the directory on the host, as root. I can chown it from the host to the UID=1000 user and its all works fine. However, think means running docker (to create the directory) and it failing (because sudo failed), and running it again. Its a mess for CI automation :-/ – user48956 Jul 29 '22 at 20:35

1 Answers1

1

The simple solution was to always have the host create that folder, and not leave it up to docker-compose. If docker-compose creates it, it is created as root.

user48956
  • 14,850
  • 19
  • 93
  • 154