简体   繁体   中英

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.

There are two files.

Dockerfile

FROM node:lts-alpine

WORKDIR /server

COPY package*.json ./

RUN npm install

COPY . . 

EXPOSE 3030

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

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
When I run docker-compose up --build

a. The image will be built based on Dockerfile
b. What's then?

Question 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?

Question 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?

Update

Question 4

    build: ./

What is this line for?

It is recommended to go through the Getting Started , most of your questions would be solved.

Let me try to highlight some of those to you.

  1. The difference between Dockerfile and Compose file
    1. Docker can build images automatically by reading the instructions from a Dockerfile
    2. Compose is a tool for defining and running multi-container Docker applications
    3. The main difference is Dockerfile is used to build an image while Compose is to build and run an application.
    4. You have to build an image by Dockerfile then run it by 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
  3. If you specify the image then it would be download while built if specify the build: ./
  4. Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. , 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.

Remember, Doc is always your friend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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