部署 docker-compose 容器 [英] Deploying docker-compose containers
问题描述
我正在尝试部署一个使用 docker-compose 构建的应用程序,但感觉我完全走错了方向.
I'm trying to deploy an app that's built with docker-compose, but it feels like I'm going in completely the wrong direction.
- 我的一切都在本地运行——
docker-compose up
使用适当的网络和主机启动我的应用程序. - 我希望能够在生产机器上运行相同配置的容器和网络,只需使用不同的
.env
文件.
- I have everything working locally—
docker-compose up
brings up my app with the appropriate networks and hosts in place. - I want to be able to run the same configuration of containers and networks on a production machine, just using a different
.env
file.
我当前的工作流程如下所示:
My current workflow looks something like this:
docker save [web image] [db image] > containers.tar
zip deploy.zip containers.tar docker-compose.yml
rsync deploy.zip user@server
ssh user@server
unzip deploy.zip ./
docker load -i containers.tar
docker-compose up
此时,我希望能够在他们到达那里时再次运行 docker-compose up
,但这会尝试按照 docker-compose.yml 重建容器
文件.
At this point, I was hoping to be able to run docker-compose up
again when they get there, but that tries to rebuild the containers as per the docker-compose.yml
file.
我有一种明显的感觉,我错过了一些东西.我应该先发送完整的应用程序,然后在服务器上构建图像吗?如果您要从注册表存储/加载图像,您将如何启动组合容器?
I'm getting the distinct feeling that I'm missing something. Should I be shipping over my full application then building the images at the server instead? How would you start composed containers if you were storing/loading the images from a registry?
推荐答案
问题是我在开发和生产中使用了同一个 docker-compose.yml 文件.
The problem was that I was using the same docker-compose.yml file in development and production.
应用程序服务没有指定存储库名称或标签,所以当我在服务器上运行 docker-compose up
时,它只是尝试在我的应用程序的源代码目录中构建 Dockerfile(其中服务器上不存在).
The app service didn't specify a repository name or tag, so when I ran docker-compose up
on the server, it just tried to build the Dockerfile in my app's source code directory (which doesn't exist on the server).
我最终通过向本地 docker-compose.yml 添加显式图像字段来解决该问题.
I ended up solving the problem by adding an explicit image field to my local docker-compose.yml.
version: '2'
services:
web:
image: 'my-private-docker-registry:latest'
build: ./app
然后为生产创建了一个替代的撰写文件:
Then created an alternative compose file for production:
version: '2'
services:
web:
image: 'my-private-docker-registry:latest'
# no build field!
在本地运行 docker-compose build
后,使用存储库名称 my-private-docker-registry
和标签 latest<构建 Web 服务映像/代码>.
After running docker-compose build
locally, the web service image is built with the repository name my-private-docker-registry
and the tag latest
.
然后只是将图像推送到存储库的情况.
Then it's just a case of pushing the image up to the repository.
docker push 'my-private-docker-registry:latest'
并且运行 docker pull
,可以安全地停止并重新创建正在运行的容器,并使用新的镜像.
And running docker pull
, it's safe to stop and recreate the running containers, with the new images.
这篇关于部署 docker-compose 容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!