Docker container not writing to volume - docker

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!

Related

docker-compose how to persist mongodb-database

I am using docker-compose and here is my docker-compose.yaml file:
version: "3.7"
services:
node:
container_name: my-app
image: my-app
build:
context: ./my-app-directoty
dockerfile: Dockerfile
command: npm run dev
environment:
MONGO_URL: my-database
port: 3000
volumes:
- ./my-app-directory/src:/app/src
- ./my-app-directory/node_modules:/app/node_modules
ports:
- "3000:3000"
networks:
- my-app-network
depends_on:
- my-database
my-database:
container_name: my-database
image: mongo
ports:
- "27017:27017"
networks:
- my-app-network
networks:
my-app-network:
driver: bridge
I expect to find a clear and newly created database each time I run the following command:
docker-compose build
docker-compose up
But this is not the case. When I bring the containers up with docker-compose up, my database has the exact state of the last time I shut it down with docker-compose down command. And since I have not specified a volume prop in my-database object, is this normal behaviour? Does this mean that no other action to persisting database state is required? And can I use this in production if I ever choose to use docker-compose?
The mongo image define the following volumes:
/data/configdb
/data/db
So docker-volume will create and use a unamed volume for data/db.
If you want to have a new one, use:
docker-compose down -v
docker-compose up -d --build
Or use a mount point mounted on the volume location like:
volumes:
- ./db:/data/db:rw
And drop your local db directories when you want to start over.

persistent data using map volume to kong container

I am trying to learn kong, using docker-compose, i am able to run kong+konga and create services. But whenever i do docker-compose down and then up again i lose all my data:
kong:
container_name: kong
image: kong:2.1.4-alpine
restart: unless-stopped
networks:
kong-net:
ipv4_address: 172.1.1.40
volumes:
- kong_data:/usr/local/kong/declarative
environment:
KONG_DATABASE: postgres
KONG_PG_HOST: kong-database
KONG_PG_USER: kong
KONG_PG_PASSWORD: password
KONG_ADMIN_LISTEN: "0.0.0.0:8001, 0.0.0.0:8444 ssl"
KONG_DB_UPDATE_FREQUENCY: 1m
KONG_PROXY_ACCESS_LOG: /dev/stdout
KONG_ADMIN_ACCESS_LOG: /dev/stdout
KONG_PROXY_ERROR_LOG: /dev/stderr
KONG_ADMIN_ERROR_LOG: /dev/stderr
depends_on:
- kong-migration
ports:
- "8001:8001"
- "8444:8444"
- "8000:8000"
- "8443:8443"
Looks like volume mapping not working. pleasE help
If you want to keep data when your kong docker-compose is down it is better to use kong in database mode.
So then you will create a persistent volume for your database and it will keep your changes.
By the kong manual you will find there are two type of database supported: postgresql and cassandra
Postgresql is my choice for small project as I'm not planning for huge horizontal scale with cassandra database.
As you will find in the manual starting your project with docker and database is very simple.
But remember to add a volume to your database service as in the sample mentioned in manual there is no volume.
For postgresql you can add: -v /custom/mount:/var/lib/postgresql/data in docker run command
or
volumes:
postgress-data:
driver: local
services:
postgress:
restart: unless-stopped
image: postgres:latest
environment:
- POSTGRES_USER=your_db_user
- POSTGRES_DB=kong
- POSTGRES_PASSWORD=your_db_password
volumes:
- postgres-data:/var/lib/postgresql/data
Answer : You should use docker volume for having persistent data
As reference says :
Volumes are the preferred mechanism for persisting data generated by and used by Docker containers
First step is to create a volume that you want your host and docker container communicate using :
docker volume create new-volume
Second step is to use that volume in a docker-compose (in your case)
A single docker compose service with a volume looks like this:
version: "3.9"
services:
frontend:
image: node:lts
volumes:
- myapp:/home/node/app
volumes:
myapp:
On the first invocation of docker-compose up the volume will be created. The same volume will be reused on following invocations.
A volume may be created directly outside of compose with docker volume create and then referenced inside docker-compose.yml as follows:
version: "3.9"
services:
frontend:
image: node:lts
volumes:
- myapp:/home/node/app
volumes:
myapp:
external: true

Docker compose mounts volume for one service only

I am trying to build two services that mount the same volumes. Running the following docker-compose configuration allows the cms service to mount the directory, however, the client service does not mount the directory.
I've tried docker inspect for the service, and mounts is just an empty array.
version: "2"
services:
cms:
container_name: cms_keystone
build: ./cms_keystone/
network_mode: bridge
environment:
- NODE_ENV=docker_production
ports:
- "3010:3000"
volumes:
- /data/naqib.info_static_content:/data/naqib.info_static_content:rw
client:
container_name: client_nextjs
build: ./client_nextjs/
network_mode: bridge
environment:
- NODE_ENV=docker_production
# - YARN_CACHE_FOLDER=/data/naqib.info_static_content/shm/yarn_cache
ports:
- "8001:7001"
- "8002:7002"
depends_on:
- cms
volumes:
- /data/naqib.info_static_content:/data/naqib.info_static_content:ro
Any suggestion what should I do to debug?
Update:
I'm not sure if it is relevant here, but the volume in the host machine is an HDD connected via USB3.

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

Resources