简体   繁体   English

docker-compose.yml 和 dockerfile 如何协同工作

[英]how does docker-compose.yml and dockerfile works together

From my understanding,据我了解,
Dockerfile is like the config/recipe for creating the image, while docker-compose is used to easily create multiple containers which may have relationship, and avoid creating the containers by docker command repeatedly. Dockerfile 就像创建镜像的 config/recipe,而 docker-compose 用于轻松创建多个可能有关系的容器,避免通过 Z05B6053C41A2130AFD6FC3B158BDA4E6 命令重复创建容器。

There are two files.有两个文件。

Dockerfile Dockerfile

FROM node:lts-alpine

WORKDIR /server

COPY package*.json ./

RUN npm install

COPY . . 

EXPOSE 3030

CMD ["npm", "run", "dev"]

docker-compose.yml docker-compose.yml

version: '2.1'

services:
  test-db:
    image: mysql:5.7
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
      - MYSQL_USER=admin
      - MYSQL_PASSWORD=12345
      - MYSQL_DATABASE=test-db
    volumes:
      - ./db-data:/var/lib/mysql
    ports:
      - 3306:3306
  test-web:
    environment:
      - NODE_ENV=local
      #- DEBUG=*
      - PORT=3030
    image: node:lts-alpine
    build: ./
    command: >
        npm run dev
    volumes:
      - ./:/server
    ports:
      - "3030:3030"
    depends_on:
      - test-db

Question 1问题 1
When I run docker-compose up --build当我运行 docker-compose up --build

a.一个。 The image will be built based on Dockerfile镜像将基于 Dockerfile 构建
b.湾。 What's then?那是什么?

Question 2问题2

  test-db:
    image: mysql:5.7
  test-web:
    environment:
      - NODE_ENV=local
      #- DEBUG=*
      - PORT=3030
    image: node:lts-alpine

I am downloading the image for dockerhub with above code, but why and when do I need the image created in --build?我正在使用上面的代码下载 dockerhub 的图像,但是为什么以及何时需要在 --build 中创建的图像?

Question 3问题 3

    volumes:
      - ./db-data:/var/lib/mysql

Is this line means that the data is supposed to store at memory in location /var/lib/mysql, while I copy this data in my working directory./db-data?这行是否意味着数据应该存储在位置 /var/lib/mysql 的 memory 中,而我将这些数据复制到我的工作目录./db-data 中?

Update更新

Question 4问题 4

    build: ./

What is this line for?这条线是干什么用的?

It is recommended to go through the Getting Started , most of your questions would be solved.建议通过入门go ,您的大部分问题都会得到解决。

Let me try to highlight some of those to you.让我试着向你强调其中的一些。

  1. The difference between Dockerfile and Compose file DockerfileCompose file的区别
    1. Docker can build images automatically by reading the instructions from a Dockerfile Docker 可以通过读取 Dockerfile 的指令自动构建图像
    2. Compose is a tool for defining and running multi-container Docker applications Compose 是一个用于定义和运行多容器 Docker 应用程序的工具
    3. The main difference is Dockerfile is used to build an image while Compose is to build and run an application.主要区别在于Dockerfile用于构建映像,而Compose用于构建和运行应用程序。
    4. You have to build an image by Dockerfile then run it by Compose您必须通过Dockerfile构建图像,然后通过Compose运行它
  2. After you run docker-compose up --build the image is built and cached in your system, then Compose would start the containers defined by docker-compose.yml在你运行docker-compose up --build之后,镜像被构建并缓存在你的系统中,然后 Compose 将启动docker-compose.yml定义的容器
  3. If you specify the image then it would be download while built if specify the build: ./如果您指定image ,那么如果指定build: ./
  4. Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.卷是保存由 Docker 容器生成和使用的数据的首选机制。 , Images are read-only, and all editing for a container would be destroyed after it's deleted, so you have to use Volumes if you want to persistent data. , 图像是只读的,容器的所有编辑在删除后都会被破坏,所以如果你想持久化数据,你必须使用Volumes

Remember, Doc is always your friend.请记住,Doc 永远是您的朋友。

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

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