Skip to content

docker compose 配置详解

docker-compose.yaml

docker-compose.yaml
yaml
version: '3.8'

services:

  ubuntu:
    build:
      dockerfile: ./Dockerfile.ubuntu # 指定 dockerfile
    # image: ubuntu:latest # 指定镜像名称
    container_name: my-ubuntu # 指定容器名称
    working_dir: /var/www/
    ports:
      - "8080:3000"
    restart: unless-stopped
    tty: true
    
  # 注意:如果使用了 build 指令,则不需要指定 image,因为 build 会自动创建镜像
  # 如果需要重新构建镜像,可以使用以下命令:  
  # docker-compose up -d --build ubuntu

  ubuntu_latest:
    image: ubuntu:latest
    container_name: ubuntu_latest
    volumes:
      - .:/app
    working_dir: /app
    ports:
      - "5000:5000"
    tty: true
    

  node_latest:
    image: node:latest
    container_name: node_latest
    volumes:
      - .:/app
      - /app/node_modules # 不予以挂载node_modules
    working_dir: /app
    ports:
      - "5001:5000"
    tty: true

  node_alpine:
    image: node:alpine
    container_name: node_alpine
    volumes:
      - .:/app
      - /app/node_modules # 不予以挂载node_modules
    working_dir: /app
    # ports:
    #   - "5002:5000"
    tty: true    


networks:
  vue3-demo-network: # 网络名称
    external: false # 外部网络:true 不存在则报错,false 不存在则创建 vue3-demo-network

Dockerfile.ubuntu

Dockerfile.ubuntu
ubuntu
FROM ubuntu:latest
 
# 更新包列表并安装unzip
RUN apt-get update && apt-get install -y unzip

node:latestnode:alpine 的区别

node:latest

  • 基于 Debian 的,体积较大,但是功能齐全,适合开发环境。
  • apt 做包管理器。
  • node:latest apt 换源:
sh
# 查看 apt 源配置文件路径
ls /etc/apt/sources.list.d/
# debian.sources
# /etc/apt/sources.list.d/debian.sources

# 查看 apt 源配置文件内容
cat /etc/apt/sources.list.d/debian.sources

# 修改 apt 源配置文件内容: http://deb.debian.org 改 http://mirrors.aliyun.com
sed -i "s@http://deb.debian.org@http://mirrors.aliyun.com@g" /etc/apt/sources.list.d/debian.sources

node:alpine

  • 基于 Alpine 的,体积较小,但是功能可能不全,适合生产环境。
  • apk 做包管理器。
  • node:alpine apk 换源:
sh
# 查看 apk 源配置文件路径
ls /etc/apk/repositories
# /etc/apk/repositories

# 查看 apk 源配置文件内容
cat /etc/apk/repositories

# 换源指令
sed -i "s@https://dl-cdn.alpinelinux.org@http://mirrors.aliyun.com@g" /etc/apk/repositories