Docker volumes_from does not work as expected - docker

Hello Guys I am facing a problem in volumes_from in docker-compose file.
- i have 3 services first one has my app files and the second is php-fpm which take volume from the data service.
my file is like this.
version: '2'
services:
cms_data:
image: ""image from private repository contain application file"
container_name: "cms-data"
php-fpm:
image: "image from private repository contain php configuration"
container_name: "php-fpm"
env_file:
- ../.env.production
volumes_from:
- cms_data
working_dir: /iprice/octobercms
expose:
- 9000
depends_on:
- cms_data
restart: "always"
nginx:
image: "image from private repository contain nginx configuration"
container_name: "nginx"
ports:
- "80:80"
- "443:443"
links:
- php-fpm
volumes_from:
- cms_data
depends_on:
- cms_data
restart: "always"
the cms-data image has the files which is correct.
but the php-fpm container doesn't please help.

volumes_from mounts the volumes present on other containers. It does not create new volumes.
The cms-data container does not have any volumes associated with it. So volumes_from cant do anything. If you want to share a particular folder inside cms-data, first create a volume linking that folder.
NOTE: creating a volume will overwrite the contents of the internal container with the /path/on/host folder. So first copy the contents of the container folder to this host folder.
Run the current docker-compose as is so the containers start.
Copy the contents from the cms-data container to the host folder:
docker cp :/path/to/shared/folder /path/on/host
Make the following changes to the docker-compose file and restart.
services:
cms_data:
image: ""image from private repository contain application file"
container_name: "cms-data"
volumes:
- /path/on/host:/path/to/shared/folder
...

Related

Docker shared volume is not readable for a container after changing volume contents

I have got following compose file where i'm sharing some generated html data from Jenkins container to the host drive and reading this data by Nginx container from the host drive. I'm using Ubuntu Server 18.04 on AWS.
The problem is that I can read contents of the jenkins/workspace/allure-report only once. After updating of the html data it becomes inaccessible for Nginx and it throws 403 status code.
I tried all the possible solutions but nothing works. The only ugly solution is to restart Nginx container after every html data updating. I don't like this way and looking for some inbuilt docker features to resolve this.
What didn't help: sharing volume straight between containers without using docker host drive, using rslave option, using docker separate volume that can be used as buffer between the two containers... I believe it should be much more easier!
version: '2'
services:
jenkins:
container_name: jenkins
image: "jenkins/jenkins"
ports:
- "8088:8080"
- "50000:50000"
env_file:
- variables.env
volumes:
- ./jenkins:/var/jenkins_home
selenoid:
container_name: selenoid
network_mode: bridge
image: "aerokube/selenoid"
# default directory for browsers.json is /etc/selenoid/
command: -listen :4444 -conf /etc/selenoid/browsers.json -video-output-dir /opt/selenoid/video/ -timeout 3m
ports:
- "4444:4444"
env_file:
- variables.env
volumes:
- $PWD:/etc/selenoid/ # assumed current dir contains browsers.json
- /var/run/docker.sock:/var/run/docker.sock
selenoid-ui:
container_name: selenoid-ui
network_mode: bridge
image: "aerokube/selenoid-ui"
links:
- selenoid
ports:
- "8080:8080"
env_file:
- variables.env
command: ["--selenoid-uri", "http://selenoid:4444"]
nginx:
container_name: nginx
image: "nginx"
ports:
- "80:80"
volumes:
- ./jenkins/workspace/allure-report:/usr/share/nginx/html:ro,rslave
Found the solution: the easiest way to get access to the dynamic data is to use volumes_from in that container you want to look from.
When I configured my compose file like that I faced another issue - the 403 status has gone but the data was static. But that was my fault, I didn't use "cp -r " command correctly so my data has been copied only once.

Why aren't my docker images, built by "docker-compose build", using the correct version of my code?

Docker doesn't use the latest code after running git checkout <non_master_branch>, while I can see it in the vscode.
I am using the following docker-compose file:
version: '2'
volumes:
pgdata:
backend_app:
services:
nginx:
container_name: nginx-angular-dev
image: nginx-angular-dev
build:
context: ./frontend
dockerfile: /.docker/nginx.dockerfile
ports:
- "80:80"
- "443:443"
depends_on:
- web
web:
container_name: django-app-dev
image: django-app-dev
build:
context: ./backend
dockerfile: /django.dockerfile
command: ["./wait-for-postgres.sh", "db", "./django-entrypoint.sh"]
volumes:
- backend_app:/code
ports:
- "8000:8000"
depends_on:
- db
env_file: .env
environment:
FRONTEND_BASE_URL: http://192.168.99.100/
BACKEND_BASE_URL: http://192.168.99.100/api/
MODE_ENV: DOCKER_DEV
db:
container_name: django-db
image: postgres:10
env_file: .env
volumes:
- pgdata:/var/lib/postgresql/data
I have tried docker-compose build --no-cache, followed by docker-compose up --force-recreate but it didn't solve the problem.
What is the root of my problem?
Your volumes: are causing problems. Docker volumes aren't intended to hold code, and you should delete the volume declarations that mention backend_app:.
Your docker-compose.yml file says in part:
volumes:
backend_app:
services:
web:
volumes:
- backend_app:/code
backend_app is a named volume: it keeps data that must be persisted across container runs. If the volume doesn't exist yet the first time then data will be copied into it from the image, but after that, Docker considers it to contain critical user data that must not be updated.
If you keep code or libraries in a Docker volume, Docker will never update it, even if the underlying image changes. This is a common problem in JavaScript applications that mount an anonymous volume on their node_modules directory.
As a temporary workaround, if you docker-compose down -v, it will delete all of the volumes, including the one with your code in it, and the next time you start it will get recreated from the image.
The best solution is to simply not use a volume here at all. Delete the lines above from your docker-compose.yml file. Develop and test your application in a non-Docker environment, and when you're ready to do integration testing, run docker-compose up --build. Your code will live in the image, and an ordinary docker build will produce a new image with new code.

Docker: Write to disk of linked container

I have a Docker container that runs a simple web application. That container is linked to two other containers by Docker Compose with the following docker-compose.yml file:
version: '2'
services:
mongo_service:
image: mongo
command: mongod
ports:
- '27017:27017'
tomcat_service:
image: 'bitnami/tomcat:latest'
ports:
- '8080:8080'
web:
# gain access to linked containers
links:
- mongo_service
- tomcat_service
# explicitly declare service dependencies
depends_on:
- mongo_service
- tomcat_service
# set environment variables
environment:
PYTHONUNBUFFERED: 'true'
# use the image from the Dockerfile in the cwd
build: .
ports:
- '8000:8000'
Once the web container starts, I want to write some content to /bitnami/tomcat/data/ on the tomcat_service container. I tried just writing to that disk location from within the web container but am getting an exception:
No such file or directory: '/bitnami/tomcat/data/'
Does anyone know what I can do to be able to write to the tomcat_service container from the web container? I'd be very grateful for any advice others can offer on this question!
you have to use docker volumes if you want one service to write to other service. If web writes to someFolderName the same file will exist in the tomcat_service.
version: '2'
services:
tomcat_service:
image: 'bitnami/tomcat:latest'
volumes:
- my_shared_data:/bitnami/tomcat/data/
web:
volumes:
- my_shared_data:/someFolderName
volumes:
my_shared_data:
Data in volumes persist and they will be available even next time you re-create docker containers. You should always use docker volumes when writing some data in docker containers.

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

Confused about volumes_from and volumes usage in docker-compose.yml

I am trying to understand - maybe I already did maybe not - the differences between volumes_from and volumes usage in a docker-compose.yml file. I have read docs already but from there is not so clear to me so I am doing a real exercise.
I have the following setup:
a root directory
a directory named php-apache with a Dockerfile under root
a directory named mongo with a Dockerfile under root
a docker-compose.yml file under root
Note: If it's not clear to you, take a look here and everything exposed down below is right there as well (mongodb-test branch)
At php-apache/Dockerfile I have the following entry:
VOLUME /data /data
At mongo/Dockerfile I have the following entry:
VOLUME /data/db /data/configdb
At docker-compose.yml I have the following:
version: '2'
services:
php-apache:
container_name: "php55-dev"
image: reynierpm/php55-dev
ports:
- "80:80"
environment:
PHP_ERROR_REPORTING: 'E_ALL & ~E_DEPRECATED & ~E_NOTICE'
volumes:
- ~/mmi:/var/www
volumes_from:
- volumes_data
mongo:
container_name: "mongodb"
image: reynierpm/mongodb
ports:
- "27017:27017"
volumes_from:
- volumes_data
volumes_data:
image: tianon/true
volumes:
- ~/data/mongo:/data/db
- ~/data:/data
This is what I am understanding from that setup:
The image reynierpm/php55-dev will expose a /data directory and this will be mapped to ~data:/data in tianon/true image
The image reynierpm/mongodb will expose a /data/db to the outside and mapped to /data/configdb internally then the /data/db is mapped to ~/data/mongo:/data/db in tianon/true image.
Is a mess in my head right now because what do I want to achieve is the following:
Keep mapped the code on the host to the container (this line <path_on_host>:/var/www on docker-compose.yml)
Keep data stored on a local directory in the host
So, it's fine what I am doing? Feel free to made any modification at this setup since I am still learning.
The image reynierpm/php55-dev will expose a /data directory and this will be mapped to ~data:/data in tianon/true image
It's better to say it will mapped to your ~/data on docker host. Please note that there will be a /data/db from the second volume too.
The image reynierpm/mongodb will expose a /data/db to the outside and mapped to /data/configdb internally then the /data/db is mapped to ~/data/mongo:/data/db in tianon/true image.
This container will be the same as php-apache in terms of volumes from
volume_data container.
In case of your objectives:
If your code is in ~/mni/ you are fine. You are mounting mongoDB database directory to php-apache container, I don't think you need that.
You need to create a user defined network for your container connectivity or link containers (legacy). To create user defined network:
docker network create --driver bridge <yournetwork name>
You don't need a DOC. Thats why I removed the third container. I also fixed the unnecessary volume mappings.
Updated Docker file:
version: '2'
services:
php-apache:
container_name: "php55-dev"
image: reynierpm/php55-dev
ports:
- "80:80"
environment:
PHP_ERROR_REPORTING: 'E_ALL & ~E_DEPRECATED & ~E_NOTICE'
volumes:
- ~/mmi:/var/www
volumes_from:
- volumes_data
mongo:
container_name: "mongodb"
image: reynierpm/mongodb
ports:
- "27017:27017"
volumes_from:
- volumes_data
volumes_data:
image: tianon/true
volumes:
- ~/data/mongo:/data/db
- ~/data:/data
networks:
default:
external:
name: <your network name>
Please note you have to call your mongo container from your web application by its name, in your case mongodb.

Resources