Accessing Volumes in docker - docker

I want to access an external directory 'web-app' in my docker container.
Consider the directory structure below:
MyDcocker
|-dockerFile
|-web-app
|-flask-web.py
I have the following lines in my dockerfile:
VOLUME ["web-app"]
# Run flask-web (API) file
CMD ["python3","web-app/flask-web.py"]
When running the image, I get the error:
python3: can't open file 'web-app/flask-web.py': [Errno 2] No such file or directory
I believe the directory has not been mounted properly. How do I solve this ?

You will write in Dockerfile how you build the container, not how container will interact with your host.
For mount a directory from your host in your container, you have 2 solutions :
with docker command line :
docker run -v $(pwd)/web-app:/var/lib/web-app/ dck-image-name
with docker-compose
version: '2'
services:
myservice:
image: dck-image-name
volumes:
- ./web-app:/var/lib/web-app/

Related

How to mount a tmp directory with docker-compose?

How do you specify a mount volume in docker-compose, so your Dockerfile can access files from it?
I have a docker-compose.yml like:
version: "3.6"
services:
app_test:
build:
context: ..
dockerfile: Dockerfile
volumes:
- /tmp/cache:/tmp/cache
And in my Dockerfile, I want to access files from /tmp/cache via RUN like:
RUN cat /tmp/cache/somebinary.tar.gz | processor.sh
However, running docker-compose gives me the error:
/tmp/cache/somebinary.tar.gz does not exist
Even though on the host, ls /tmp/cache/somebinary.tar.gz confirms it does exist.
Why is docker-compose/Docker unable to mount or access my host directory?
Dockerfile RUN commands are executed at build time of the image.
The volume is mounted at run time once the image is run as a container. So the mounted files will not be available until you spawn a container based on your image.
To define the commands to use at run time, use CMD, or depending on how you intend your image to be used ENTRYPOINT.
You would need to add this at the end of your Dockerfile:
CMD cat /tmp/cache/somebinary.tar.gz | processor.sh

Can not run appimages inside rootless docker container

I have installed rootless docker on ubuntu 20.04 [https://docs.docker.com/engine/security/rootless/][1]
I have download vscodium appimage from [https://github.com/VSCodium/vscodium/releases/download/1.66.0/VSCodium-1.66.0-1648720116.glibc2.17-x86_64.AppImage][1]
i have shared host directory containing this Appimage with rootless docker container. But it doesn't run. When I manually install(apt-get install) any GUI package(ex. firefox) inside the container it runs successfully.
output of the command: docker-compose up vscodium
Creating vscodium ... done
Attaching to vscodium
vscodium | codium: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
vscodium exited with code 127
content of file docker-compose.yml
version: "3"
services:
vscodium:
image: python:3.10.4-bullseye
entrypoint: custom-docker-entrypoint.sh
container_name: vscodium
environment:
- DISPLAY=${DISPLAY}
volumes:
- /tmp/.X11-unix:/tmp/.X11-unix:ro
- $HOME/.Xauthority:$HOME/.Xauthority:ro
- ./custom-docker-entrypoint.sh:/usr/local/bin/custom-docker-entrypoint.sh
- ./appImages/VSCodium.AppImage:/ide/VSCodium.AppImage
network_mode: host
content of file custom-docker-entrypoint.sh
#!/bin/sh
chmod a+x /ide/VSCodium.AppImage
/ide/VSCodium.AppImage --appimage-extract-and-run
A few notes on running AppImages insude docker:
AppImages require fuse to run which is usually not available/usable on docker
Extract the AppImage contents and mount that folder on your docker
missing libnss3.so, you will have to install this on the host system. If it doesn't work you will have to report it to the AppImage author to for them to include it in the bundle.

When running docker-compose remotely, an error occurs with mounting volumes

I am trying to run a project on docker-compose via a remote server. Everything works, but as soon as I add the item about mounting the volume, it gives an error:
Error response from daemon: invalid mount config for type "bind": invalid mount path: 'C:/Users/user/Projects/my-raspberry-test' mount path must be absolute
To run I use tools from PhpStorm.
The docker-compose.yml file itself looks like this:
version: "3"
services:
php:
image: php:cli
volumes:
- ./:/var/www/html/
working_dir: /var/www/html/
ports:
- 80:80
command: php -S 0.0.0.0:80
I checked by ssh:
Daemon is running,
Docker works (on a similar Dockerfile with the same tasks),
Docker-compose works (on the same file).
Also checked docker remote run using phpstorm and file:
FROM php:cli
COPY . /var/www/html/
WORKDIR /var/www/html/
CMD php -S 0.0.0.0:80
It didn’t give an error and it worked.
OS on devices:
PC: Windows 10
Server: Fedora Server
Without mounting the volume in docker-compose, everything starts. Maybe someone faced a similar problem?
php for an example.
The path must be absolute for the remote host and the project data itself must be loaded there. That is, you need to upload the project to a remote host.
I corrected everything like this:
volumes:
- /home/peter-alexeev/my-test:/var/www/html/

What is happening when using ../ with docker-compose volume

I am having problems with writing files out from inside a docker container to my host computer. I believe this is a privilege issue and prefer not to set privileged: True. A work around for writing out files is by pre-pending ../ to a volume in my docker-compose.yml file. For example,
version: '3'
services:
example:
volumes:
- ../:/example
What exactly is ../ doing here? Is it taking from the container's privileges and "going up" a directory to the host machine? Without ../, I am unable to write out files to my host machine.
Specifying a path as the source, as opposed to a volume name, bind mounts a host path to a path inside the container. In your example, ../ will be visible inside the container at /example on a recent version of docker.
Older versions of docker can only access the directory it is in and lower, not higher, unless you specify the higher directory as the context.
To run the docker build from the parent directory:
docker build -f /home/me myapp/Dockerfile
As opposed to
docker build -f /home/me/myapp Dockerfile
Doing the same in composer:
#docker-compose.yml
version: '3.3'
services:
yourservice:
build:
context: /home/me
dockerfile: myapp/Dockerfile
Or with your example:
version: '3'
services:
build:
context: /home/me/app
dockerfile: docker/Dockerfile
example:
volumes:
- /home/me/app:/example
Additionally you have to supply full paths, not relative paths. Ie.
- /home/me/myapp/files/example:/example
If you have a script that is generating the Dockerfile from an unknown path, you can use:
CWD=`pwd`; echo $CWD
To refer to the current working directory. From there you can append /..
Alternately you can build the image from a directory one up, or use a volume which you can share with an image that is run from a higher directory, or you need to output your file to stdout and redirect the output of the command to the file you need from the script that runs it.
See also: Docker: adding a file from a parent directory
The statement volumes: ['../:/example'] makes the parent directory of the directory containing docker-compose.yml on the host (../) visible inside the container at /example. Host directory bind-mounts like this, plus some equivalent constructs using a named volume attached to a specific host directory, are the only way a container can write out to the host filesystem.

How to create data volume in docker?

I am having some gitcode (around 10gb) kept in a folder "src" in my home directory. I have read somewhere that we can mount this code as a data volume in docker.
I am a newbie to docker. I only have an idea of using "docker volume create" command, but totally unsure about how to use it.
Could someone help me in achieving this.
Bishal's answer has instructions how to use mapping with Docker compose. When using plain docker, use command
docker run -v <absolute path to src folder on host>:<absolute path on container> some-image
# Real example:
docker run -v ~/src:/src some-image
Docker allows for easy volume mapping. This can be configured in your docker-compose.yaml file.
Volume mapping allows you to share a directory in your host machine to your docker-container.
version: '2'
services:
web:
build: .
volumes:
- .:/code
In the above snippet, the files in the current directory of host machine will be mapped to /code of the docker container.
This article has detailed explanation.

Resources