Skip to content

开发常用

镜像选择

尽量选择 Alpine 版本,体积小,使用apk安装依赖,如 apk add --no-cache gcc g++ python2

如需要python2,尽量使用 alpine 3.14 左右的版本

镜像对比

属性 Alpine + apk Ubuntu + apt Debian Slim Distroless BusyBox
镜像大小 5MB 29MB 20MB 极小 1MB
兼容性 较低(需配置) 高 较高 静态编译 非常低
生态支持 较少 丰富 丰富 无包管理器 基本无
配置复杂度 较高(需调整) 较低 较低 中等 高

alpine 设置时区

apk add --no-cache tzdata
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' > /etc/timezone
可参考dockerfile
dockerfile
# Use the official Alpine image
FROM alpine:latest

# Set the environment variable for the timezone
ENV TZ=UTC
#ENV TZ=Asia/Shanghai

# Install tzdata package for timezone data
RUN apk add --no-cache tzdata

# Set the timezone
RUN cp /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Verify the timezone is set correctly
RUN date

# Your application setup goes here
# ...

# Example entrypoint
CMD ["sh"]

postgres

podman run --rm -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=hello_dev postgres:12.19-alpine

# -v "$PWD/data":/var/lib/postgresql/data

redis

docker run --rm -p 6379:6379 redis

# -v "$PWD/data":/data

mysql

docker run --rm -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=<default db> -p 3306:3306 mysql

# -v "$PWD/data":/var/lib/mysql

mongodb

docker run --rm -p 27017:27017 mongo

# -v "$PWD/data":/data/db

Prometheus + Grafana + loki + promtail + node-exporter

面板推荐 16098

docker-compose.yml
version: "3.3"

networks:
  loki:
    external: true

services:
  loki:
    image: grafana/loki:latest
    ports:
      - "3100:3100"
    command: -config.file=/etc/loki/local-config.yaml
    networks:
      - loki

  promtail:
    image: grafana/promtail:latest
    volumes:
      - /var/log:/var/log
    command: -config.file=/etc/promtail/config.yml
    networks:
      - loki

  grafana:
    environment:
      - GF_AUTH_ANONYMOUS_ENABLED=false
      - GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
    image: grafana/grafana:latest
    ports:
      - "3000:3000"
    networks:
      - loki

  prometheus:
    image: prom/prometheus:latest
    ports:
      - 9090:9090
    networks:
      - loki
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml

  node-exporter:
    image: prom/node-exporter:latest
    ports:
      - 9100:9100
    networks:
      - loki
prometheus.yml
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
      - targets: ["node-exporter:9100"]

编译设置代理

docker build -t ai:latest . --build-arg HTTP_PROXY=http://<ip>:<port>

启动映射代理

docker run --rm -p 8501:8501 --env http_proxy=socks5://host.docker.internal:10808 --env https_proxy=socks5://host.docker.internal:10808 --env all_proxy=socks5://host.docker.internal:10808 --gpus all rqlove/videolingo:latest

发送文件进continaer

docker cp wav2vec2_fairseq_base_ls960_asr_ls960.pth <容器id>:/root/.cache/torch/hub/checkpoints

环境变量映射

docker run --rm --env-file .env -p 3000:3000 ai-chat

映射目录问题(windows环境)

# cmd
docker run --rm -it -v %cd%:/user/src/app gcc
# powershell
docker run --rm -it -v ${PWD}:/user/src/app gcc
# linux
docker run --rm -it -v ${pwd}:/user/src/app gcc
# cross platform
docker run --rm -it -v ${PWD}:/user/src/app gcc
docker run --rm -it -v ${PWD}:/user/src/app gcc

dev container 例子

Dockerfile
# Use the official Node.js image to copy Node.js binaries
FROM node:16.20.0-alpine AS nodejs
# Node.js binaries are already installed in the official image at /usr/local/
# No additional steps needed here.

# Use the official Go image to copy Go binaries
FROM golang:1.21.0-alpine AS golang
# Go binaries are already installed in the official image at /usr/local/go
# No additional steps needed here.

# Use Alpine as the base image
FROM alpine:latest

# Install necessary dependencies in the Alpine image
RUN apk add --no-cache \
    librdkafka-dev \
    pkgconf \
    gcc \
    g++ \
    make \
    tzdata \
    git \
    python3 \
    bash \
    libc6-compat

# Copy Node.js from the Node.js image
COPY --from=nodejs /usr/local /usr/local

# Copy Go from the Go image
COPY --from=golang /usr/local/go /usr/local/go

# Set up environment variables for Go
ENV PATH="/usr/local/go/bin:${PATH}" \
    GOPATH=/go

ENV TZ=Asia/Shanghai
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone

RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://goproxy.cn,direct

# Verify installations
RUN node -v && npm -v && go version && python --version

万能解决各种

https://docs.docker.com/engine/containers/run/#runtime-privilege-and-linux-capabilities

privileged: true
or

docker run --privileged ..

在slim版本,使用apt如何减小体积

Dockerfile
RUN apt-get update && \
    apt-get install -y --no-install-recommends git && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*