Why can't docker build COPY files to the /dev folder?
Asked Answered
K

2

8

I want to create a docker image where I add a file to the /dev folder. I'm using this Dockerfile:

FROM ubuntu:bionic
COPY test.txt /dev/

After building this with:

docker build -t test .

I get a docker image where nothing has been added to the /dev folder. No error has been thrown by docker build.

I find this very strange because copying to different folders works fine. For example

  • COPY test.txt /
  • COPY test.txt /root/
  • COPY test.txt /home/

all work fine.

Does the /dev folder have some special permissions? How do I copy a file to the /dev folder?

I'm using Docker Toolbox on windows.

Ken answered 28/5, 2019 at 16:0 Comment(4)
Why do you want to use the /dev folder? That one is a special folder which contains references to all devices?Ingratiating
See: tldp.org/LDP/sag/html/dev-fs.htmlIngratiating
Related: askubuntu.com/questions/165219/…Ingratiating
@Ingratiating I want to add a driver.more info github.com/EAIBOT/ydlidarKen
E
11

/dev is a special folder on linux systems reserved to maintain the devices related ressources (filesystem, disks, etc...) and mounted on a special filesystem. In a docker container, it will be remounted with a tmpfs dedicated filesystem and is not on the main container filesystem (/). See the following example:

$ docker run -it --rm ubuntu:18.04
root@17b9ad96ccbc:/# df -h /dev/
Filesystem      Size  Used Avail Use% Mounted on
tmpfs            64M     0   64M   0% /dev

In your case, your file was actually copied during the build but in a temporary filesystem that died as soon as the build was finished.

Conclusion: don't use /dev as a destination, choose an other folder.

Exostosis answered 28/5, 2019 at 16:19 Comment(1)
Thanks @Zeitounator. Now I understand why the file file dissapeared whil no error was thrownKen
S
0

I think /dev is mounted when the container is created, so if you really need it you can copy (or symlink) a file into it after that, e.g. on a entrypoint.sh script:

#!/bin/bash
set -e

ln -s /app/my_virtual_device /dev/my_virtual_device

exec "$@"

Then reference it on the Dockerfile:

FROM ...

...

ENRTYPOINT ["bash", "entrypoint.sh"]
Subcritical answered 26/7, 2022 at 8:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.