Linux 系统下 docker 搭建和使用

1 docker 搭建

docker 官方文档地址: https://docs.docker.com/engine/reference/commandline/docker/

安装方法官方文档:https://docs.docker.com/engine/install/ubuntu/#prerequisites

单用户授权: https://docs.docker.com/engine/install/linux-postinstall/

# 卸载之前的docker
sudo apt-get remove docker docker-engine docker.io containerd runc
# 安装docker 需要的包
 sudo apt-get update
 sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
    
# docker 添加证书
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    
# 本地的稳定安装
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 安装
 sudo apt-get update
 sudo apt-get install docker-ce docker-ce-cli containerd.io
 
# 用户加入docker 组别
# sudo groupadd docker  # 默认是有的
sudo usermod -aG docker $USER

出现下列信息说明安装成功。

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

2 docker 使用

2.1 查看 docker 镜像
docker image ls

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8CziOVTe-1642650775496)(D:\Program_Files\Typora\Picutre\Docker\001.png)]

  • REPOSITORY: 来自于哪个仓库;
  • TAG: 镜像的标签信息,比如 5.7、latest 表示不同的版本信息;
  • IMAGE ID: 镜像的 ID, 如果您看到两个 ID 完全相同,那么实际上,它们指向的是同一个镜像,只是标签名称不同罢了;
  • CREATED: 镜像最后的更新时间;
  • SIZE: 镜像的大小,优秀的镜像一般体积都比较小,这也是我更倾向于使用轻量级的 alpine 版本的原因;
2.2 给镜像重名
docker tag ubuntu:15.10 ubuntu15.10
docker image ls
#### 显示信息
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   3 months ago   13.3kB
ubuntu15.10   latest    9b9cb95443b5   5 years ago    137MB
ubuntu        15.10     9b9cb95443b5   5 years ago    137MB
2.3 docker 常用命令
#### 查看镜像的详细信息
docker inspect ubuntu15.10
#### 查看镜像历史信息
docker history ubuntu15.10
2.4 docker启动常用命令

所需要的命令主要为 docker run

当利用 docker run 来创建容器时,Docker 在后台运行的标准操作包括:

  • 检查本地是否存在指定的镜像,不存在就从 registry 下载
  • 利用镜像创建并启动一个容器
  • 分配一个文件系统,并在只读的镜像层外面挂载一层可读写层
  • 从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去
  • 从地址池配置一个 ip 地址给容器
  • 执行用户指定的应用程序
  • 执行完毕后容器被终止
# 查看docker中已下载的镜像
docker images
# 通过 docker中的一个镜像生成并进入容器
docker run ubuntu:18.04 /bin/echo 'Hello world'
# -t 选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上, -i 则让容器的标准输入保持打开
docker run -t -i ubuntu:18.04 /bin/bash
# 分配终端,挂载本地文件启动,同时给容器命名,( 进入容器时既可以指定容器名,也可以指定容器ID )
docker run -it --name="userTest001" -v  /opt/work/opt_user/:/home/mntdata  ubuntu_env_true:latest /bin/bash
# 修改容器名
docker rename userTest001 user_test

# 启动已终止容器
docker container start
# 查看终止状态的容器
docker container ls -a
# 查看运行状态的容器
docker container ls 
2.5 容器操作
# 查看 docker 中所有的容器
docker ps -a             # (-aq 只有容器id , -a 有状态显示 )
docker ps 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值