docker用法整理:安装、启动与卸载,创建第一个docker容器

2022-11-2709:32:38服务器及运维Comments1,256 views字数 2666阅读模式
作者:大刚测试开发实战文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html

一、docker安装与启动

1.安装docker的几种方式

1)安装最新版本docker

① 先卸载旧版本的docker文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html

yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

② 指定Docker下载源(可选,适用于首次安装)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html

yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

③ 安装Docker(默认安装最新版本)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html

yum install -y docker-ce docker-ce-cli containerd.io
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html
docker用法整理:安装、启动与卸载,创建第一个docker容器

④ 验证是否安装成功文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html

docker version
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html
docker用法整理:安装、启动与卸载,创建第一个docker容器

2)安装指定版本的docker

yum list docker-ce --showduplicates | sort -r  # 查看所有可用版本
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io  # 安装指定版本
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html
docker用法整理:安装、启动与卸载,创建第一个docker容器

3)通过脚本一键安装docker

脚本内容如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html

#!/bin/bash
echo "set default docker install repo"
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
echo "install docker ..."
yum install -y docker-ce docker-ce-cli containerd.io
systemctl start docker
systemctl status docker

2.启动docker

systemctl start docker  # 启动服务
systemctl status docker  # 查看状态
systemctl stop docker  # 停止服务
systemctl restart docker  # 重启服务

二、创建第一个docker容器

1.创建容器

按照国际惯例,先运行一个hello-world的容器文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html

docker run hello-world

# 如果网络等一切正常的话,会出现如下提示,表示容器已经创建成功
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:18a657d0cc1c7d0678a3fbea8b7eb4918bba25968d3e1b0adebfa71caddbc346
Status: Downloaded newer image for hello-world:latest

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 ps -a  # 查看所有容器

# 创建成功,容器列表中就会有hello-world的容器,但名字不是hello-world,因为我们在运行容器时并未指定名称
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
a07f1a8ea1a4   hello-world   "/hello"   21 seconds ago   Exited (0) 20 seconds ago             adoring_chatterjee

三、卸载docker

1.常规方式卸载

① 停止docker服务文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html

systemctl stop docker

② 搜索已经安装的docker安装包文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html

yum list installed | grep docker
rpm -qa | grep docker
yum -y remove docker-ce.x86_64 
yum -y remove docker-ce-cli.x86_64 
yum -y remove containerd.io.x86_64

③ 移除所有相关安装包文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html

yum -y remove contained.io.x86_64

④ 删除docker镜像及相关文件夹文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html

rm -rf /var/lib/docker

2.脚本卸载

所谓的使用脚本安装和卸载,通俗理解就是把上述多个操作步骤的命令放在一个脚本中批量执行,内容如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html

#!/bin/bash
systemctl stop docker
yum -y remove docker-ce.x86_64
yum -y remove docker-ce-cli.x86_64
yum -y remove containerd.io.x86_64
rm -rf /var/lib/docker
rm -rf /etc/docker/daemon.json
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/30134.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/30134.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定