在ubuntu中使用docker

当前环境

Ubuntu 22.04.3 LTS

一、如何安装

1) 安装

# 更新apt源
sudo apt update
# 安装依赖
sudo apt install ca-certificates curl gnupg
# 添加公钥
install -m 0755 -d /etc/apt/keyrings

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

sudo chmod a+r /etc/apt/keyrings/docker.gpg

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 更新apt源
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# 将当前用户添加到docker用户组
sudo usermod -a -G docker $(whoami)
sudo chmod o+rw /var/run/docker.sock

2) 配置镜像加速

镜像站网址:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

进入该网址,按照指示操作即可

二、如何使用

docker是运行docker镜像的工具,在进入虚拟环境之前还需要下载一个合适的镜像

1) 搜索与下载镜像

使用命令搜索镜像

# 搜索镜像
docker search <key word>
cxcc@cxcc-vm:/etc/docker$ docker search ubuntu
NAME                             DESCRIPTION                                      STARS     OFFICIAL
ubuntu                           Ubuntu is a Debian-based Linux operating sys…   16787     [OK]
websphere-liberty                WebSphere Liberty multi-architecture images …   296       [OK]
open-liberty                     Open Liberty multi-architecture images based…   62        [OK]
neurodebian                      NeuroDebian provides neuroscience research s…   106       [OK]
ubuntu-debootstrap               DEPRECATED; use "ubuntu" instead                 52        [OK]
ubuntu-upstart                   DEPRECATED, as is Upstart (find other proces…   115       [OK]
ubuntu/nginx                     Nginx, a high-performance reverse proxy & we…   111
ubuntu/squid                     Squid is a caching proxy for the Web. Long-t…   76
ubuntu/cortex                    Cortex provides storage for Prometheus. Long…   4
ubuntu/prometheus                Prometheus is a systems and service monitori…   55
ubuntu/apache2                   Apache, a secure & extensible open-source HT…   70
ubuntu/kafka                     Apache Kafka, a distributed event streaming …   38
ubuntu/bind9                     BIND 9 is a very flexible, full-featured DNS…   69
ubuntu/mysql                     MySQL open source fast, stable, multi-thread…   57
ubuntu/zookeeper                 ZooKeeper maintains configuration informatio…   12
ubuntu/postgres                  PostgreSQL is an open source object-relation…   34
ubuntu/redis                     Redis, an open source key-value store. Long-…   22
ubuntu/jre                       Distroless Java runtime based on Ubuntu. Lon…   13
ubuntu/dotnet-aspnet             Chiselled Ubuntu runtime image for ASP.NET a…   17
ubuntu/grafana                   Grafana, a feature rich metrics dashboard & …   9
ubuntu/dotnet-deps               Chiselled Ubuntu for self-contained .NET & A…   13
ubuntu/memcached                 Memcached, in-memory keyvalue store for smal…   5
ubuntu/dotnet-runtime            Chiselled Ubuntu runtime image for .NET apps…   14
ubuntu/prometheus-alertmanager   Alertmanager handles client alerts from Prom…   9
ubuntu/cassandra                 Cassandra, an open source NoSQL distributed …   2

在官网搜索镜像:Docker Hub

# 拉取镜像
docker pull ubuntu
# 查看镜像列表
docker images

2) 启动镜像

启动镜像时可以添加目录映射、端口映射等

# 启动镜像并进行端口和文件映射,指定容器名
docker run -itd --name test_ubuntu -p 8080:80 -v /host/path:/container/path ubuntu:22.04 
# 查看正在运行的容器
docker ps
# 查看所有容器
docker ps -a
# 删除容器
docker rm <容器ID或容器名>
# 查看镜像日志
docker logs <容器ID或容器名>

参考文章

  1. Docker CE 软件仓库 (安装教程)
  2. ubuntu配置docker源,配置国内加速
  3. Docker Image(镜像)基本操作(搜索、获取、删除镜像)
0%