Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Files created inside docker are write protected on host

I am using docker container for rails and ember.I am mounting the source from my local to the container. All the changes I make here on local are reflected in the container.

Now I want to use generators to create files. The files are created, but they are write protected on my machine.

When I try to do docker-compose run frontend bash, I get a root@061e4159d4ef:/frontend# superuser prompt access inside of the container. I can create files when I am in this mode. These files are write protected in my host.

I have also tried docker-compose run --user "$(id -u):$(id -g)" frontend bash, I get a I have no name!@31bea5ae977c:/frontend$, I am unable to create any file in this mode. Below is the error message that I get.

I have no name!@31bea5ae977c:/frontend$ ember g template about
/frontend/node_modules/ember-cli/node_modules/configstore/node_modules/mkdirp/index.js:90
                    throw err0;
                    ^

Error: EACCES: permission denied, mkdir '/.config'
    at Error (native)
    at Object.fs.mkdirSync (fs.js:916:18)
    at sync (/frontend/node_modules/ember-cli/node_modules/configstore/node_modules/mkdirp/index.js:71:13)
    at Function.sync (/frontend/node_modules/ember-cli/node_modules/configstore/node_modules/mkdirp/index.js:77:24)
    at Object.create.all.get (/frontend/node_modules/ember-cli/node_modules/configstore/index.js:39:13)
    at Object.Configstore (/frontend/node_modules/ember-cli/node_modules/configstore/index.js:28:44)
    at clientId (/frontend/node_modules/ember-cli/lib/cli/index.js:22:21)
    at module.exports (/frontend/node_modules/ember-cli/lib/cli/index.js:65:19)
    at /usr/local/lib/node_modules/ember-cli/bin/ember:26:3
    at /usr/local/lib/node_modules/ember-cli/node_modules/resolve/lib/async.js:44:21

Here is my Dockerfile:

FROM node:6.2

ENV INSTALL_PATH /frontend
RUN mkdir -p $INSTALL_PATH

WORKDIR $INSTALL_PATH

# Copy package.json separately so it's recreated when package.json
# changes.
COPY package.json ./package.json
RUN npm install
COPY . $INSTALL_PATH
RUN npm  install -g phantomjs bower ember-cli ;\
    bower --allow-root install

EXPOSE 4200
EXPOSE 49152

CMD [ "ember", "server" ]

Here is my docker-compose.yml file, please note it is not in the current directory, but the parent.

frontend:
   build: "frontend/"
   dockerfile: "Dockerfile"
   environment:
      - EMBER_ENV=development
   ports:
      - "4200:4200"
      - "49152:49152"
   volumes:
      - ./frontend:/frontend

I want to know, how can I use generateors? I am new to learning docker. Any help is appreciated.

like image 565
aks Avatar asked Jul 06 '16 05:07

aks


People also ask

Can I write to a docker container from my host?

If you want to write shared data from within your Docker container and use it from your host regularly, this can get tedious really fast. In addition, this approach can break the dockerized program for future runs, especially if the container’s user does not have root permissions.

What's wrong with file permissions in Docker containers?

The whole issue with file permissions in docker containers comes from the fact that the Docker host shares file permissions with containers (at least, in Linux).

Why can't I run Docker images as root in a container?

Most Docker images you find on Docker hub are designed to run as root inside the container. This can lead to your file system to be polluted with files that are owned by root.

What is the owner UID of a file in a dockerfile?

The owner UID of files that belong to the host root will be 0 in the container. So, they will be accessible to the intruder. Another issue is related to the user under which the build process of a docker image is executed. This user is the user under which RUN, CMD and ENTRYPOINT directives of Dockerfile are executed.


1 Answers

You get the I have no name! because of this: $(id -u):$(id -g) The user id and group in your host are not linked to any user in your container.

Solution:

Execute chown UID:GID -R /frontend inside the container if its already running and you cannot stop it for some reason. Otherwise you could just do the chown command in the host and then run your container again. Node that UID and GID must belong to a user inside the container

Example: chown 101:101 -R /frontend with 101 is the UID:GID of www-data.

If there are no other user exept root in your container, you will have to create a new one. To do so you must create a Dockerfile and put something like this:

FROM your_image_name
RUN useradd -ms /bin/bash newuser

More information about Dockerfiles can be found here or just by googlin' it.

like image 129
Fares Avatar answered Nov 15 '22 03:11

Fares