-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docker Compose mounts named volumes as 'root' exclusively #3270
Comments
I don't think this is supported by the docker engine, so there's no way we can support it in Compose until it is added to the API. However I don't think it's necessary to add this feature. You can always chown the files to the correct user:
The issue you're facing is about initializing a named volume. This is admittedly not something that is handled by Compose (because it's somewhat out of scope), but you can easily use the docker cli to initialize a named volume before running |
I was indeed not sure whether this was a Docker or a Compose problem, sorry if I misfiled it. I understand the possibility of manually logging in the container and With the solution you described (manually "logging" in the container with This is why I think this feature should be implemented. In the Docker API, we can specify What do you think, does my request make sense? |
Actually I come here with news, it seems what I am trying to achieve is doable, but I don't know if this is a feature or a bug. Here is why I changed: In my Dockerfile, before changing to user 'postgres' I added these: # ...
RUN mkdir /volume_data
RUN chown postgres:postgres /volume_data
USER postgres
# ... What this does is create a directory /volume_data and change its permissions so that user 'postgres' can write on it. Now I havent changed anything on the docker-compose.yml : so docker-compose still creates the Named Volume So if this the intended behavior or a breach of security? (It does serve me in this case, though !) |
I believe this was added in Docker 1.10.x so that named volumes would be initialized from the first container that used them. I think it's expected behaviour. |
I'm also doing named volumes with ownership being set in the Dockerfile and in compose I'm adding |
|
Related (to some extent) moby/moby#28499 |
Did someone opened already an issue on the Moby project? The answer from @dnephin is not working. The problem is because we are running the container as a standard user, the |
@jcberthon the suggested method is to run the container with root as the starting user and then use the |
That's fine if you are in control of the Docker image but if you're using existing images that's not really an option. |
@dragon788 and @micheljung, I solved my problem. Actually the real issue was that i my Dockerfile, I declared a So the mistake was: FROM blabla
RUN do stuff
VOLUME /vol
RUN useradd foo && chown -R foo /vol
USER foo
CMD ["blabla.sh"] The By changing the file to: FROM blabla
RUN do stuff
RUN useradd foo && chown -R foo /vol
USER foo
VOLUME /vol
CMD ["blabla.sh"] the problem is solved. |
@jcberthon could you please share how do you bind your volume /vol with host system in your docker-compose.yml ? |
I am working with Docker on Fedora (so, SELinux enabled) and none of the above mentioned methods worked for me. Ideally, I want to run applications in my Containers under the context of a user (no root) but this Volume issue is a blocker to that. The only workaround that works for me is to eliminate my application user and run/own everything as the root user. |
The recipe followed here is inspired on docker/compose#3270
Hi @renanwilliam and @egee-irl I've been using the above mentioned solution on several OS incl. Fedora 26 and CentOS 7 both with SELinux enforced, Ubuntu 16.04, 17.10 and Raspbian 9 all three with AppArmor activated (with a mixture of amd64 and armhf platforms). So as I said, I've now moved the So next step is to create the "pendant" of the
Then when running the container as user foo, it will be able to write to the
On SELinux based hosts, you might want to add the
Update: you can check my repo here https://github.com/jcberthon/unifi-docker I'm using this method and explaining how to configure the host and run your container. I hope this can help futher solving your problems. |
Btw, I apologise @renanwilliam for the long delay in replying to you. I don't have much free time this end of the year... |
So, long story short for the impatient:
Creating the volume directory beforehand and a |
This is a poor work around as it is non-obvious (Doing a chown in a Dockerfile and then inheriting that ownership during the mount). Exposing owner and group control at the docker-compose and docker CLI would be the path of least surprise for unix style commands. |
A small tip: merge the 2
But beware (as I mentioned in a comment above) that you need to do the above |
@colbygk it would be indeed handy, but that's not how Linux works. Docker uses the Linux mount namespace to create different single-directory hierarchies ( Simple Linux (and actually Unix as well) rules apply for the container. The trick is to see and understand the possibilities and limits of Linux namespaces today, and then it is becoming clearer how to solve problems such as this issue. I solved it entirely using classical Unix commands. |
@jcberthon Thank you for your thoughtful response: I would argue that this should be an issue that is pushed into the plugin layer as you suggest and therefore could become part of the generic volume handler plugin that ships with Docker. It seems very uncloud/container like to me to force an external resource (external to a particular container) to adhere to essentially static relationships that are defined in the image a container is derived from. There are other examples of this exact sort of uid/gid mapping that occurs in other similar areas of "unix":
Please correct me if I am wrong; openzfs/zfs#4177 appears to be originated by the "leader of LXC/LXD" as an issue related to ZFS on linux not correctly providing UID/GID information to allow mapping of those into a container in almost the exact way we are discussing here. Looking closely at openzfs/zfs#4177 it appears that the zfs volume type actually could already support this uid/gid mapping between namespaces, but does not expose the controls to do so. |
… default non-root user `node` Error Log: client | [Error: EACCES: permission denied, open '/usr/src/app/client/.next/package.json'] { client | errno: -13, client | code: 'EACCES', client | syscall: 'open', client | path: '/usr/src/app/client/.next/package.json' client | } References: node.js - How to keep node_modules inside container while using docker-compose and a non-root user? - Stack Overflow https://stackoverflow.com/questions/49941708/how-to-keep-node-modules-inside-container-while-using-docker-compose-and-a-non-r https://stackoverflow.com/a/49952703 https://stackoverflow.com/questions/49941708/how-to-keep-node-modules-inside-container-while-using-docker-compose-and-a-non-r/49952703#49952703 Docker Compose mounts named volumes as 'root' exclusively · Issue #3270 · docker/compose · GitHub docker/compose#3270 Stuck with a Problem of access permission denied · Issue #8908 · vercel/next.js · GitHub vercel/next.js#8908 vercel/next.js#8908 (comment) What is the best way to use NextJS with docker? · Discussion #16995 · vercel/next.js · GitHub vercel/next.js#16995 reactjs - error An unexpected error occurred: "EACCES: permission denied - Stack Overflow https://stackoverflow.com/questions/52713928/error-an-unexpected-error-occurred-eacces-permission-denied docker - Dockerfile with nextjs and puppteer permission denied - Stack Overflow https://stackoverflow.com/questions/64565062/dockerfile-with-nextjs-and-puppteer-permission-denied [RUN chown -Rh $user:$user /home/node] https://stackoverflow.com/a/64922590 https://stackoverflow.com/questions/64565062/dockerfile-with-nextjs-and-puppteer-permission-denied/64922590#64922590 [More relevant for production build, suggests we modify next.config.js by adding a distDir directive: module.exports = { distDir: 'build', serverRuntimeConfig: { // Will only be available on the server side apiUrl: 'http://signup-api:1984' }, publicRuntimeConfig: { // Will be available on both server and client apiUrl: 'http://localhost:1984' } } ]
… default non-root user `node` Error Log: client | [Error: EACCES: permission denied, open '/usr/src/app/client/.next/package.json'] { client | errno: -13, client | code: 'EACCES', client | syscall: 'open', client | path: '/usr/src/app/client/.next/package.json' client | } References: node.js - How to keep node_modules inside container while using docker-compose and a non-root user? - Stack Overflow https://stackoverflow.com/questions/49941708/how-to-keep-node-modules-inside-container-while-using-docker-compose-and-a-non-r https://stackoverflow.com/a/49952703 https://stackoverflow.com/questions/49941708/how-to-keep-node-modules-inside-container-while-using-docker-compose-and-a-non-r/49952703#49952703 Docker Compose mounts named volumes as 'root' exclusively · Issue #3270 · docker/compose · GitHub docker/compose#3270 Stuck with a Problem of access permission denied · Issue #8908 · vercel/next.js · GitHub vercel/next.js#8908 vercel/next.js#8908 (comment) What is the best way to use NextJS with docker? · Discussion #16995 · vercel/next.js · GitHub vercel/next.js#16995 reactjs - error An unexpected error occurred: "EACCES: permission denied - Stack Overflow https://stackoverflow.com/questions/52713928/error-an-unexpected-error-occurred-eacces-permission-denied docker - Dockerfile with nextjs and puppteer permission denied - Stack Overflow https://stackoverflow.com/questions/64565062/dockerfile-with-nextjs-and-puppteer-permission-denied [RUN chown -Rh $user:$user /home/node] https://stackoverflow.com/a/64922590 https://stackoverflow.com/questions/64565062/dockerfile-with-nextjs-and-puppteer-permission-denied/64922590#64922590 [More relevant for production build, suggests we modify next.config.js by adding a distDir directive: module.exports = { distDir: 'build', serverRuntimeConfig: { // Will only be available on the server side apiUrl: 'http://signup-api:1984' }, publicRuntimeConfig: { // Will be available on both server and client apiUrl: 'http://localhost:1984' } } ]
…tainer's default non-root user `node` Error Log: client | [Error: EACCES: permission denied, open '/usr/src/app/client/.next/package.json'] { client | errno: -13, client | code: 'EACCES', client | syscall: 'open', client | path: '/usr/src/app/client/.next/package.json' client | } References: node.js - How to keep node_modules inside container while using docker-compose and a non-root user? - Stack Overflow https://stackoverflow.com/questions/49941708/how-to-keep-node-modules-inside-container-while-using-docker-compose-and-a-non-r https://stackoverflow.com/a/49952703 https://stackoverflow.com/questions/49941708/how-to-keep-node-modules-inside-container-while-using-docker-compose-and-a-non-r/49952703#49952703 Docker Compose mounts named volumes as 'root' exclusively · Issue #3270 · docker/compose · GitHub docker/compose#3270 Stuck with a Problem of access permission denied · Issue #8908 · vercel/next.js · GitHub vercel/next.js#8908 vercel/next.js#8908 (comment) What is the best way to use NextJS with docker? · Discussion #16995 · vercel/next.js · GitHub vercel/next.js#16995 reactjs - error An unexpected error occurred: "EACCES: permission denied - Stack Overflow https://stackoverflow.com/questions/52713928/error-an-unexpected-error-occurred-eacces-permission-denied docker - Dockerfile with nextjs and puppteer permission denied - Stack Overflow https://stackoverflow.com/questions/64565062/dockerfile-with-nextjs-and-puppteer-permission-denied [RUN chown -Rh $user:$user /home/node] https://stackoverflow.com/a/64922590 https://stackoverflow.com/questions/64565062/dockerfile-with-nextjs-and-puppteer-permission-denied/64922590#64922590 [More relevant for production build, suggests we modify next.config.js by adding a distDir directive: module.exports = { distDir: 'build', serverRuntimeConfig: { // Will only be available on the server side apiUrl: 'http://signup-api:1984' }, publicRuntimeConfig: { // Will be available on both server and client apiUrl: 'http://localhost:1984' } } ]
What is the easiest workaround? |
One workaround is to have an init container mount the same named volume, my-service-init:
image: node:16
user: root
group_add:
- '1000'
volumes:
- my-service-recover:/tmp/recover_data
command: chown -R node:1000 /tmp/recover_data
my-service:
restart: always
user: node
group_add:
- '1000'
volumes:
- my-service-recover:/tmp/recover_data
depends_on:
my-service-init:
condition: service_completed_successfully
volumes:
my-service-recover:
|
This article resolved the issue for me: Here's my TL/DR summary of the article: |
* Experiment running system-tests for mysql inside containers [#183164582] Signed-off-by: Fernando Naranjo <fnaranjo@vmware.com> * Build database-backup-restore and fix echo commands [#183164582] Also replace some additional commands performed on BOSH VM * Ensure we invoke `bash -c` for every `go.Command` [#183164582] Running os commands using goexec is not identical to running those commands in a shell such as bash. Some examples of known things to require a bash shell: - Pipes - Redirects Signed-off-by: Fernando Naranjo <fnaranjo@vmware.com> * Create workflow to run MySQL tests on PRs [#183164582] Signed-off-by: Fernando Naranjo <fnaranjo@vmware.com> * Generate TLS certificates and shared them across containers [#183164582] Mysql service is the one in charge of creating the certificates. To make these generated certificates available to the container running the tests we use a shared volume named `mysql-certs` which we mount in both containers. * Support testing several MYSQL versions using GHAction matrix [#183164582] For this we need to pass the MYSQL version we want to test as a Docker build arg * Remove from matrix failing unsupported versions of MYSQL [#183164582] There are chances that the versions removed could also work with our current release but they need some packages not included by default in the Dockerimage. Doing the extra effort to make the pass is irrelevant to the BOSH release itself, since the filesystem is not identical to what will be included in the VM. We might need to investigate how to reduce this filesystem mismatch if possible. Signed-off-by: Fernando Naranjo <fnaranjo@vmware.com> * WIP try to improve some docs-type things [#183164582] Signed-off-by: Gareth Smith <sgareth@vmware.com> * Remove unneded folder intialization steps [#183164582] They seem to have inconsistent behaviour on different operating systems * Restore folder initialization and add it to system-mysql Dockerfile too [#183164582] * Print database logs in case of test failures [#183164582] * Reorganize VOLUME declarations to overcome a possible bug in Docker [#183164582] docker/compose#3270 (comment) * Replace chmod with chown to better express the intent of the operation [#183164582] * Revert "Replace chmod with chown to better express the intent of the operation" This reverts commit e102885. It seems `chmod root:root` counteracts `chmod mysql:mysql` causing a race condition between the two containers. It seems we are better off using `chmod 777` here. * Improve debugging experience by mounting the whole repo instead of a subfolder [#183164582] By doing this any error message in the logs would point to the exact path where the problem occurred instead of a docker-mounted path in an arbitrary location. This change pursues reducing the knowledge burden introduced by the use of Docker by trying to make the Docker integration as thin and transparent as possible. Ideally, not requiring any Docker knowledge to be able to work with the codebase. * Improve unit-tests debug experience by mounting full repo instead of subfolder [#183164582] By doing this any error message in the logs would point to the exact path where the problem occurred instead of a docker-mounted path in an arbitrary location. This change pursues reducing the knowledge burden introduced by the use of Docker by trying to make the Docker integration as thin and transparent as possible. Ideally, not requiring any Docker knowledge to be able to work with the codebase. * Refactor system-db-tests to use a similar approach to unit-tests [#183164582] Some goals of this refactor were: - Don't have an explicit ENTRYPOINT in Dockerfile, specify it in docker-compose - Move most of the boilerplate required by the tests to the tests folder - Remove hardcoded paths in boilerplate environment variables required by tests - Make scripts/run-system-db-tests-mysqsl more self-container and self-documenting - Flatten the path to start adding tests for other databases such as Postgres - Flatten the path to add some tests for testing the version detection logic * Simplify TestRunner.Dockerfile and `backup`/`restore` invocations [#183164582] Original tests made use of two auxiliary scripts `backup`/`restore` to set the required environment variables every time they were used. Since these variables are static and never change we added these vars to the `run-system-db-tests-mysql.bash` script itself and invoke the `database-backup-restore` binary directly instead from the tests. * Reduce certs handling in `Dockerfile` and `docker-compose.yml` [#183164582] By aglutinating the three previous variables each one pointing to a different certificate into a single variable pointing to a folder we should contain the three files. These files are expected to always be named the same so there isn't a need to specify the full path for each of them individually. * Fix errors introduced in latest commit [#183164582] Some of the environment variables which previously we thought were not needed explicitly by the tests were in fact required for mysql to successfully establish a connection using TLS certificates. * Add MySQL5.7 binary as it is used to determine version of db [#183164582] By looking at the source-code we determined that mysql5.7 binary is used to connect with the mysql database and ask for its version. Only later, the corresponding binary will be choosed and used. Signed-off-by: Fernando Naranjo <fnaranjo@vmware.com> * Add tests for different versions of MariaDB [#183164582] In this case we didn't copy the binary from the mariadb Docker image because it had unsatisfied dependencies (such as libssl3) * Trigger PR workflows * Show database logs on error to facilitate debugging [#183164582] * Workaround permission issues when specifying Volume in Dockerfile [#183164582] * Try triggering tests again [#183164582] * Use chmod 777 both in TestsRunner and BackingDB Dockerfiles [#183164582] * Fix mysql5.7-debian tests and make mysql tests more precise [#183164582] Since bbr-sdk-release always uses 5.7 at least once (to query the db version) we always need to specify MYSQL_CLIENT_5_7_PATH. Since we are using GitHub matrixes to tests several MySQL versions using the same scripts, it seems difficult to use the MYSQL_VERSION variable to download only the required binaries since we will need some conditional logic to populate the right ENV variables. For that reason this commit simply downloads the latest MySQL 8.0 and MySQL 5.7 binaries and populates the corresponding ENV variables every time, without caring which specific MYSQL_VERSION we are currently testing. * Add Postgres system tests [#183164582] * Fix typo in Postgres system tests workflow [#183164582] * Test compilation against diff stemcells and improve system tests [#183164582] To test compilation against diff stemcells we use multistage Docker containers to download bosh-lite warden tarballs and using the rootfs included in the tarball to create a container FROM scratch. Inside that dockerized stemcell we run the release packaging scripts which allows testing compilation against different stemcell using GHActions matrix strategies. Now that we are able to compile the release directly in GHActions, we can improve system tests by leveraging the binaries built on-the-fly from the blobs. This approach has huge benefits compared to the previous one: - Tests are more true to reality - Bosh blobs are fully tested (instead of testing just the GO code) - Compilation is being tested against different stemcells * Use `backup` and `restore` scripts from the /var/vcap/jobs folder [#183164582] Also, compiled database-backup-restorer using its packaging script instead of manually running golang during the `run-system-db-tests` scripts and refactored tests to use the binary located in /var/vcap/packages * Reduce verbosity of the logs [#183164582] * Add ubuntu-xenial tests except for MySQL 8.0 which is unsupported [#183164582] * Run postgres_tls and postgres_mutual_tls test suites in containers [#183164582] Signed-off-by: Fernando Naranjo <fnaranjo@vmware.com> * Fix Postgres TLS and MUTUAL_TLS test suites [#183164582] * Ensure system-db-postgres-backing-db database gets cleaned up after each job [#183164582] * Fix ENABLE_TLS var not being passed to docker-compose run commands [#183164582] All tests were running the basic Postgres tests instead of TLS and MUTUAL * Run dockerize-release as a separate step from the tests [#183164582] This change will help us differentiate how much time is spent in the actual tests versus compiling the release itself. This should serve as a first step towards optimizing builds * Prevent running Postgres TLS and MTLS tests for 9.4-alpine [#183164582] postgres:9.4-alpine doesn't have openssl package installed by default which causes the database to error before start and prevents tests from running. Testing Postgres 9.4 without TLS/MTLS should be enough for now to ensure this version is supported. Patching postgres Dockerfile to add openssl is possible but might no be needed or desirable since Postgres 9.4 reached EOGS in February 2020 * Disable 9.4-alpine for Mutual TLS not normal tests [#183164582] * Move dockerfiles from .github/actions to ci/ [#183164582] Signed-off-by: Diego Lemos <dlemos@vmware.com> * Restore tests and guard new BOSH-less functionality under a flag [#183164582] * Redirect stderr to stdout when running with flag RUN_TESTS_WITHOUT_BOSH [#183164582] The tests expected error messages to be available in stdout. Probably because when running the commands by SSHing into a BOSH VM the errors are retreived from Stdout Signed-off-by: Fernando Naranjo <fnaranjo@vmware.com> Signed-off-by: Gareth Smith <sgareth@vmware.com> Signed-off-by: Diego Lemos <dlemos@vmware.com> Co-authored-by: Gareth Smith <sgareth@vmware.com> Co-authored-by: Diego Lemos <dlemos@vmware.com> Co-authored-by: Cryogenics CI Bot <mapbu-cryogenics@groups.vmware.com>
I solved it by not modifying Dockerfile or entrypoint. Mount the named volume to /path with uid=70 in the compose.yml below. Create named volume and change ownerMake sure there is no
Change the owner of a named volume.
Specify externrl in volumes of compose.yml.Specify external for volumes as shown below.
Start compose and check the owner.
Although named volumes are part of docker, we believe they should be managed separately from the container just like any other external storage. |
ndeloof's solution helped me to resolve the same issue. It's important to note that only short syntax of |
This stops working when the files are in WSL, the user becomes "xfs" and it seems to be unchangeable. |
thanks @giraffesyo I made a little more complet example where the original file is not yet in the volume my-service-init:
image: node:16
user: root
group_add:
- '1000'
volumes:
- ./cred.json:/opt/conf/cred.json:ro
- my-service-recover:/opt/something/conf/
command: bash -c "ls -la /opt/conf/ && ls -la /opt/something/conf/ && cp /opt/conf/cred.json /opt/something/conf/cred.json && chown node /opt/something/conf/cred.json && chmod 755 /opt/something/conf/cred.json && ls -la /opt/something/conf/"
giving ->
|
I want to draw attention to this elegant solution which is fully encapsulated in the compose file: You can use |
@mogul While it seems unlikely a data race would occur in practice, to guarantee the
|
See helpful comment [here](docker/compose#3270 (comment)).
Great point!💡 I tested that that works locally, and I've updated that in the PR that I pointed to as my worked example.🙇♂️ |
It's about named volumes (so no "data volume container", no "volumes-from") and docker-compose.yml.
The goal here is to use docker-compose to manage two services 'appserver' and 'server-postgresql' in two separate containers and use the "volumes:" docker-compose.yml feature to make data from service 'server-postgresql' persistent.
The Dockerfile for 'server-postgresql' looks like this:
Adn the docker-compose.yml looks like this:
Then I start everything with
docker-compose up -d
, I enter my server-postgresql container withdocker-compose exec server-postgresql bash
, a quickls
does reveal/volume_data
, I thencd
into it and trytouch testFile
and got "permission denied. Which is normal because a quickls -l
show thatvolume_data
is owned byroot:root
.Now what I think is happening is that since I have
USER postgres
in the Dockerfile, when I rundocker-compose exec
I am logged in as user 'postgres' (and the postgresql daemon runs as user 'postgres' as well, so it won't be able to write to/volume_data
).This is confirmed because when I run this instead:
docker-compose exec --user root server-postgresql bash
and retry tocd /volume_data
andtouch testFile
, it does work (it's not a permission error between the host and the container, as it is somtimes the case when the container mounts a host folder, this is a typical unix permission error because/volume_data
is mounted as 'root:root' while user 'postgres' is trying to write).So there should be a way in docker-compose.yml to mount namedvolumes as specific user, smth like:
The only dirty workaround that I can think of is remove the
USER posgres
directive from the Dockerfile, and change the ENTRYPOINT so that it points to a custom "init_script.sh" (wihch would be run as 'root' since I removedUSER postgres
), this script would change permissions of/volume_data
so that 'postgres' can write on it, thensu postgres
and execute the postgresql daemon (in foreground). But this is actually very dirty, because it links the Dockerfile and docker-compose.yml in a non standard way (runtime ENTRYPOINT would rely on the fact that a mounted volume is made available by docker-compose.yml).The text was updated successfully, but these errors were encountered: