Docker volume replace previous volume - docker

I have docker-compose file, where I have specified volumes:
services:
portal:
env_file:
- ${ENV_FILE}
build:
context: .
dockerfile: ./Dockerfile
container_name: sensus-portal
image: sensus-portal
hostname: ${DOMAIN}
working_dir: /var/www/html
volumes:
- ../localhost-certificates:/var/certificates
- ./portal:/var/www/html
- ../wp-content:/var/www/html/wp-content
ports:
- "80:80"
- "443:443"
Problem is that when last volume added, it replaces previous stuffs which was in var/www/html/wp-content ... I want to assign volume /var/www/html and then into this add (merge) volume /var/www/html/wp-content.
Is there any chance how to do this?

No, there’s no way to merge the contents of two volumes or host directories. This works the same way as normal Linux mount(8): the directory or volume you’re mounting into the container hides whatever was there already, and there’s no way to get at the hidden content.
The Dockerfile COPY directive is a little more flexible this way and you might consider building this content into your image.

Related

What does volumes do in docker-compose

I am currently learning Docker. I am stucked at the idea of volumes. I assume that they made to store the data whenever we restart the container etc., but i do not understand what happens if we don't provide the ":" for the source:target.
Example:
- "/usr/src/my-app/frontend/node_modules"
- "/usr/src/my-app/backend/node_modules"
What do we store inside the container if we use volumes like above?
The whole docker-compose
version: '3'
services:
nginx:
image: nginx
container_name: nginx
ports:
- 80:80
restart: always
volumes:
- "./nginx/default.conf:/etc/nginx/conf.d/default.conf"
backend:
build:
dockerfile: Dockerfile.dev
context: ./backend
container_name: backend
volumes:
- "/usr/src/my-app/backend/node_modules"
- "./backend:/usr/src/my-app/backend"
frontend:
build:
dockerfile: Dockerfile.dev
context: ./frontend
container_name: frontend
environment:
CHOKIDAR_USEPOLLING: "true"
volumes:
- "/usr/src/my-app/frontend/node_modules"
- "./frontend:/usr/src/my-app/frontend"
It is an anonymous volume. It is managed by docker like a named volume, but it doesn't have a real name, only a GUID. Likewise, it's similar to the one you get when you use the VOLUME instruction in your Dockerfile without mounting a named volume or bind mount to that path when running the container.
See this for example (emphasis mine):
-v or --volume: Consists of three fields, separated by colon characters (:). The fields must be in the correct order, and the meaning of each field is not immediately obvious.
In the case of named volumes, the first field is the name of the volume, and is unique on a given host machine. For anonymous volumes, the first field is omitted.
https://docs.docker.com/storage/volumes/#choose-the--v-or---mount-flag
As long as you keep the container and only restart it, the same volume will be used. If you delete the container / create a new container, it will use a new volume.

Docker: How to change mountpoint of volumes?

I have a web application running in container.
The application allows users to upload files. These files are stored in Docker volumes web_data1 and web_data2.
Due to changes in the application, I need to change the mountpoint of these volumes i.e.
the data that were in /srv/app/web_data1_mountpoint, now need to be moved to /srv/app/web_data1_changed_mountpoint.
What is the proper way to do this?
docker-compose.yml
version: "3"
volumes:
web_data1:
web_data2:
services:
web:
build:
context: .
dockerfile: .docker/Dockerfile
image: web-image
ports:
- 80:80
- 443:443
volumes:
- web_data1:/srv/app/web_data1_mountpoint
- web_data2:/srv/app/web_data2_mountpoint
That depends a bit of the image you are using. Just changing the volume would work in your docker-compose.yml like this:
volumes:
- web_data1:/srv/app/web_data1_changed_mountpoint
- web_data2:/srv/app/web_data2_changed_mountpoint
But I dont know, what your image does with the directory. Maybe something inside the image depends on the directory.

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 volumes_from does not work as expected

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
...

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