我正在使用docker-compose(以前的无花果)在docker中运行一个简单的rails应用程序:
搬运工-compose.yml
db:
image: postgres
volumes:
- pgdata:/var/lib/postgresql/data
web:
build: .
command: bundle exec rails s -b 0.0.0.0
volumes:
- .:/usr/src/app
ports:
- "3011:3000"
links:
- db
Dockerfile
FROM rails:onbuild
我需要运行一些定期维护脚本,例如数据库备份,将站点地图ping到搜索引擎等。
我不想在我的主机上使用cron,因为我更喜欢保持应用程序的可移植性,我的想法是使用docker-compose使用docker-compose链接https://registry.hub.docker.com/u/hamiltont/docker-cron/等图像。 / p>
rails官方映像没有启用ssh所以我不能只让cron容器进入Web容器并运行脚本。
docker-compose是否有办法让容器获取shell进入链接容器以执行某些命令?
答案 0 :(得分:1)
What actually would you like to do with your containers? If you need to access some objects from container's file system, you should just mount the volume to the ancillary container (consider --volumes-from
option).
Any SSH interaction between containers is considered as a bad practice (at least since docker 1.3, when docker exec
has been implemented). Running more than one process inside the container (e.g. smth but the postgres
or rails
in your case) will result in a large overhead: in order to have a sshd
along with rails
you'll have to deploy something like supervisord
.
But if you really need to provide some kind of nonstandard interaction between the containers and you're sure that you really need it, I would suggest you to use one of the full-featured docker client libraries (like docker-py
). It will allow you to launch docker exec
in a programmable way.