[英]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.让我试着向你强调其中的一些。
Dockerfile
and Compose file
Dockerfile
和Compose file
的区别
Dockerfile
is used to build an image while Compose
is to build and run an application.Dockerfile
用于构建映像,而Compose
用于构建和运行应用程序。Dockerfile
then run it by Compose
Dockerfile
构建图像,然后通过Compose
运行它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
定义的容器image
then it would be download while built if specify the build: ./
image
,那么如果指定build: ./
Volumes
if you want to persistent data. Volumes
。 Remember, Doc is always your friend.请记住,Doc 永远是您的朋友。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.