SoFunction
Updated on 2025-03-10

Docker series compose ymal file analysis and learning

document

The yml file format is similar to JSON. It uses indentation to distinguish between superior and subordinate relationships, and supports multiple data types, characters, booleans, integers, dates, time, etc. The detailed syntax is available for you to check and view it yourself if you are interested in it.

The previous file example is as follows

# yaml configurationversion: '1.18'  
services:  
  web:  
    build: ./  
    ports:  
     - "8080:8080"  

The default is , and the .yaml extension format can also be used

Then explain the commands involved one by one

There is nothing to say about version, it represents the mirror version, which is consistent with the mirror version you are based on.

The services service provider module, which can also be considered a container instance, can contain multiple subservices, and can run multiple services with the same image at the same time.

web subservice name, customizable,

build is used to declare the context path of the image, which will scan the Dockerfile file under the path and build the image

All services must use the build command or image command to generate a specified image.

**ports service port mapping, can map multiple ports, equivalent to the docker -p command **

Dockerfile file parsing

Since the Dockerfile file has been mentioned many times, let's parse it.

The example is as follows

Dickerfile

# FROM based on golang:1.18-alpineFROM golang:1.18-alpine AS builder
# ENV Set environment variablesENV GOPATH=/opt/repo
ENV GO111MODULE=on
ENV GOPROXY=,direct
# RUN Setting Asia/Beijing time zoneRUN apk --no-cache add tzdata  && \
    ln -sf /usr/share/zoneinfo/Asia/Beijing /etc/localtime && \
    echo "Asia/Beijing" > /etc/timezone
# COPY Source Path Target Path COPY from the imageCOPY --from=builder /opt /opt
# EXPOSE Set port mappingEXPOSE 8080/tcp
# WORKDIR Settings Working DirectoryWORKDIR /opt
# CMD Setting Start CommandCMD ["./", "run"]

Actually, my annotations are already very clear

Here I will add to copy here. The source path of this copy is not the host's path, but the path in the mirror, and the target path is the path in the container.

workdir is the working directory set, but not in the host, but creates the working directory on each layer of the image

When docker build, a new image layer will be created whenever run is executed. Only when workdir is declared can the directory be created on each layer when creating a new image layer.

The above is the detailed content of the Docker series compose ymal file analysis learning. For more information about Docker compose ymal file analysis, please follow my other related articles!