Solution to the Docker container not having permission to write to the host directory

Solution to the Docker container not having permission to write to the host directory

When applying docker containers, we often mount the host directory into the docker container.

When the folder permissions of the host machine belong to the root, we need to set the folder permissions user to chown to ensure the normal writing of the directory contents.

Here is an example:

The docker version of jenkins is used. After running, the following error occurs:

[root@localhost CICD]# docker logs -f jenkins 
touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

The directory where my jenkins is mounted is /opt/jenkins/xxxxx, created by the root user, and the uid of the jenkins user is 1000

So you need to chown the settings as follows:

sudo chown -R 1000:1000 /opt/jenkins

Then restart the container and the error will go away.

Supplement: Introduce two ways to handle file permissions when writing volumes from Docker containers

Say it in advance

Containers are often used as a replacement for native installation tools. It is much better to use containers with the required versions on the host than to use outdated tools. However, any time the container interacts with the host system, files are left with incorrect or corrupt permissions.

Fortunately, the solution to this problem does not require the use of scripts.

Problem Description

When a container mounts a local directory and writes files to it, its ownership is determined by the user inside the container:

nicholas@host:~/source$ mkdir source
nicholas@host:~/source$ docker run -it --rm --volume $(pwd):/source --workdir /source ubuntu
root@a031d11c9515:/source# mkdir subdir
root@a031d11c9515:/source# touch subdir/newfile
root@a031d11c9515:/source# exit
exit
nicholas@host:~/source$ ls -lR
.:
total 4
drwxr-xr-x 2 root root 4096 Jul 16 19:35 subdir
 
./subdir:
total 0
-rw-r--r-- 1 root root 0 Jul 16 19:35 newfile
nicholas@host:~/source$ rm -rf subdir/
rm: cannot remove 'subdir/newfile': Permission denied

Additionally, you may not be able to delete these directories and files that have incorrect ownership.

Solution 1: Remove from container

A very common solution is to change the ownership of files and directories from inside the container:

nicholas@host:~/source$ docker run -it --rm --volume $(pwd):/source --workdir /source ubuntu
root@d1c3bee8bb2b:/source# ls -al
total 12
drwxrwxr-x 3 1000 1004 4096 Jul 16 19:35 .
drwxr-xr-x 1 root root 4096 Jul 16 19:39 ..
drwxr-xr-x 2 root root 4096 Jul 16 19:35 subdir
root@d1c3bee8bb2b:/source# chown 1000:1000 subdir/ -R
root@d1c3bee8bb2b:/source# ls -l
total 4
drwxr-xr-x 2 1000 1000 4096 Jul 16 19:35 subdir
root@d1c3bee8bb2b:/source# exit
exit
nicholas@host:~/source$ ls -l
total 4
drwxr-xr-x 2 nicholas lpadmin 4096 Jul 16 19:35 subdir
nicholas@host:~/source$

The downside to this approach is that it requires additional logic, and that you need to know the user and group IDs of the user running the container.

Solution 2: Create a file with the correct ownership

The second solution is cleaner and will create the files and directories with the correct ownership inside the container. Docker provides a parameter to set the user ID and group ID of the user in the container:

nicholas@host:~/source$ docker run -it --rm --volume $(pwd):/source --workdir /source --user $(id -u):$(id -g) ubuntu
groups: cannot find name for group ID 1004
I have no name!@bf7f355f3b65:/source$ touch newfile
I have no name!@bf7f355f3b65:/source$ exit
exit
nicholas@host:~/source$ ls -l
total 4
-rw-r--r-- 1 nicholas nicholas 0 Jul 16 19:42 newfile
drwxr-xr-x 2 nicholas lpadmin 4096 Jul 16 19:35 subdir
nicholas@host:~/source$

This method can help you solve user ID and group ID errors.

Please note that for security purposes, running as root inside a container is the worst practice. Dockerfiles should always use the USER directive to avoid using root privileges directly.

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Docker enables seamless calling of shell commands between container and host
  • Solution to the Docker container being unable to access the host port
  • Execute the shell or program inside the Docker container on the host
  • Call and execute host docker operations in docker container
  • Detailed explanation of how to solve the problem that the docker container cannot access the host machine through IP
  • How to use Docker container to access host network
  • Solve the problem of 8 hours difference between docker container and host machine

<<:  Detailed explanation of the execution process of MySQL query statements

>>:  JavaScript Interview: How to implement array flattening method

Recommend

Javascript implements simple navigation bar

This article shares the specific code of Javascri...

Detailed explanation of Nginx static file service configuration and optimization

Root directory and index file The root directive ...

Solving problems encountered when importing and exporting Mysql

background Since I converted all my tasks to Dock...

Share 8 MySQL pitfalls that you have to mention

MySQL is easy to install, fast and has rich funct...

JavaScript event delegation principle

Table of contents 1. What is event delegation? 2....

Differences between MySQL CHAR and VARCHAR when storing and reading

Introduction Do you really know the difference be...

How to use an image button as a reset form button

When we make a form, we often set a submit button ...

Our thoughts on the UI engineer career

I have been depressed for a long time, why? Some t...

Forty-nine JavaScript tips and tricks

Table of contents 1. Operation of js integer 2. R...

JavaScript implementation of classic snake game

This article shares the specific code of JavaScri...

How to limit the input box to only input pure numbers in HTML

Limit input box to only pure numbers 1、onkeyup = ...

Display ellipsis effect when table cell content exceeds (implementation code)

illustrate In front-end development, you often en...

Invalid solution when defining multiple class attributes in HTML

In the process of writing HTML, we often define mu...