Code Yarns β€πŸ‘¨β€πŸ’»
Tech Blog ❖ Personal Blog

How to set umask for Docker container

πŸ“… 2017-Jul-21 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ docker ⬩ πŸ“š Archive

Problem

Once you run a Docker container using docker run and get a shell inside it, you can set the file creation mode mask there with the umask command of the shell. This is usually 0022 and you can set it to whatever you want. All consecutive operations at the shell and child processes forked from the shell will have with umask.

What if you don’t want to manually type this umask command, but want it set automatically in the container?

Solution

$ docker run -it --rm some_image "umask 0000; /bin/bash"

This does not work either. The umask is back to the normal one in the shell. There is no other way to specify umask directly in a docker run as discussed here.

$ cat set_umask.sh
#!/bin/bash
umask 0002
/bin/bash

To be able to run this script when the container is run, we first need to make this executable:

$ chmod +x set_umask.sh

Next we add commands to the Dockerfile to copy this into the image and make the script as the entry point:

COPY set_umask.sh set_umask.sh
ENTRYPOINT ["./set_umask.sh"]

Build the container and run it and see your umask already enabled at the shell!


Β© 2023 Ashwin Nanjappa β€’ All writing under CC BY-SA license β€’ 🐘 @codeyarns@hachyderm.io β€’ πŸ“§