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
docker-compose copy file or directory to container #5523
Comments
What's the usecase? Most of the suggested usage I've seen were antipatterns. |
You can see some of many usecases clicking at link provided. As you can see many of subscribers consider it as really useful feature instead of "antipattern" |
ooops, now I see "something" happened to issue #2105 as there are no comments at all anymore... |
so, I find really useful to copy some configuration/initialization files to container. As example some *.sql stuff for db containers, some html/js/css content for apache/nginx containers or even jar file for java container. This will make it available/runnable "globally" not only on machine where it was composed as in case of mounting volume(s). Mainly this will be some combination of host-local and container-contained files. In fact any container can be considered useless without any configuration or initialization this is correct link: #1664 |
+1 |
The problem with this is that it is incredibly short-sighted (hence the term "anti-pattern"), as it will force you to repeat the operation every time the containers to be recreated. Not to mention the fact that it scales very poorly (what if you have 10 containers? 20? 100?) The actual solution to your issue is to include those necessary files in your build (Dockerfile) and rebuild when an update is needed. |
of course, if it is composed including all "shared" content into container, scaling 10-20-100- containers would be much easier. Everything you need is to pull it from repository and mount(yes, in this case mount) only node-specific config. And even more, you don't need run docker-compose on each node. |
I'm running into an issue where copy would come in handy (at least as an override). I mostly develop on mac so I almost never see an issue with commands running as root in the container and exporting to a mounted volume. However, recently using the same workflow on a CentOs has caused some major pain because files owned by the root user are being added to the host via the mounted volume. I would like in these cases to just be able to copy the host files to the container instead of mounting them. The related issue: #1532 updateI think in my case I can get away with using COPY in the Dockerfile and having multiple docker-compose files one of which uses a volume mount. |
Use-case: I can't use rw volume, because filesystem is read only. It would be awesome to make writes that are persists only when container runs. I can make wrapper (https://stackoverflow.com/questions/36362233/can-a-dockerfile-extend-another-one) to only |
Use case: starting multiple docker containers simultaneously from .gitlab-ci.yml which need to write into the git repository's directory. If the process inside a container fails or if the ci job is cancelled before the container has cleaned up after itself, the remaining files can't be deleted by gitlab-runner due to lack of permissions. Now I could copy the files within the container out of the volume into another directory, but that would be an antipattern, wouldn't it? |
Is this different from |
@harpratap you are right, but the drawback is that /folder_in_container must not exist or must be empty or else it will be overwritten. If you have a bash script as your entry point, you could circumvent this by symlinking your files into the originally intended directory after you create a volume at /some_empty_location +1 for having a COPY functionality. Our use case is for rapidly standing up local development environments and copying in configs for the dev settings. |
+1 for COPY. This would really be a helpful feature. Use case: in swarm mode, I have a service using mysql image. I need to copy my initialization scripst in /docker-entrypoint-initdb.d/ so that MySQL can execute them. Though it is possible to create an image on top of mysql, copy the files and use it or connect to the mysql |
+1 for COPY/ADD, Use Case: |
This comment has been minimized.
This comment has been minimized.
Suppose one has a shared config file across a number of docker machines, with their Dockerfiles in respective subdirectories under the docker-compose directory. How do you copy that shared config into each image? I can't symbolically link to In this instance when running docker-compose build, I'd like to copy the config files from the docker-compose context prior to running the docker build steps. I'm happy if someone can suggest a clean workaround of course. |
This comment has been minimized.
This comment has been minimized.
Please don't comment with just +1 - it's a waste of everyone's time. If you have additional information to provide, please do so ; otherwise, just add a thumbs up to the original issue. |
What is the use of dogmatically insisting it is antipattern, just because in some cases it could eventually cause problems? This definitely has a good use as you could add one line to an existing file, instead of having to create an extra folder and file, then move the file to be added there. This pointless, bureaucratic creation of tiny files is the real antipattern, preventing users from creating simple and easy to maintain docker-compose files. If users want to do harmful things with Docker, they will find a way no matter what you do. Refusing to add legitimate features just because someone may misuse them one day is foolish. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I created a quick gist for this. It assumes the docker compose service is named |
This comment has been minimized.
This comment has been minimized.
COPY/ADD would definitely be a welcome feature. A usecase: running a Graylog instance in Docker for Dev purposes. In order to launch an input automatically, a JSON spec has to be put in /usr/share/graylog/data/contentpacks In order to get it working now (on Oct 16, 2018), need to mount a volume to that point AND copying the original content of that folder to the persistent volume. Which is quiet inconvenient. |
I would benefit from that, i have a set of tools that import a database seed into a container and then i run the devtools database importer based on that file. I don't want to have to do: docker cp "${seed_file}" $(docker-compose ps -q devtools):/tmp/seed_file to be able to import my seed. And no, i will not compile my dev images with a fixed schema, this goes against web development pattern at the very least. Containers should be for app portability, not data. It would make way more sense to do: docker-compose cp "${seed_file}" devtools:/tmp/seed_file All in all, it is just a short-hand that basically does the same thing, but it looks better to leverage |
|
@funkyfuture If you think that these use-cases follow an antipattern, then please suggest a solution that does not. |
What about k8s-like "data section" ? services:
service1:
image: image.name
data:
filename1.ini: |
[foo]
var1=val1
[bar]
var2=val2
filename2.yml: |
foo:
bar: val1 or perhaps the same but for the volumes:
service_config:
data:
filename1.ini: |
[foo]
var1=val1
[bar]
var2=val2
services:
service1:
image: image.name
volumes:
- service_config:/service/config |
Any update on this? I currently have a project which would benefit from this implemented as i have to use an ugly workaround to get directories from one container to another. |
Use case:
This then will be part of an admin script that helps moving backups between compose deployments. Such script then would look very clean. Mainly used during development and evaluation. |
I still wonder, why this should be an antipattern.
Just like the wait-for-it.sh script is fine, but having that possibility built into docker-compose would still be a great improvement in expressivness. |
Can add copy! |
Closing this as implemented in Compose v2 |
Yay! Any example of how the syntax would look like? |
same as
|
Nice! Will there also be a way to define this in the Docker Compose file? |
compose officially an anti-pattern enabler, thank god. |
@robclancy the anti-pattern is the way some might use this command. We introduced this command as |
@ndeloof in I'm hoping the latter is true, and that this can be configured inside the docker-compose.yaml file? And what version of docker-compose is this supported under? |
@evbo Also, this is command line only, there's no equivalent in compose file. Declaring a volume bind sounds a reasonable alternative if you want this set in your yaml configuration. this is implemented in Docker Compose v2, not docker-compose |
ok, thanks for clarifying @ndeloof If I understand correctly, the main benefit is that What if I have a |
this is indeed "just" |
One use case for me is to put the file custom.cnf into /etc/mysql/conf.d inside mysql container. Volume is not an answer to that but file copy would be more appropriate. So you expect me to create a Dockerfile just copy that one file? |
Common Scenario: 1 or more files that live in non-empty directories need to be changed on-the-fly. You can't use volume with these directories as you'd lose all the other files in the directory. You most definitely do not want to copy the entire directory down to work around this. A suggestion of rebuilding the image every time you make a change completely ignores the real world, time, and the rage induced because you missed a comma. A basic copy command isn't an anti-pattern by any stretch.
A quick implementation of this might be an underlying volume that takes these files and creates a symlink to their respective destinations. |
I just want to add to what @jadon1979 said, that any and all workarounds to the copy conundrum suffer in one of many ways:
There was recently a PR merged for docker-compose v2 that performs a docker |
why not add this to docker-compose and enable for use in docker-compose.yml configs as well? |
I solved it. Gave up on Docker entirely and am using LXC. Solved everything for my use case AND no more people insulting me for "antipatterns". Win-win. |
It is disingenous to write that the feature was implemented, because it was not. The feature that was asked for have not been not implemented, and instead the feature that nobody has asked for and which was already fully covered by underlying docker was added. The central issue of this feature request is that one does not own most of the images they have in their Docker Compose file, and when they bring up an image like |
This ends the nearly 6 year long holy war. The reasonable among us were just asking for a way to put configs in our containers without volume mounts and without rolling new images. Apparently it's available in docker compose 2.23.1. I would follow that thread if you are still seriously interested in this feature. |
@washtubs while inlined config indeed don't rely on bind mount, this is not the reason this feature was introduced. We could support file-based configs without a bind mount, just this would have some side effects on existing users. @alamar the example you mention suggest we should investigate lifecycle hooks. A pre-start hook to |
@ndeloof I haven't used docker compose in some time. I got renewed interest in it just now after realizing it was rewritten in go (yeah it's been a while), and seeing the output of So forgive me for my lack of ecosystem knowledge here. I assume those who are used to helm and kubernetes would like to have some kind of templating pre-processor engine on top of docker compose and use that to insert file contents inline. I'm not sure if compose has a "helm"-like sister software like kubernetes does, or perhaps even builtin support. But that's an approach that would be familiar to me which I would leverage to insert config files inline as a pre-processing step. |
@washtubs docker compose do support variable interpolation to offer some flexibility, but we don't want this to become a templating system like the one helm uses. Obviously someone could built another tool to produce compose.yaml from another source. |
That doesn't seem quite the same. I think that is basically coalescing multiple yaml files together into one, another thing that helm does. But helm uses go's template engine which gives a ton of flexibility on the kinds of things you can insert, and manipulations you can do, like quoting. A common pattern is inserting the contents of a file inline with indent. You can even define your own custom template functions to be reused throughout your project. Honestly you could probably use helm to do this for compose, just make a helm chart alongside your docker-compose file, and you can use the helm template command to get the output docker-compose file with your file contents rendered and script the deployment yourself. (won't be able to use helm install or upgrade obviously, since those are k8 specific) |
Another usecase: I have a compose file on a local machine and my dockerhost ist a remote machine. How do I reference a file to be mounted from my local directory? Answer: not at all without building a specific image! A solution would be to copy it into the container. |
we miss a possibility to copy a file or directory using docker-compose. I find this really useful.
Please check many +1 in premature closed #2105
The text was updated successfully, but these errors were encountered: