Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The data volume causes the container directory to be overwritten. #4581

Closed
wsad137 opened this issue Mar 7, 2017 · 14 comments
Closed

The data volume causes the container directory to be overwritten. #4581

wsad137 opened this issue Mar 7, 2017 · 14 comments

Comments

@wsad137
Copy link

wsad137 commented Mar 7, 2017

version: '3'
services:    
  tomcat7jar8:
        container_name: tomcat7-jar8
        image: tomcat:8.5
        ports:
            - 8080
        volumes:
            - ./tomcat/webapps:/usr/local/tomcat/webapps
            - ./tomcat/logs:/usr/local/tomcat/logs

Adding the first line causes the file in the container to be removed.
Log file synchronization is normal.
Commented out webapps tomcat is normal.
This question is very strange, I do not know how to solve.
Or other ways to solve.

@shin-
Copy link

shin- commented Mar 7, 2017

If you mount your local directory into /usr/local/tomcat/webapps in the container, it will replace what was there originally. That's the whole point.

What are you trying to do?

@wsad137
Copy link
Author

wsad137 commented Mar 8, 2017

I want to synchronize the items in the container webapps to the host.
As logs, logs in the log file, synchronized over, is normal.

@wsad137
Copy link
Author

wsad137 commented Mar 8, 2017

Do not know how to use the data volume.
@shin-

@shin-
Copy link

shin- commented Mar 8, 2017

Are you saying the contents of ./tomcat/webapps are not available inside the container?

@wsad137
Copy link
Author

wsad137 commented Mar 8, 2017

./tomcat/webapps folder is normal, but inside the file. Not synchronized.
@shin-

@wsad137
Copy link
Author

wsad137 commented Mar 8, 2017

/ Usr / local / tomcat / webapps inside the file is not synchronized to ./tomcat/webapps.
Container webapps in the original file, also disappeared, leaving an empty directory.
The webapps in the container can not be synchronized.
But logs can be synchronized.

@wsad137
Copy link
Author

wsad137 commented Mar 8, 2017

The log directory in the container
image
The webapps directory in the container
image

Comment out webapps data volumes
volumes:
-# - ./tomcat/webapps:/usr/local/tomcat/webapps
- ./tomcat/logs:/usr/local/tomcat/logs
The file in the container webapps appeared again.
image

@nicodmf
Copy link

nicodmf commented Aug 9, 2017

This is the normal behavior :) If you mount a local directory on a container directory, the container directory is hidden and replaced by your local.

The logs files will written after the start of a container, in your local directory. If you comment the second line withg your logs, the container write always in the same place for it but neither in your local directory.

@mikeyjk
Copy link

mikeyjk commented May 16, 2018

I think what @nicodmf is saying, is (correct me if I'm wrong, obv.):

  • ./tomcat/webapps:/usr/local/tomcat/webapps
  • ./tomcat/logs:/usr/local/tomcat/logs

If host './tomcat/webapps' is not empty, it will overwrite container files, and be shared.
If host './tomcat/logs' is empty, the containers files will be copied into the host, and be shared.

It's /meant/ to depend on the host folder being empty. I'm specifically having an issue where the folder is empty, but isn't getting the container files copied to it.

(^ I was conflating 'named volumes' with 'mount binds')

--

I've come full circle and have realised I was expecting behaviour from Docker volumes that didn't exist at the time. This is the behaviour I wanted, which was missing:

  • Volumes
  • Bind mounts
    • Sync code between host filesystem and container filesystem
      • Pass
    • The container codebase is copied into the host codebase (with empty host codebase)
      • Fail
        • Using a bash script inside the container to do the copy for me, would prefer to avoid this though.
    • The host codebase is copied into the container codebase (with populated host codebase)
      • Pass
    • The host location can be specified / host has permission to modify
      • Pass

For now I'm using bind mounts, with a script available inside the container that will then sync the code between host and container - ideally I'd be able to deprecate this bash script!

Now using a named volume, that is accessible via the host.
No idea how I'm going to scale this to 'n' containers with 'n' different shared volumes yet, but at least I've gotten it working for where n=1 xD!

I would be interested to see metrics comparing a bash script to copy from container -> host, vs named volumes, I swear it is slightly slower using named volumes...

@dantebarba
Copy link

@mikeyjk hey is there any solution to this or ticket to follow?. I was uploading some images for my work and I realized that all the readmes and tutorials I was doing were wrong because the spec mentioned in the docs from Docker is not accurate. When the host codebase is empty, the container doesn't copy anything.

@mikeyjk
Copy link

mikeyjk commented Oct 1, 2018

@dantebarba There are a /few/ tickets that are kind of related to this, but my issue was more of a comprehension problem understanding the behaviour of bind mounts vs named volumes. What I wanted was a named volume, but I thought I wanted a bind volume.

It is possible the Docker docs have gotten this wrong or worded it strangely, can you link me / copy the relevant docs? Docker is still 'relatively' new, so there are still things to be ironed out. So damn useful though.

@ijc
Copy link

ijc commented May 22, 2019

Seems like this was some confusion over the behaviour of volume vs binds which mikeyjk has explained. I'm going to close, if there is still confusion please do reopen.

@ijc ijc closed this as completed May 22, 2019
@kitanog
Copy link

kitanog commented Feb 2, 2024

I know this was closed a while ago, but I'm running into an issue where my named volume is overwriting my docker file path, even when the host volume does not exist.
This is my docker compose:

services:
  librespeed:
    image: lscr.io/linuxserver/librespeed:latest
    container_name: librespeed_netinsights
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Etc/UTC
      - PASSWORD=PASSWORD
    volumes:
      -/docker_configs/librespeed-vol:/app
    ports:
      - 801:80
    restart: unless-stopped
volumes:
  librespeed-vol:

In this case it is causing the /app folder in my docker container to be empty, because it created an empty librespeed-vol folder on my local host.

From my understanding of how volumes work (and how it worked initially for me) docker compose should create the volume (librespeed-vol) and sync the files from the container (/app) to the volume path on the initial creation.

Not sure what I'm doing wrong here, but hoping it's a simple fix.

@dantebarba
Copy link

@kitanog I don't think docker is supposed to do that. In your yaml you are binding the directory /docker_configs/librespeed-vol from your host and you are also creating a volume called librespeed-vol which is empty and not binded. I don't think docker-compose nor docker will understand that you want to copy the directory contents into a new volume. Feel free to comment and quote the docs if you need so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants