使用docker-compose将文件添加到标准映像 [英] Adding files to standard images using docker-compose
问题描述
我不确定是否有明显的东西逃脱了我,或者这是不可能的,但是我试图用docker hub中的图像组成整个应用程序堆栈。
其中之一是mysql,它支持通过卷添加自定义配置文件并从已安装的目录运行.sql文件。
但是,我将这些文件存储在运行docker-compose的计算机上,而不是主机上。在运行入口点/ cmd之前,没有办法指定本地计算机上要复制到容器中的文件吗?
如果您不能使用卷(想要无状态docker),是否真的需要为所有内容创建本地映像?
-compose.yml并使用远程计算机),就可以用命令编写配置文件。
nginx配置示例,在官方映像中:
版本: 3.7
服务:
nginx:
图片:nginx:高山
端口:
-80:80
环境:
NGINX_CONFIG:
服务器{
server_name〜^ www\。(。*)$$;
返回301 $$ scheme:// $$ 1 $$ request_uri;
}
服务器{
server_name example.com
...
}
命令:
/ bin / sh -c echo \ \ $$ NGINX_CONFIG\> /etc/nginx/conf.d/redir.conf; nginx -g \ daemon off; \
环境变量也可以保存在.env文件中,您可以使用Compose的扩展功能或从shell环境中加载(在其他地方从eny那里获取):
https:// docs。 docker.com/compose/compose-file/#env_file
https://docs.docker.com/compose/compose-file/#variable-substitution
获取a的原始入口点命令容器:
docker container inspect [container] | jq --raw-output。[0] .Config.Cmd
调查修改该文件的文件通常可以正常工作:
docker exec --interactive --tty [container] sh
I'm unsure if something obvious escapes me or if it's just not possible but I'm trying to compose an entire application stack with images from docker hub.
One of them is mysql and it supports adding custom configuration files through volumes and to run .sql-files from a mounted directory.
But, I have these files on the machine where I'm running docker-compose, not on the host. Is there no way to specify files from the local machine to copy into the container before it runs it entrypoint/cmd? Do I really have to create local images of everything just for this case?
If you can not use volumes (wants stateless docker-compose.yml and using remote machine), you can have config file written by command.
Example for nginx config in official image:
version: "3.7"
services:
nginx:
image: nginx:alpine
ports:
- 80:80
environment:
NGINX_CONFIG: |
server {
server_name "~^www\.(.*)$$" ;
return 301 $$scheme://$$1$$request_uri ;
}
server {
server_name example.com
...
}
command:
/bin/sh -c "echo \"$$NGINX_CONFIG\" > /etc/nginx/conf.d/redir.conf; nginx -g \"daemon off;\""
Environment variable could also be saved in .env file, you can use Compose's extend feature or load it from shell environment (where you fetched it from enywhere else):
https://docs.docker.com/compose/compose-file/#env_file https://docs.docker.com/compose/compose-file/#variable-substitution
To get the original entrypoint command of a container:
docker container inspect [container] | jq --raw-output .[0].Config.Cmd
To investigate which file to modify this usually will work:
docker exec --interactive --tty [container] sh
这篇关于使用docker-compose将文件添加到标准映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!