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

编译设置代理

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

映射目录问题(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 ..