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
相关推荐
IT利刃出鞘1 小时前
Docker Compose--解决修改配置后不生效的问题
运维·docker·容器
java_logo1 小时前
Docker 部署 CloudBeaver:轻松搭建网页数据库管理平台
数据库·docker·容器·数据库管理·dbeaver·轩辕镜像·cloudbeaver
Lsetea1 小时前
Docker更新SSL证书后容器仍读旧文件:单文件挂载与目录挂载排查
运维·nginx·docker·https·ssl证书
好用斋3 小时前
拾光坞安装网盘聚合LitePan:把Docker命令翻译成图形界面
运维·docker·容器
对象存储与RustFS3 小时前
Velero 接 RustFS 实操:K8s 集群备份落到自建对象存储
后端·docker·云原生
70后写java的老码农6 小时前
docker-compose 部署 Redis Stack Server 6.2.6-v19
redis·docker
.冰块.7 小时前
流量大了自动加 Pod!K8s HPA 扩缩容与 ResourceQuota/LimitRange 资源管控实战
linux·云原生·容器·kubernetes·hpa·quota·limitrange
方便面不加香菜7 小时前
Docker Container -- 容器命令详解
运维·docker·容器
工业一体机老司机21 小时前
Docker部署工控机采集服务 Compose编排与开机自启
运维·docker·容器
miaobinfei1 天前
docker拉取dify镜像,执行docker compose up -d 报错
运维·docker·容器