Linux系统Docker安装

安装 Docker

在 Linux 系统上安装 Docker 需要根据具体的发行版选择对应的安装方式。以下以 Ubuntu 为例:

更新软件包索引并安装依赖:

bash 复制代码
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common

添加 Docker 官方 GPG 密钥:

bash 复制代码
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

添加 Docker 仓库:

bash 复制代码
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

安装 Docker Engine:

bash 复制代码
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io

验证安装:

bash 复制代码
sudo docker run hello-world

管理 Docker 服务

启动 Docker 服务:

bash 复制代码
sudo systemctl start docker

设置 Docker 开机自启:

bash 复制代码
sudo systemctl enable docker

检查 Docker 运行状态:

bash 复制代码
sudo systemctl status docker

常用 Docker 命令

拉取镜像:

bash 复制代码
sudo docker pull ubuntu:latest

列出本地镜像:

bash 复制代码
sudo docker images

运行容器:

bash 复制代码
sudo docker run -it ubuntu /bin/bash

列出运行中的容器:

bash 复制代码
sudo docker ps

列出所有容器(包括停止的):

bash 复制代码
sudo docker ps -a

停止容器:

bash 复制代码
sudo docker stop <container_id>

删除容器:

bash 复制代码
sudo docker rm <container_id>

删除镜像:

bash 复制代码
sudo docker rmi <image_id>

构建自定义镜像

创建 Dockerfile:

dockerfile 复制代码
FROM ubuntu:latest
RUN apt update && apt install -y python3
CMD ["python3", "--version"]

构建镜像:

bash 复制代码
sudo docker build -t my-python-image .

运行自定义镜像:

bash 复制代码
sudo docker run my-python-image

数据卷和端口映射

挂载主机目录到容器:

bash 复制代码
sudo docker run -v /host/path:/container/path ubuntu

映射主机端口到容器端口:

bash 复制代码
sudo docker run -p 8080:80 nginx

容器网络

创建网络:

bash 复制代码
sudo docker network create my-network

连接容器到网络:

bash 复制代码
sudo docker run --network=my-network ubuntu

清理资源

删除所有停止的容器:

bash 复制代码
sudo docker container prune

删除未被使用的镜像:

bash 复制代码
sudo docker image prune -a

Docker Compose 使用

安装 Docker Compose:

bash 复制代码
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

创建 docker-compose.yml 文件:

yaml 复制代码
version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: example

启动服务:

bash 复制代码
sudo docker-compose up -d

停止服务:

bash 复制代码
sudo docker-compose down
相关推荐
Linux-187419 小时前
分布式链路追踪系统之二进制安装skywalking
elasticsearch·云原生·skywalking·分布式链路追踪系统·应用程序性能监控
阿里云云原生1 天前
阿里云 AgentTeams 7月第二周产品动态
云原生
CodexDave1 天前
数据库连接池耗尽:排查顺序与三层兜底
服务器·前端·数据库·git·云原生·容器·kubernetes
名字还没想好☜1 天前
Kubernetes Ingress 实战:域名路由、TLS 证书与 502/404 排查
运维·云原生·容器·kubernetes·ingress
Zhu7581 天前
在k8s部署alist 3.62.0
云原生·容器·kubernetes
阿里云云原生2 天前
阿里云 Agent Native Cloud:让智能体成为企业原生的能力
云原生·agent
小码哥哥2 天前
云原生时代的企业AI知识库:从云生态融合到多云知识治理
人工智能·云原生
IT小辉同学2 天前
# Milvus v3.0-beta docker-compose 启动失败完整排错教程
docker·eureka·milvus
人间凡尔赛2 天前
Post-Kubernetes 时代:2026 云原生架构从 Cloud Native 到 AI Native 的范式跃迁
云原生·架构·kubernetes
AOwhisky2 天前
Python 学习笔记(第十一期)——运维自动化(上·后篇):进程级监控与子进程管理——psutil进阶
运维·开发语言·python·学习·云原生·运维开发