Docker compose-Mount volume on a few containers AND the host - docker

Im trying to share data between a few containers and the host using docker-compose. I have a docker-compose.yml file that looks like this:
version: '3'
services:
base:
container_name: base
build:
context: .
dockerfile: BaseDockerfile
volumes:
- dependencies:/volumes/dependencies
romee:
container_name: romee
build:
context: .
dockerfile: RomeeDockerfile
environment:
- PYTHONPATH=/volumes/base_dependencies/
volumes:
- dependencies:/volumes/base_dependencies
volumes:
dependencies:
Now the volume "dependencies" is shared successfully between the containers, but I want to also share it with the host. How can I do that?

The question is equivalent to how to specify a path of a named volume:
Solution:
volumes:
dependencies:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/abs/path/to/dependencies'
EDIT
The complete flow would be like,
Image: Dependency generator, at build time (docker build), generate dependency to /temp, then at run time (docker run / docker-compose up), cp -pr /temp /dependencies, after that it can exit 0.

Related

docke named volume doesn't appear

I have the following two docker compose files:
docker-compose.yml :
version: '2.3'
services:
# test11 service
test11:
build: test11/.
image: "test11"
and
docker-compose.yml (file inside the folder named test11 that contains Dockerfile and the following docker-compose ):
version: '2.3'
networks:
citrixhoneypot_local:
services:
# CitrixHoneypot service
test11:
build: .
container_name: test11
restart: always
networks:
- citrixhoneypot_local
ports:
- "443:443"
image: "test11:2006"
# read_only: true
volumes:
- test11:/opt/test11/logs
volumes:
test11:
driver:local
when i run docker-compose up --build for the first file, everything seems ok and the container build and i can run exec -it sh on it and get access.
but the problem is that the volume isn't made in the path
/var/lib/docker/volumes and i can't find it there.
also when i write in /opt/test11/logs from inside the docker container no file is made under /var/lib/docker/volumes .
I tried this with bind path too. same problem with that too.

Docker Windows Containers and NFS Volume

I have successfully created a Windows Docker Image and I can successfully run the container with docker-compose, for full functionality the application expects a folder location to exist, which is currently a NFS attached drive that contains a folder inside it that the application reads and writes to and is also used by other applications.
I have tried to find valid examples and documentation on Docker for Windows, Windows Containers and Docker Compose for Volumes that are NFS.
This is what I am currently trying
version: "3.4"
services:
service:
build:
context: .
dockerfile: ./Dockerfile
image: imageName
ports:
- 8085:80
environment:
-
volumes:
- type: volume
source: nfs_example
target: C:\example
volume:
nocopy: true
volumes:
nfs_example:
driver: local
driver_opts:
type: "nfs"
o: "addr=\\fileserver.domain,nolock,soft,rw"
device: ":\\Shared\\Folder"
The error message I get is:
ERROR: create service_nfs_example: options are not supported on this platform
NFS doesn't work.I solved it using SMB on the host, then mounted that volume.
New-SmbGlobalMapping -RemotePath \\contosofileserver\share1 -Credential Get-Credentials -LocalPath G:
version: "3.4"
services:
service:
build:
context: .
dockerfile: ./Dockerfile
image: imageName
ports:
- 8085:80
environment:
-
volumes:
- type: bind
source: G:\share1
target: C:\inside\container
This microsoft documentation on the windows containers helped me achieve this.

Mounting a host directory to a docker container from yaml/compose file

I have part of my current config like this
mymicroservice:
image: service_img
networks: myoverlay
volumes:
- /Users/abcdUser/mountme:/opt/company/
This does the job as my machine's directory gets mounted to /opt/company when I deploy the docker swarm service stack.
However, I want to specify the source directory under a separate volumes: and then specify that name over there. I think this is possible but I am not able to find the syntax.
So I want something along the following lines but not able to do so:
mymicroservice:
image: service_img
networks: myoverlay
volumes:
- myownvolume:/opt/company/
volumes:
- myownvolume: /Users/abcdUser/mountme
I want to clarify that myownvolume here is just pointing to the directory /Users/abcdUser/mountme and I am not intending to create a docker volume. Or there is any other better way to do this?
It is possible to do so but not with the standard setup. The default volume driver doesn't allow the format you are looking for. You need to use docker plugins which requires external installation. Consider the below yaml
version: '2'
services:
one:
image: alpine
working_dir: /one/
command: sleep 600
volumes:
- data:/one/
two:
image: alpine
working_dir: /two/
command: sleep 600
volumes:
- data:/two/
volumes:
data:
driver: local-persist
driver_opts:
mountpoint: /data/local-persist/data
Above would work when you have the local-persist plugin installed. https://github.com/CWSpear/local-persist
You can find about other plugins available on
https://docs.docker.com/engine/extend/legacy_plugins/#volume-plugins
Also if repetition of volumes entries is an issues for you then you can use anchors in YAML
version: '3'
services:
alpines:
image: alpine
command: sleep 200
volumes: &common_volumes
- ./data:/data
- ./config:/config
alpine2:
image: alpine
command: sleep 200
volumes: *common_volumes
$ docker-compose config
services:
alpine2:
command: sleep 200
image: alpine
volumes:
- /home/vagrant/so/volumes2/data:/data:rw
- /home/vagrant/so/volumes2/config:/config:rw
alpines:
command: sleep 200
image: alpine
volumes:
- /home/vagrant/so/volumes2/data:/data:rw
- /home/vagrant/so/volumes2/config:/config:rw
version: '3.0'
That's not possible. You either have to use the bind mount syntax or the volume syntax. Volumes at the compose config top level won't allow you to mix both. See how to define a general mount point in docker compose for a similar question.

docker-compose volumes_from equivalent with version 3

I'm trying to create an Nginx/PHP FPM setup with docker compose and am having issues with the version 3 volumes syntax/changes.
My Dockerfile:
FROM php:7-fpm
VOLUME /var/www/html
My docker-compose.yml:
version: "3"
services:
php:
build: .
volumes:
- ./html:/var/www/html
web:
image: nginx
links:
- php
ports:
- "8888:80"
volumes:
- php:/var/www/html
- ./default.conf:/etc/nginx/conf.d/default.conf
volumes:
php:
When I add an index.php file into ./html, I can view that by going to http://localhost:8888, but any static files (like CSS) return a 404 because Nginx cannot find those in its container (/var/www/html is empty on the nginx container). With version 3 docker compose files do not have volumes_from anymore, which is basically what I'm trying to replicate.
How can I get this to work with version 3?
For using "Named volumes" for sharing files between containers you need to define
1) volumes: section on the top level of yml file and define volume name
volumes:
php:
2) define volume section on first container like you did (Where share will mount)
web:
volumes:
- php:/var/www/html #<container_name>:<mount_point>
3) define volume section on second container (Share will mount from)
php:
volumes:
- php:/var/www/html
4) (optionally) If you need to store volume data on the host machine you can use local-persist docker plugin. You can specify docker volume driver and path where you data will be stored.
volumes:
php:
driver: local-persist
driver_opts:
mountpoint: /path/on/host/machine/
In your case you forgot define volume name for php container. Just replace
php:
build: .
volumes:
- ./html:/var/www/html
to
php:
build: .
volumes:
- php:/var/www/html
and use Local Persist Docker Plugin

Docker container not writing to volume

I want to setup ownCloud with Docker and Docker-Compose. To achieve this I have a docker-compose.yml with 3 containers and their volumes.
version: '2'
services:
nginx:
build: ./nginx
networks:
- frontend
- backend
volumes:
- owncloud:/var/www/html
owncloud:
build: ./owncloud
networks:
- backend
volumes:
- owncloud:/var/www/html
- data:/data
mysql:
build: ./mariadb
volumes:
- mysql:/var/lib/mysql
networks:
- backend
volumes:
owncloud:
driver: local
data:
driver: local
mysql:
driver: local
networks:
frontend:
driver: bridge
backend:
driver: bridge
I also tried it without the data volume. ownCloud could not write to /data or without this volume to /var/www/html/data. The log only shows timestamps whenever I accessed ownCloud. Changing from data:/data to a hosted volume /var/ownclouddata:/data results in no difference.
The Dockerfiles have only one line each: FROM:image
I´ve tried adding RUN mkdir /data, but it didn´t fix anything.
You need to mount the volumes in the Dockerfile something like this.
VOLUME /data
Later in your docker-compose file, you can either use a named volume like you did earlier or simply use it like this.
/mnt/test:/data
Here /mnt/test is your host volume path and /data is your docker container path.
Hope it helps!

Resources