How to Permanently Change Docker Directory Permissions on Linux

Docker is a powerful tool that allows you to run applications in isolated environments called containers. However, sometimes you may need to change the permissions of Docker folders to ensure that your applications can access the necessary files and directories.

This article will guide you through the process of permanently changing Docker folder permissions on a Linux system.

Understanding Docker Folder Permissions

By default, Docker stores its data, including images, containers, and volumes, in specific directories on your Linux system. The most common directory is /var/lib/docker.

The permissions of these folders determine who can read, write, or execute files within them. If the permissions are too restrictive, your applications may not function correctly.

Why Change Docker Folder Permissions?

There are several reasons why you might need to change Docker folder permissions:

  • You may want to restrict or grant access to specific users or groups.
  • Some applications require specific permissions to function correctly.
  • Adjusting permissions can help secure your Docker environment.

Steps to Permanently Change Docker Folder Permissions

Changing Docker folder permissions permanently involves modifying the ownership and permissions of the Docker directories.

Here’s how you can do it:

Step 1: Identify the Docker Directory

First, you need to identify where Docker stores its data, the default location is /var/lib/docker and you can confirm this by running the following command:

docker info | grep "Docker Root Dir"

This command will output the Docker root directory, which is typically /var/lib/docker.

Step 2: Stop the Docker Service

Before making any changes, you need to stop the Docker service to prevent any conflicts or data corruption using the following systemctl command:

sudo systemctl stop docker

Step 3: Change Ownership of the Docker Directory

To change the ownership of the Docker directory, use the chown command. For example, if you want to change the ownership to a user named john and a group named docker, you would run:

sudo chown -R john:docker /var/lib/docker

The -R option ensures that the ownership change is applied recursively to all files and subdirectories within the Docker directory.

Step 4: Change Permissions of the Docker Directory

Next, you need to change the permissions of the Docker directory by using the chmod command. For example, to give the owner full permissions and the group read and execute permissions, you would run:

sudo chmod -R 750 /var/lib/docker

Here, 750 means:

  • 7 for the owner: read, write, and execute permissions.
  • 5 for the group: read and execute permissions.
  • 0 for others: no permissions.

After changing the ownership and permissions, restart the Docker service to apply the changes:

sudo systemctl start docker

Finally, verify that the changes have been applied correctly by checking the ownership and permissions of the Docker directory using the following command:

ls -ld /var/lib/docker

This command will display the ownership and permissions of the Docker directory.

Making the Changes Permanent

The changes you made to the Docker folder permissions will persist across reboots. However, if Docker updates or reinstalls, the permissions might revert to the default settings.

To ensure that the changes are permanent, you can create a systemd service or a cron job that applies the permissions every time the system starts.

Option 1: Using a Systemd Service

Create a new systemd service file.

sudo nano /etc/systemd/system/docker-permissions.service

Add the following content to the file.

[Unit]
Description=Set Docker folder permissions
After=docker.service

[Service]
Type=oneshot
ExecStart=/bin/chown -R john:docker /var/lib/docker
ExecStart=/bin/chmod -R 750 /var/lib/docker

[Install]
WantedBy=multi-user.target

Save the file and enable the service to run at boot.

sudo systemctl enable docker-permissions.service

Option 2: Using a Cron Job

Open the crontab editor.

crontab -e

Add the following line to the crontab file to apply the permissions at every reboot.

@reboot /bin/chown -R john:docker /var/lib/docker && /bin/chmod -R 750 /var/lib/docker

Save and close the file.

Conclusion

Changing Docker folder permissions on Linux is a straightforward process that can help you manage access control, meet application requirements, and enhance security.

By following the steps outlined in this article, you can permanently change the ownership and permissions of Docker directories, ensuring that your Docker environment functions smoothly and securely.

Remember to verify the changes and consider using a systemd service or cron job to make the changes permanent.

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Ravi Saive
Ravi Saive
Administrator
About the author

I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.