Skip to content

部署 Ubuntu

运行 Ubuntu 容器

bash
# 交互式运行
docker run -it --name myubuntu ubuntu:22.04 bash

# 后台运行(保持存活)
docker run -d --name myubuntu ubuntu:22.04 sleep infinity

# 进入后台运行的容器
docker exec -it myubuntu bash

常用操作

bash
# 在容器内安装软件
apt-get update && apt-get install -y curl vim git

# 安装 sudo(如需要)
apt-get install -y sudo

# 查看系统版本
cat /etc/os-release
lsb_release -a

自定义 Ubuntu 镜像

dockerfile
FROM ubuntu:22.04

# 设置时区(避免交互式提示)
ENV DEBIAN_FRONTEND=noninteractive \
    TZ=Asia/Shanghai

# 更新并安装常用工具
RUN apt-get update && apt-get install -y \
    curl \
    wget \
    vim \
    git \
    unzip \
    net-tools \
    iputils-ping \
    && rm -rf /var/lib/apt/lists/*

# 设置时区
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# 创建普通用户
RUN useradd -m -s /bin/bash appuser

USER appuser
WORKDIR /home/appuser

CMD ["/bin/bash"]

使用国内 APT 镜像(加速)

dockerfile
FROM ubuntu:22.04

# 使用阿里云镜像源
RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list \
    && sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list \
    && apt-get update \
    && apt-get install -y curl vim \
    && rm -rf /var/lib/apt/lists/*

典型用途

Ubuntu 容器常用于:

  • 作为其他镜像的基础镜像
  • 调试和测试环境
  • 运行需要完整 Linux 环境的脚本

提示:如果只需要最小化 Linux 环境,优先考虑 Alpine(alpine:3.x),体积仅约 5MB,而 Ubuntu 约 77MB。