Share files between host system and docker container using specific UID

alkalinity

I'm trying to share files within a Docker guest using the volume sharing. In order to get the same UID, and therefore interoperability with those files, I would like to create a user in the Docker guest with the same UID as my own user.

In order to test out the idea, I wrote the following simplistic Dockerfile:

FROM phusion/baseimage

RUN touch /root/uid-$UID

Testing it with docker build -t=docktest . and then docker run docktest ls -al /root reveals that the file is simply named uid-.

Is there a means to share host environment variables with Docker during the guest build process?

ISanych

The environment is not shared, you could use -e, --env options to set env variables in container.

I usually use this approach when I want to have the same owner of the mapped volume: I check uid & gid of directory in container and then create a corresponding user. Here my script (setuser.sh) which creates a user for a directory:

#!/bin/bash

setuser() {
  if [ -z "$1" ]; then
    echo "Usage: $0 <path>"
    return
  fi
  CURRENT_UID=`id -u`
  DEST_UID=`stat -c "%u" $1`
  if [ $CURRENT_UID = $DEST_UID ]; then
    return
  fi
  DEST_GID=`stat -c "%g" $1`
  if [ -e /home/$DEST_UID ]; then
    return
  fi
  groupadd -g $DEST_GID $DEST_GID
  useradd -u $DEST_UID -g $DEST_GID $DEST_UID
  mkdir -p /home/$DEST_UID
  chown $DEST_UID:$DEST_GID /home/$DEST_UID
}
setuser $1

And this is the wrapper script which runs commands as the user, where the directory with permissions is specified either as $USER_DIR or in /etc/user_dir

#!/bin/bash
if [ -z "$USER_DIR" ]; then
  if [ -e /etc/user_dir ]; then
    export USER_DIR=`head -n 1 /etc/user_dir`
  fi
fi
if [ -n "$USER_DIR" ]; then
  if [ ! -d "$USER_DIR" ]; then
    echo "Please mount $USER_DIR before running this script"
    exit 1
  fi
  . `dirname $BASH_SOURCE`/setuser.sh $USER_DIR
fi
if [ -n "$USER_DIR" ]; then
  cd $USER_DIR
fi
if [ -e /etc/user_script ]; then
  . /etc/user_script
fi
if [ $CURRENT_UID = $DEST_UID ]; then
  "$@"
else
  su $DEST_UID -p -c "$@"
fi

P.S. Alleo suggested different approach: to map users and groups files into container and to specify uid and gid. So your container does not depend on built-in users/groups you could use it without additional scripts.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to share folder between host os and docker container

From Dev

Docker replicate UID/GID in container from host

From Dev

Deleting files inside docker container not freeing up space on host system

From Dev

Docker (Dockerfile): Share a directory from host to container

From Dev

Sharing files between container and host

From Dev

Docker: Copying files from Docker container to host

From Dev

Reuse host binaries or share between containers in Docker

From Dev

Are Docker Swarm container stats are host specific?

From Dev

How can I have shared assets (pictures, text documents, etc) between my Docker container and host system?

From Java

How to copy files from host to Docker container?

From Java

Copying files from Docker container to host

From Dev

How to edit Docker container files from the host?

From Dev

expose files from docker container to host

From Dev

What is the difference between Docker Host and Container

From Dev

Docker container Network connection between different host

From Dev

Communication between Windows Host and Linux Docker Container

From Dev

Volume share from container to host docker-compose

From Dev

How do I share a directory between an LXC container and the host?

From Dev

How to pause a docker container then reboot host system and unpause container?

From Dev

Share single files between docker containers

From Dev

Interprocess communcation between docker and host system

From Dev

How to share host network bridge when using docker in docker

From Dev

How to share a folder between my Mac and a Docker container

From Dev

Run docker container on Linux VM which is running on Linux host system

From Dev

Mount directory in Container and share with Host

From Dev

Mount directory in Container and share with Host

From Dev

Access host IP inside docker container using docker-compose

From Dev

Copy files from host to docker container then commit and push

From Dev

How to Copy Files From Docker Ubuntu Container to Windows Host

Related Related

  1. 1

    how to share folder between host os and docker container

  2. 2

    Docker replicate UID/GID in container from host

  3. 3

    Deleting files inside docker container not freeing up space on host system

  4. 4

    Docker (Dockerfile): Share a directory from host to container

  5. 5

    Sharing files between container and host

  6. 6

    Docker: Copying files from Docker container to host

  7. 7

    Reuse host binaries or share between containers in Docker

  8. 8

    Are Docker Swarm container stats are host specific?

  9. 9

    How can I have shared assets (pictures, text documents, etc) between my Docker container and host system?

  10. 10

    How to copy files from host to Docker container?

  11. 11

    Copying files from Docker container to host

  12. 12

    How to edit Docker container files from the host?

  13. 13

    expose files from docker container to host

  14. 14

    What is the difference between Docker Host and Container

  15. 15

    Docker container Network connection between different host

  16. 16

    Communication between Windows Host and Linux Docker Container

  17. 17

    Volume share from container to host docker-compose

  18. 18

    How do I share a directory between an LXC container and the host?

  19. 19

    How to pause a docker container then reboot host system and unpause container?

  20. 20

    Share single files between docker containers

  21. 21

    Interprocess communcation between docker and host system

  22. 22

    How to share host network bridge when using docker in docker

  23. 23

    How to share a folder between my Mac and a Docker container

  24. 24

    Run docker container on Linux VM which is running on Linux host system

  25. 25

    Mount directory in Container and share with Host

  26. 26

    Mount directory in Container and share with Host

  27. 27

    Access host IP inside docker container using docker-compose

  28. 28

    Copy files from host to docker container then commit and push

  29. 29

    How to Copy Files From Docker Ubuntu Container to Windows Host

HotTag

Archive