how to save static content in docker container - docker

I'm a little confused about how to configure docker to save user generated files in a volume. I have my docker-compose set up as such:
services:
web_service:
container_name: webapp
build: .
environment:
- ASPNETCORE_URLS=http://+:5000
- ASPNETCORE_ENVIRONMENT=Docker
volumes:
- type: bind
source: ./wwwroot/static
target: /usr/lib/volumes
ports:
- "5000"
networks:
- webnet
nginx:
container_name: nginx
image: mynginx:1.0
ports:
- "80:80"
networks:
- webnet
networks:
webnet:
driver: bridge
Every time I run docker-compose up it creates a new volume. I want to be able to reuse a volume and save the static files across containers and on top of that I have a sqlite database file in the working directory that needs r/w permissions. I guess what I don't really understand is the difference between source and target, because I assume the source is the file storage folder, but the documentation uses a named volume as the source.
I've read through the documentation and it shows this:
version: "3.8"
services:
web:
image: nginx:alpine
ports:
- "80:80"
volumes:
- type: volume
source: mydata
target: /data
volume:
nocopy: true
- type: bind
source: ./static
target: /opt/app/static
networks:
webnet:
volumes:
mydata:

Related

Volume not used even though declared in docker compose // creating a directory at location in assumed volume is copyied to other location in the same

I am trying to use multiple containers defined together in the following docker-compose .yml file
services:
mobile_app:
container_name: mobile_app # optional
build:
context: .
dockerfile: Dockerfile # optional, default is Dockerfile in context
ports:
- 19000:19000
- 19001:19001
- 19002:19002
environment:
- EXPO_DEVTOOLS_LISTEN_ADDRESS=0.0.0.0
# - CHOKIDAR_USEPOLLING=true # Drains batterty, to be checked without usage, used for hot reload
- REACT_NATIVE_PACKAGER_HOSTNAME=<?????????> # TODO: USE YOUR IPV4 address without any port
- MONGODB_CONNSTRING=mongodb://root:secret#mongodb # (experimental)
volumes:
- type: volume
source: sihv
target: /root/app/
volume:
nocopy: true
# - code:/root/workspace/app # TODO: change to your workspace
auth_server:
container_name: auth_server
build:
context: .
dockerfile: Dockerfile.auth_server
ports:
- 1111:1111
volumes:
- type: volume
source: sihv
target: /root/servers/auth
volume:
nocopy: true
# - code:/root/workspace/servers/auth
mongodb:
container_name: mongodb
image: mongo
ports:
- 27018:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=secret
- MONGO_INITDB_DATABASE=db
volumes:
- "C:\\Users\\Public\\db:/data/db" # mount is in the local machine
volumes:
sihv:
external: true
when i start it, it runs fine as
but the sihv in docker dashboard is empty
another
when I try to attach to multiple .devcontainer.jsons stored as in the following file structure
when I open multiple devcontainers seperately on different vscode windows and create a directory say "a", it's reflecting at these multiple locations in same volume
is there a way to resolve both of these issues..? and is there anything wrong with the declaration in docker compose file??
After changing above docker-compose.yml to the one below, it works now
services:
mobile_app:
container_name: mobile_app # optional
build:
context: .
dockerfile: Dockerfile # optional, default is Dockerfile in context
ports:
- 19000:19000
- 19001:19001
- 19002:19002
environment:
- EXPO_DEVTOOLS_LISTEN_ADDRESS=0.0.0.0
- REACT_NATIVE_PACKAGER_HOSTNAME=<????????> # TODO: USE YOUR IPV4 address without any port
- MONGODB_CONNSTRING=mongodb://root:secret#mongodb # (experimental)
volumes:
- type: volume
source: sih_mobile_app
target: /root/app/
volume:
nocopy: true
# - code:/root/workspace/app # TODO: change to your workspace
auth_server:
container_name: auth_server
build:
context: .
dockerfile: Dockerfile.auth_server
ports:
- 1111:1111
volumes:
- type: volume
source: sih_server_auth
target: /root/servers/auth
volume:
nocopy: true
# - code:/root/workspace/servers/auth
mongodb:
container_name: mongodb
image: mongo
ports:
- 27018:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=secret
- MONGO_INITDB_DATABASE=db
volumes:
- sih_mongodbv:/data/db # mount is in the local machine
sih_flask1:
container_name: sih_flask1
build:
context: .
dockerfile: Dockerfile.flask1
ports:
- 8012:8012
volumes:
- sih_flask1:/tf
sih_flask2:
container_name: sih_flask2
build:
context: .
dockerfile: Dockerfile.flask2
ports:
- 8016:8016
volumes:
- sih_flask2:/tf
volumes:
sih_mobile_app:
name: sih_mobile_app
sih_server_auth:
name: sih_server_auth
sih_mongodbv:
name: sih_mongodbv
sih_flask1:
name: sih_flask1
sih_flask2:
name: sih_flask2

docker-compose with volumes - no files in project directory in continers

I am trying to change my docker-compose to use volumes but my /application dir in containers is empty.
When i have config without volumes everything works fine:
volumes:
- .:/application
But when i use
volumes:
- code:/application
volumes:
code:
i get empty /application in containers.
Full docker-compose file:
version: "3.9"
services:
mariadb:
image: mariadb:10.5
container_name: youtube-playlist-mariadb
working_dir: /application
networks:
- backend
volumes:
- /var/lib/mysql/data:/var/lib/mysql
- /var/lib/mysql/logs:/var/log/mysqld.log
- /var/docker/mariadb/conf:/etc/mysql
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=database
- MYSQL_USER=root
- MYSQL_PASSWORD=password
ports:
- "3003:3306"
web:
image: nginx:alpine
container_name: youtube-playlist-web
working_dir: /application
networks:
- frontend
- backend
volumes:
- code:/application
- ./docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "8080:80"
php:
build: docker/php-fpm
container_name: youtube-playlist-php
working_dir: /application
networks:
- frontend
- backend
volumes:
- code:/application
node:
image: node:12.22.1
container_name: youtube-playlist-node
working_dir: /application
networks:
- frontend
- backend
volumes:
- code:/application
networks:
frontend:
backend:
volumes:
code:
x-mutagen:
sync:
defaults:
ignore:
vcs: true
code:
alpha: "."
beta: "volume://code"
mode: "two-way-resolved"
Edit: added mutagen config
Edit:
SOLUTION
I added this config to docker-compose
volumes:
code:
driver: local
driver_opts:
type: none
device: $PWD
o: bind
when you do what you are doing, you are just creating a volume where the content of the /application is going to be stored.
This is different to a bind-mount as you are doing in the first example (the one you say it's working). In this case, you are binding a directory (.) to /application.

Docker does not support storing secrets on Windows home system using Docker toolbox

Using Docker toolbox on Windows 10 Home, Docker version 19.03, we have created a docker-compose.yml and added a secrets file as JSON, it runs fine on a Mac system, but it is unable to run the same in Windows 10 Home.
Error after running docker-compose up:
ERROR: for orthancserver Cannot create container for service orthanc: invalid mount config for type
"bind": invalid mount path: 'C:/Users/ABC/Desktop/Project/orthanc.json' mount path must be absolute
docker-compose.yml:
version: "3.7"
services:
orthanc:
image: jodogne/orthanc-plugins:1.6.1
command: /run/secrets/
container_name: orthancserver
restart: always
ports:
- "4242:4242"
- "8042:8042"
networks:
- mynetwork
volumes:
- /tmp/orthanc-db/:/var/lib/orthanc/db/
secrets:
- orthanc.json
dcserver:
build: ./dc_node_server
depends_on:
- orthanc
container_name: dcserver
restart: always
ports:
- "5001:5001"
networks:
- mynetwork
volumes:
- localdb:/database
volumes:
localdb:
external: true
networks:
mynetwork:
external: true
secrets:
orthanc.json:
file: orthanc.json
orthanc.json file kept next to docker-compose.yml
Found an alternative solution for windows 10 home, with docker toolbox. as commented by #Schwarz54, the file-sharing works well with docker volume for Dockerized Orthanc server.
Add shared folder:
Open Oracle VM manager
Go to setting of default VM
Click Shared Folders
Add C:\ drive to the list
Edit docker-compose.yml to transfer the config file to Orthanc via volume
version: "3.7"
services:
orthanc:
image: jodogne/orthanc-plugins:1.6.1
command: /run/secrets/
container_name: orthancserver
restart: always
ports:
- "4242:4242"
- "8042:8042"
networks:
- mynetwork
volumes:
- /tmp/orthanc-db/:/var/lib/orthanc/db/
- /c/Users/ABCUser/Desktop/Project/orthanc.json:/etc/orthanc/orthanc.json:ro
dcserver:
build: ./dc_node_server
depends_on:
- orthanc
container_name: dcserver
restart: always
ports:
- "5001:5001"
networks:
- mynetwork
volumes:
- localdb:/database
volumes:
localdb:
external: true
networks:
mynetwork:
external: true

docker-compose > v.3 search volume_from analog

i try to migrate from v.2 docker-compose file to v3.3
early i can write somthing like that
nginx:
image: nginx
ports:
- "80:80"
volumes_from:
- php
php:
build:
./docker/php
volumes:
- ./:code
now volume_from dont exist and i must do somthng like that
nginx:
image: nginx
ports:
- "80:80"
volumes:
- ./:/code
php:
build:
./docker/php
volumes:
- ./:/code
i dont like this way, i wish have one volumes for two container, i'm RTFM the docker docs and try do next
version: "3.3"
services:
nginx:
image: nginx
ports:
- "8014:80"
volumes:
- type: volume
source: data
target: /code
php:
build:
./docker/php
volumes:
- type: volume
source: data
target: /code
volumes:
data:
But, of couse i dont have any files in data how i can put's all my files from ./ to data ?

How to replace volumes_from in docker-composer v3

I want to know the equivalent of the configuration below to suit version 3 of docker-composer.yml! volumes_from is no longer valid so am I supposed to skip the data volume and replace it with top level volumes ?
version: '2'
services:
php:
build: ./docker-files/php-fpm/.
volumes_from:
- data
working_dir: /code
links:
- mysql
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
volumes_from:
- data
links:
- php
data:
image: tianon/true
volumes:
- .:/code
By default named volumes allow you to share data between containers. But it is some troubles with storing data in the same place on the host machine after restarting containers. But we can use local-persist docker plugin for fix it.
For migration to version 3 you need
1) install local-persist docker plugin (if you want to store volumes data to the particular place on the host machine)
2) modify docker-compose.yml
version: '3'
services:
php:
build: ./docker-files/php-fpm/.
volumes:
- data:/code
working_dir: /code
links:
- mysql
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
volumes:
- data:/code
links:
- php
data:
image: tianon/true
volumes:
- data:/code
# If you use local persist plugin
volumes:
data:
driver: local-persist
driver_opts:
mountpoint: /path/on/host/machine/
# Or If you dont want using local persist plugin
volumes:
data:
Also you can store volumes data to the host machine with this volumes section:
volumes:
data:
external: true #< it means store my data to the host machine
But you can't specify path for this volume on host machine

Resources