Unable to give write permissions to Docker volume to non-root user in Docker Compose

Viewed 611

I'm running a Docker Compose environment in which to execute a bunch of Selenium tests. In order to do that, I'm using the images Selenium provides. I run two containers: one as the Selenium HUB and another as a Selenium Firefox Node. I have a third container defined who is in charge of the tests execution (let's call it Tests Node).

I created a Docker volume used by the Selenium Firefox Node and the Tests Node so certain files can be shared by them both. I called the volume selenium_volume and it's mounted for both on /selenium_tests.

I soon faced a problem for that folder is created in both systems for user root, but the user the Selenium Firefox Node uses by default is seluser. It has read but not write permissions, which I need.

I tried to use the following as the container entrypoint, so I make seluser the owner of the directory: bash -c 'sudo chown -R seluser:seluser /selenium_tests && /opt/bin/entry_point.sh', but it's not working.

When I connect to the container after it's launched (docker exec -ti selenium-firefox bash), I see the folder still belongs to root. If I then once connected run the command sudo chown -R seluser:seluser /selenium_tests && /opt/bin/entry_point.sh', the folder permissions are changed and we reach to the point I was expecting.

I would like to know why it's working when I run the command manually but it's not when running through the entrypoint before the entrypoint script of the container.

Currently my docker-compose.yml looks similar to the following:

    version: '3.8'
    
    services:
      tests-node:
        build: .
        depends_on:
         - selenium-hub
         - selenium-firefox
        # the wait-for-it.sh command makes the container wait until the Selenium containers are ready to work
        entrypoint: ["./wait-for-it.sh", "-t", "15", "selenium-firefox:5555", "--"]
        command: ["execute_tests.sh"]
        volumes:
          - ./remote_reports/:/selenium_tests/reports/
          - type: volume
            source: selenium_volume
            target: /selenium_tests
        networks:
          selenium_net: {}
    
      selenium-hub:
        image: selenium/hub:3.141.59
        ports:
          - "4444:4444"
        networks:
          selenium_net: {}
    
      selenium-firefox:
        image: selenium/node-firefox:3.141.59
        depends_on:
          - selenium-hub
        entrypoint: bash -c 'sudo chown -R seluser:seluser /selenium_tests && /opt/bin/entry_point.sh'
        volumes:
          - /dev/shm:/dev/shm
          - type: volume
            source: selenium_volume
            target: /selenium_tests
        environment:
          - HUB_HOST=selenium-hub
          - HUB_PORT=4444
        networks:
          selenium_net: {}
        expose:
          - 5555
    
    volumes:
      selenium_volume:
    
    networks:
      selenium_net:
        driver: bridge

I tried to run the chown command as command after the entrypoint but it never reaches I think because the script that is launched during the entrypoint keeps running in foreground.

I would like to do all the things directly on the docker-compose.yml and avoiding to create any Dockerfile, but I don't know if this is possible at this point.

2 Answers

I've found a possible solution. Using this as the entrypoint:

bash -c '( while ! timeout 1 bash -c "echo > /dev/tcp/localhost/5555"; do sleep 1; done ; sudo chown -R seluser:seluser /selenium_tests ) & /opt/bin/entry_point.sh'

It runs the part of the code before the & in background, allowing entry_point.sh to run without a problem. When the port 5555 is ready, it finally gives the permissions to the desired folder to seluser.

The simplest way is to add chmod -R 777 /selenium_tests to execute_tests.sh before starting the tests. In this case, @aorestr, you don't need to change entrypoint of the selenium-firefox container.

Related