Particularidades do Bind Mount
Arquivos criados pelo container e pelo terminal identificados com usuários diferentes:
$ ls -l
total 0
-rw-r--r-- 1 marcelo marcelo 0 Aug 25 11:30 teste.txt
-rw-r--r-- 1 root root 0 Aug 25 11:31 teste2.txt
Essa seria uma desvantagem com níveis de segurança etc.
Uma boa prática é criar o container com o mesmo User ID do diretório do Host:
/tmp/volume/bind/vol$ echo $UID
1000
Criação de um Dockerfile:
/tmp/volume/bind$ touch Dockerfile
/tmp/volume/bind$ vim Dockerfile
FROM ubuntu
# Remove qualquer usuário existente com UID 1000 para evitar conflitos
RUN if id -u 1000 >/dev/null 2>&1; then userdel -f $(getent passwd 1000 | cut -d: -f1); fi
# Cria o usuário 'barbieri' com UID 1000
RUN useradd -u 1000 barbieri
# Define o usuário 'barbieri' como padrão
USER barbieri
Construção da imagem:
/tmp/volume/bind$ docker image build -t ubuntu-bindmount -f Dockerfile .
[+] Building 1.0s (7/7) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 339B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:latest 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> CACHED [1/3] FROM docker.io/library/ubuntu:latest 0.0s
=> [2/3] RUN if id -u 1000 >/dev/null 2>&1; then userdel -f $(getent passwd 1000 | cut -d: -f1); fi 0.3s
=> [3/3] RUN useradd -u 1000 marcelo 0.5s
=> exporting to image 0.1s
=> => exporting layers 0.0s
=> => writing image sha256:1ccb0d084d3f91c4e281bf2a13c9cfc929c15c8338539526eb9c91e59ce9db0d 0.0s
=> => naming to docker.io/library/ubuntu-bindmount
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu-bindmount latest 1ccb0d084d3f 51 seconds ago 78.1MB
Execução do container:
$ docker container run -it --mount type=bind,source="$(pwd)"/vol,target=/app ubuntu
-bindmount /bin/bash
marcelo@e3c8292fd715:/$ ls
app bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
marcelo@e3c8292fd715:/$ cd app
marcelo@e3c8292fd715:/app$ ls
teste.txt teste2.txt
marcelo@e3c8292fd715:/app$ touch teste3.txt
marcelo@e3c8292fd715:/app$ ls -l
total 0
-rw-r--r-- 1 marcelo marcelo 0 Aug 25 14:30 teste.txt
-rw-r--r-- 1 root root 0 Aug 25 14:31 teste2.txt
-rw-r--r-- 1 barbieri barbieri 0 Aug 25 14:50 teste3.txt
/tmp/volume/bind/vol$ ls -l
total 0
-rw-r--r-- 1 marcelo marcelo 0 Aug 25 11:30 teste.txt
-rw-r--r-- 1 root root 0 Aug 25 11:31 teste2.txt
-rw-r--r-- 1 marcelo marcelo 0 Aug 25 11:50 teste3.txt
O usuário "barbieri" dentro do container é o mesmo usuário "marcelo" fora do container.
Last updated