Docker 安装以及常见操作

Ubuntu 下 Docker 安装以及常见操作

一. Docker 安装
1. 卸载旧Docker
shell 复制代码
sudo apt-get remove docker docker-engine docker.io containerd runc
2. 安装docker依赖

Docker在Ubuntu上依赖一些软件包。

shell 复制代码
sudo apt update sudo apt upgrade
sudo apt-get install ca-certificates curl gnupg lsb-release
3. 添加秘钥
shell 复制代码
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
4. 添加软件源
shell 复制代码
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
5. 安装Docker
shell 复制代码
apt-get install docker-ce docker-ce-cli containerd.io
二. 配置用户组

默认情况下,只有root用户和docker组的用户才能运行Docker命令。我们可以将当前用户添加到docker组,以避免每次使用Docker时都需要使用sudo。

如果出现"启动"docker.service"需要认证。Multiple identities can be used for authentication:"的报错,说明是没有将当前用户加入到docker用户组中。

创建docker组(如果已经有,则不用创建)

shell 复制代码
sudo groupadd docker

将用户加入用户组

shell 复制代码
sudo usermod -aG docker $USER

重新登陆

刷新用户组

shell 复制代码
newgrp docker

可以通过一下命令查看存在的用户组

shell 复制代码
newgrp docker

测试能否使用docker:

shell 复制代码
docker run hello-world
三.常用命令
1. 基础命令

查看docker版本信息

shell 复制代码
docker version
docker info

启动 docker

shell 复制代码
systemctl start docker

关闭 docker

shell 复制代码
systemctl stop docker

重启 docker

shell 复制代码
systemctl restart docker

设置docker随服务启动而启动

shell 复制代码
systemctl enable docker

查看docker运行状态

shell 复制代码
systemctl status docker

如果在运行中,输入命令后会看到绿色的 active(running)

2. 镜像命令

查看镜像列表

shell 复制代码
docker images

搜索镜像

shell 复制代码
docker search 镜像名
docker search --filter=STARS=9000 mysql 搜索 STARS >9000的 mysql 镜像

拉取镜像

拉取镜像 不加tag(版本号) 即拉取docker仓库中该镜像的最新版本latest,加:tag则是拉取指定版本

shell 复制代码
docker pull 镜像名 
docker pull 镜像名:tag

运行镜像

shell 复制代码
docker run 镜像名
docker run 镜像名:Tag
3. 容器命令

查看运行中的容器

shell 复制代码
docker ps

查看所有容器

shell 复制代码
docker ps -a

启动容器

shell 复制代码
docker start 容器id或容器名

停止容器

shell 复制代码
docker stop 容器id或容器名

查看容器的所有信息

shell 复制代码
docker inspect 容器id

查看容器日志

shell 复制代码
doker container logs 容器id

查看容器里的进程

shell 复制代码
docker top 容器id

退出容器

shell 复制代码
exit

删除已停止的容器

shell 复制代码
docker rm 容器id或name

删除正在运行的容器

shell 复制代码
docker rm -f 容器id

进入容器

shell 复制代码
docker exec -it 容器ID sh
相关推荐
xidianjiapei0011 小时前
Kubernetes的Ingress 资源是什么?
云原生·容器·kubernetes
sszdzq2 小时前
Docker
运维·docker·容器
dmy3 小时前
docker 快速构建开发环境
后端·docker·容器
土豆沒加4 小时前
K8S的Dashboard登录及验证
云原生·容器·kubernetes
终端行者5 小时前
kubernetes1.28部署mysql5.7主从同步,使用Nfs制作持久卷存储,适用于centos7/9操作系统,
数据库·容器·kubernetes
2401_897930066 小时前
linux系统如何配置host.docker.internal
linux·docker·eureka
亲持红叶10 小时前
open-webui安装
docker·open-webui
伪装成塔的小兵10 小时前
Windows使用docker部署fastgpt出现的一些问题
windows·docker·容器·oneapi·fastgpt
转身後 默落14 小时前
11.Docker 之分布式仓库 Harbor
分布式·docker·容器
菩提云14 小时前
Deepseek存算分离安全部署手册
人工智能·深度学习·安全·docker·容器