简体   繁体   English

使用docker AND docker-compose构建的工作流程是什么?

[英]What is the workflow to build with docker AND docker-compose?

I have this repo , and docker-compose up will launch the project, create 2 containers (a DB and API), and everything works. 我有这个仓库docker-compose up将启动项目,创建2个容器(一个数据库和API),并且一切正常。

Now I want to build and deploy to Kubernetes. 现在,我想构建并部署到Kubernetes。 I try docker-compose build but it complains there's no Dockerfile. 我尝试docker-compose build但是它抱怨没有Dockerfile。 So I start writing a Dockerfile and then discover that docker/Dockerfiles don't support loading ENV vars from an env_file or .env file . 因此,我开始编写Dockerfile,然后发现docker / Dockerfile不支持从env_file或.env文件加载ENV var What gives? 是什么赋予了? How am I expected to build this image? 我应该如何建立这个形象? Could somebody please enlighten me? 有人可以启发我吗?

What is the intended workflow for building a docker image with the appropriate environment variables? 使用适当的环境变量构建docker映像的预期工作流程是什么?

Those environment variables shouldn't be set at docker build step but at running the application on Kubernetes or docker-compose. 这些环境变量不应在Docker构建步骤设置,而应在Kubernetes或docker-compose上运行应用程序时设置。

So: 所以:

Write a Dockerfile and place it at root folder. 编写一个Dockerfile并将其放置在根文件夹中。 Something like this: 像这样:

FROM node
COPY package.json .
RUN npm install
COPY . .
ENTRYPOINT ["npm", "start"]

Modify docker-compose.yaml. 修改docker-compose.yaml。 In the image field you must specify the name for the image to be built. 在图像字段中,您必须指定要生成的图像的名称。 It should be something like this: 应该是这样的:

image: YOUR-DOCKERHUB-USERNAME/node-rest-auth-arangodb

There is no need to set user and working_dir 无需设置userworking_dir

Build the image with docker-compose build (you can also do this with docker build) 使用docker-compose build构建映像(您也可以使用docker build来执行此操作)

Now you can use docker-compose up to run your app locally, with the .env file 现在,您可以使用docker-compose up.env文件在本地运行您的应用

To deploy it on Kubernetes you need to publish your image in dockerhub (unless you run Kubernetes locally): 要将其部署在Kubernetes上,您需要在dockerhub中发布映像(除非您在本地运行Kubernetes):

docker push YOUR-DOCKERHUB-USERNAME/node-rest-auth-arangodb

Finally, create a Kubernetes manifest. 最后,创建一个Kubernetes清单。 Sadly kubernetes doesn't support env files as docker-compose do, you'll need to manually set these variables in the manifest: 可悲的是,kubernetes不像docker-compose那样支持env文件,您需要在清单中手动设置以下变量:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: platform-api
  labels:
    app: platform-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: platform-api
  template:
    metadata:
      labels:
        app: platform-api
    spec:
      containers:
      - name: platform-api
        image: YOUR-DOCKERHUB-USERNAME/node-rest-auth-arangodb
        ports:
        - containerPort: 8080
        env:
          - name: NODE_ENV
            value: develop

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: platform-db
  labels:
    app: platform-db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: platform-db
  template:
    metadata:
      labels:
        app: platform-db
    spec:
      containers:
      - name: arangodb
        image: YOUR-DOCKERHUB-USERNAME/node-rest-auth-arangodb
        ports:
        - containerPort: 8529
        env:
          - name: ARANGO_ROOT_PASSWORD
            value: localhost

Deploy it with kubectl create 使用kubectl create部署

Please note that this code is just indicative, I don't know exactly your user case. 请注意,此代码仅供参考,我不完全了解您的用例。 Find more information in docker-compose and kubernetes docs and tutorials. 在docker-compose和kubernetes文档和教程中找到更多信息。 Good luck! 祝好运!

I've updated the project on github, it now all works, and the readme documents how to run it. 我已经在github上更新了该项目,它现在可以正常工作,并且自述文件记录了如何运行它。

I realized that env vars are considered runtime vars, which is why --env-file is an option for docker run and not docker build . 我意识到,ENV瓦尔被认为是运行时瓦尔,这就是为什么--env-file是一个选项, docker run ,而不是docker build This must also (I assume) be why docker-compose.yml has the env_file option, which I assume just passes the file to docker build . 这也必须(我假设)是为什么docker-compose.yml具有env_file选项的原因,我假设只是将文件传递给docker build And in Kubernetes, I think these are passed in from a configmap. 在Kubernetes中,我认为这些是从configmap传入的。 This is done so the image remains more portable; 这样做是为了使图像保持更可移植; same project can be run with different vars passed in, no rebuild required. 可以使用传入的不同var来运行同一项目,而无需重建。

Thanks ignacio-millán for the input. 感谢ignacio-millán的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM