ubuntu系统与docker

一 安装docker 1.更新软件包列表

shell 复制代码
sudo apt update
  1. 安装依赖包
shell 复制代码
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
  1. 添加阿里云GPG密钥
shell 复制代码
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  1. 添加阿里云Docker软件源
shell 复制代码
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  1. 安装Docker Engine
shell 复制代码
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
  1. 配置Docker镜像加速(可选)

(1)创建Docker配置目录

shell 复制代码
sudo mkdir -p /etc/docker

(2)编辑配置文件

shell 复制代码
sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://你的阿里云镜像加速地址.mirror.aliyuncs.com"] } EOF

阿里云镜像加速地址需登录阿里云官网(容器镜像服务),在「镜像工具」→「镜像加速器」中获取。

(3)重启Docker服务

shell 复制代码
sudo systemctl daemon-reload
sudo systemctl restart docker
  1. 验证安装
shell 复制代码
docker --version
docker run hello-world # 测试拉取镜像

二 创建并进入Docker容器 方法 1:创建并交互式进入容器(常用) 使用 docker run 命令创建并直接进入容器,参数说明:

  • -it:交互式模式(-i)+ 分配伪终端(-t)
  • --name:指定容器名称(可选)
  • 镜像名:标签:使用的镜像(如 ubuntu:latest、centos:7 等)
  • /bin/bash:进入容器后执行的命令(通常为 shell)

示例:创建并进入一个名为 my-ubuntu 的 Ubuntu 容器

shell 复制代码
sudo docker run -it --name my-ubuntu ubuntu:latest /bin/bash

方法2:先创建容器,再进入容器

  1. 创建容器(后台运行)

    bash 复制代码
    sudo docker create -it --name my-centos centos:7 /bin/bash
  2. 启动容器

    bash 复制代码
    sudo docker start my-centos
  3. 进入容器

    bash 复制代码
    # 方法 A:使用 exec 进入(推荐,多进程安全)
    sudo docker exec -it my-centos /bin/bash
    
    # 方法 B:使用 attach 进入(单进程,退出会停止容器)
    sudo docker attach my-centos

三 退出容器的方式 临时退出,保留容器运行

按 Ctrl + P + Q

永久退出,停止容器

输入 exit 或按 Ctrl + D

四 常用容器管理命令

查看运行中的容器

sudo docker ps

查看所有容器(含停止的)

sudo docker ps -a

停止容器

sudo docker stop 容器名/容器ID

启动已停止的容器

sudo docker start 容器名/容器ID

删除容器

sudo docker rm 容器名/容器ID

相关推荐
java_logo12 小时前
Docker 部署 EmulatorJS:轻松搭建浏览器复古游戏平台
游戏·docker·容器·复古游戏·轩辕镜像·emulatorjs·红白机
方便面不加香菜15 小时前
Docker Container --容器及容器的操作案例详解
运维·docker·容器
无人生还别怕21 小时前
如何搭建docker仓库(轻量级)registry
linux·运维·docker·云原生·容器·registry·openanolisos
YYYing.2 天前
【K8s系列 (一) 】Docker与容器(上篇)
docker·云原生·容器·kubernetes
csdn_aspnet2 天前
使用 Docker 和 RedisInsight 设置 Redis 以进行本地开发
redis·docker·容器·redisinsight
余槐i2 天前
MySQL 8.0 联合索引走错:为什么加了索引查询反而从 200ms 变成 8s
java·spring boot·mysql·docker·性能优化
csdn_aspnet2 天前
如何在本地 Docker 容器中运行 Redis,并使用 Redis Insight 和 Redis CLI 进行管理
redis·docker·容器
且从容.2 天前
Ubuntu 安装 Docker
linux·ubuntu·docker
todoitbo2 天前
我在 Mac 上用 Docker 部署 Nexent:从拉取镜像到接入模型
macos·docker·大模型·ai智能体·nexent·快速入门与环境准备
Goboy2 天前
夯爆了,我被一个一句话接入的免费可观测平台策反了,那一刻我决定放弃用了3年的Prometheus
后端·docker·kubernetes