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
相关推荐
福大大架构师每日一题2 小时前
go-zero v1.9.4 版本发布详解:云原生适配升级与稳定性性能全面提升
开发语言·云原生·golang
汪碧康16 小时前
【k8s-1.34.2安装部署】五.worker端containerd2.2.1、kubelet-1.34.2安装
docker·云原生·容器·kubernetes·jenkins·kubelet·xkube
忍冬行者1 天前
kubernetes安装traefik Gateway API,应对Ingress NGINX停止维护
云原生·kubernetes·云计算
-指短琴长-1 天前
Docker-Desktop修改WSL文件系统到D盘
docker·容器·eureka
没有bug.的程序员1 天前
微服务网关:从“必选项”到“思考题”的深度剖析
java·开发语言·网络·jvm·微服务·云原生·架构
没有bug.的程序员1 天前
Sentinel 流控原理深度解析:构建高可用微服务的底层架构
java·算法·微服务·云原生·架构·sentinel·负载均衡
前端程序猿之路1 天前
AI大模型应用开发之容器化部署
人工智能·python·语言模型·云原生·eureka·ai编程·改行学it
运维行者_1 天前
网络流量分析入门:从流量监控与 netflow 看懂核心作用
运维·开发语言·网络·云原生·容器·kubernetes·php
hanyi_qwe1 天前
Docker 容器操作 【docker (二)】
docker·容器·eureka