I am using owncloud docker image to create my owncloud. The problem is it's storing the data inside the docker image. However, I want one of my driver ( I am using windows) to be used as data files.
volumes:
files:
driver: local
services:
owncloud:
volumes:
- files:/mnt/data
This is what part of the docker-compose files looks like, I tried changing files:/mnt/data to .:/mnt/data. However, I started getting error when I tried to run the docker-compose.
The right way is to use double quotes to expand . as the current directory:
version: '3'
services:
nginx:
image: nginx
volumes:
- ".:/path/inside/container/"
Related
I've got a docker-container that's running a program which outputs something. I want that something to be stored on my local file system, not in a volume. Bind mounts are what I need.
I have a single folder called oniongen and this is where I want the output.
My compose file looks like this:
version: '3'
services:
oniongen:
image: nwtgck/mkp224o
volumes:
- ./oniongen:/gen
command: >
sh -c "mkp224o abcd -d gen"
However, the outputs never reach my local file system.
I've tried
volumes:
oniongen:
driver: local
and this incarnation just in case
volumes:
oniongen:
and also specifying bind under volume type
services:
oniongen:
image: nwtgck/mkp224o
volumes:
- type: bind
source: ./oniongen
target: /gen
volumes:
oniongen:
I've tried other suggestions too and read the docs, but can't seem to get the output stored locally.
If I sh into the container I can see the gen folder and the files in it.
How do I get these files to be stored on my local system?
You're really over-complicating it, just mount a local folder as your volume:
services:
oniongen:
image: nwtgck/mkp224o
volumes:
- ./oniongen:/gen
No need for the volumes top level declaration either.
This will result in everything the container puts in its /gen folder appearing in the host's ./oniongen folder and vice-versa.
I have some confusing moments with docker-compose volumes (In docker windows server (container)).
I wrote the next configuration
volumes:
- "C:/ProgramData/Docker/volumes/admin-stat-logs:C:/app/Logs"
and why it so necessary to write the full path to the volume?
if I write:
volumes:
- "admin-logs:C:/app/Logs"
it generates the error
ERROR: Named volume "admin-logs:C:/app/Logs:rw" is used in service "admin-stat-table" but no declaration was found in the volumes section.
why does it happen?
UPDATE
let me show what I mean(I made wrong explonation). In an image, we have a short announcement from the official documentation.
I write the same in my case:
volumes:
- admin-logs:C:/app/Logs
I create volume before by the terminal. When I start docker-compose up I get the next error
ERROR: Named volume "admin-stat-logs:C:/app/Logs:rw" is used in service "admin-stat-table" but no declaration was found in the volumes section.
I made a mistake in the first case, it's a simple mount to a folder, sorry for my misunderstanding.
volumes:
- "C:/ProgramData/Docker/volumes/admin-stat-logs:C:/app/Logs"
You don't need to use a full path. You can use a relative path to the location of your docker-compose.yml file.
When you write admin-logs:C:/app/Logs, you're telling docker-compose to use a volume named admin-logs. If you want to use a folder where you docker-compose is located, you can write ./admin-logs:C:/app/Logs.
If you plan to use a separated docker volume, you need to first define it in the docker-compose.yml. Here's an example covering both cases:
version: '3'
services:
mytest:
image: ubuntu:18.04
volumes:
- "mymnt:/mnt/volume"
- "./mymnt_on_host:/mnt/mounted_folder"
volumes:
mymnt:
You'll need a mymnt_on_host directory in the folder where the docker-compose.yml is located.
For more info about this topic, you can check the reference guide for the docker-compose.yml
UPDATE:
If you plan to use an already created volume in your docker-compose, you can use external: true in the volumes definition.
e.g.
version: '3'
services:
mytest:
image: ubuntu:18.04
volumes:
- "mymnt:/mnt/volume"
- "./mymnt_on_host:/mnt/mounted_folder"
volumes:
mymnt:
my_existing_volume:
external: true
Now if you start this, docker-compose won't try to generate my_existing_volume but it'll expect to find it already on the machine.
For more info check external reference.
Can you show your version value in the docker-compose.yml file. It should be version: '3' or version: '3.5'. Probably you have version: '2'
I'm running Docker on Windows and I've used docker-compose to start a container which created a folder containing the following folders/files:
docker-compose.yml
data
|___conf
| |__config.yaml
|
|___plugins
|___storage
I want to be able to back up the data that is stored in the container, I can see the data is being generated inside the storage directory. I've looked online and they say to back up volumes I would need to spin another container up and stop my current container etc... Can't I just write a script to back up that storage directory or better yet the entire data directory so that my data doesn't get lost?
My docker-compose.yml looks like this:
version: '3'
services:
verdaccio:
image: verdaccio/verdaccio:4
container_name: verdaccio
environment:
- VERDACCIO_PORT=4873
ports:
- "4873:4873"
volumes:
- "./data/storage:/verdaccio/storage"
- "./data/conf:/verdaccio/conf"
- "./data/plugins:/verdaccio/plugins"
volumes:
verdaccio:
driver: local
I have a few questions about Docker volumes. I have installed Docker and docker-compose on a fresh host running debian stretch. I managed to get a docker-compose file running for a simple nginx/php-fpm project, both containers mounted on the directory containing the source code. I wanted to try to create a single volume that would be shared across my containers but I have a few issue, and my understanding of the official documentation is not helping.
So this is an idea of what I'm trying to achieve :
Question 1 : Trying to create a volume from a dockerfile on a directory mounted from host
docker-compose.yml :
version: '3'
services:
php:
build:
context: .
dockerfile: php.dockerfile
volumes:
- ./host-project-directory:/project
php.dockerfile :
FROM php:7-fpm
VOLUME project
from my understanding, when running docker-compose we should have a volume created on host containing all files from /project from container. And /project from container should contain all files from ./host-project-directory from host.
If I ls the content of /project on container I can see the files from host, but using docker volume list, there are no volumes created on host, why ?
Question 2 : How to populate and use this volume from another container ?
version: '3'
services:
php:
build:
context: .
dockerfile: php.dockerfile
volumes:
- named-volume:/project
web:
image: nginx
links:
- php
volumes:
- named-volume:/project
volumes:
named-volume:
This should create a volume called 'named-volume' and bind it to /project directories on both containers php and web.
Now, how to populate this volume with content from ./host-project-directory ?
I've tried adding a dockerfile like
ADD ./host-project-directory /project
But nothing changed and the volume remained empty.
I'm sorry if this is due to my lack of experience using Docker but I can't figure out how to make this simple thing work.
Thank you for your time !
For the first question, I try a simple docker file like this:
FROM php:7-fpm
COPY ./project /project
And a docker-compose like this:
version: '3'
services:
php:
build: .
volumes:
- named-volume:/project
web:
image: nginx
links:
- php
volumes:
- named-volume:/project
volumes:
named-volume:
Since you create the volume on docker-compose you don't need to create that in the Dockerfile.
Running docker volume list, I'm able to see the volume created with a local driver. Making ls inside the folder I'm also able to see the file. It's important to note, that the file present in you local directory it's not the same that the file inside the container. So if you edit the files in the host this will not change the files in container. That's because you have your volume created in another path, probably at: /var/lib/docker/volumes/...
This happens because you map the volume to the path, but you not specifies where you want the volume. To do that just make your docker-compose like this:
version: '3'
services:
php:
build: .
volumes:
- ./project:/project
web:
image: nginx
links:
- php
volumes:
- ./project:/project
Making this I'm still able to see the volume with the volume list command but without a name.
So I don't know why you are not able to see the volume in the list.
For question 2:
Doing the example above I have the files inside the container that exists in my local "project" folder.
Please check that the path to the local folder is correct.
A bind mount is not the same thing as a volume. You're defining a named volume here, but wanting the functionality of a bind mount.
Try this
version: '3'
services:
php:
build:
context: .
dockerfile: php.dockerfile
volumes:
- ./host-project-directory:/project
web:
image: nginx
links:
- php
volumes:
- ./host-project-directory:/project
I am trying to allow nginx to proxy between multiple containers while also accessing the static files from those containers.
To share volumes between containers created using docker compose, the following works correctly:
version: '3.6'
services:
web:
build:
context: .
dockerfile: ./Dockerfile
image: webtest
command: ./start.sh
volumes:
- .:/code
- static-files:/static/teststaticfiles
nginx:
image: nginx:1.15.8-alpine
ports:
- "80:80"
volumes:
- ./nginx-config:/etc/nginx/conf.d
- static-files:/static/teststaticfiles
depends_on:
- web
volumes:
static-files:
However what I actually require is for the nginx compose file to be in a separate file and also in a completely different folder. In other words, the docker compose up commands would be run separately. I have tried the following:
First compose file:
version: '3.6'
services:
web:
build:
context: .
dockerfile: ./Dockerfile
image: webtest
command: ./start.sh
volumes:
- .:/code
- static-files:/static/teststaticfiles
networks:
- directorylocation-nginx_mynetwork
volumes:
static-files:
networks:
directorylocation-nginx_mynetwork:
external: true
Second compose file (ie: nginx):
version: '3.6'
services:
nginx:
image: nginx:1.15.8-alpine
ports:
- "80:80"
volumes:
- ./nginx-config:/etc/nginx/conf.d
- static-files:/static/teststaticfiles
networks:
- mynetwork
volumes:
static-files:
networks:
mynetwork:
The above two files work correctly in the sense that the site can be viewed. The problem is that the static files are not available in the nginx container. The site therefore displays without any images etc.
One work around which works correctly found here is to change the nginx container static files volume to instead be as follows:
- /var/lib/docker/volumes/directory_static-files/_data:/static/teststaticfiles
The above works correctly, but it seems 'hacky' and brittle. Is there another way to share volumes between containers which are housed in different compose files without needing to map the /var/lib/docker/volumes directory.
By separating the 2 docker-compose.yml files as you did in your question, 2 different volumes are actually created; that's the reason you don't see data from web service inside volume of nginx service, because there are just 2 different volumes.
Example : let's say you have the following structure :
example/
|- web/
|- docker-compose.yml # your first docker compose file
|- nginx/
|- docker-compose.yml # your second docker compose file
Running docker-compose up from web folder (or docker-compose -f web/docker-compose.yml up from example directory) will actually create a volume named web_static-files (name of the volume defined in docker-compose.yml file, prefixed by the folder where this file is located).
So, running docker-compose up from nginx folder will actually create nginx_static-files instead of re-using web_static-files as you want.
You can use the volume created by web/docker-compose.yml by specifying in the 2nd docker compose file (nginx/docker-compose.yml) that this is an external volume, and its name :
volumes:
static-files:
external:
name: web_static-files
Note that if you don't want the volume (and all resources) to be prefixed by the folder name (default), but by something else, you can add -p option to docker-compose command :
docker-compose \
-f web/docker-compose.yml \
-p abcd \
up
This command will now create a volume named abcd_static-files (that you can use in the 2nd docker compose file).
You can also define the volumes creation on its own docker-compose file (like volumes/docker-compose.yml) :
version: '3.6'
volumes:
static-files:
And reference this volume as external, with name volumes_static-files, in web and nginx docker-compose.yml files :
volumes:
volumes_static-files:
external: true
Unfortunately, you cannot set the volume name in docker compose, it will be automatically prefixed. If this is really a problem, you can also create the volume manually (docker volume create static-files) before running any docker-compose up command (I do not recommand this solution though because it adds a manual step that can be forgotten if you reproduce your deployment on another environment).