docker的简介安装【CentOS8】与简单使用

简介

简介和特点

Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。
Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。
Docker容器的特点是一次构建,处处运行。
配置和部署在Docker容器上的项目,可以直接构建为一个Docker的镜像,在客户方而已直接部署运行,不用再做其他配置,方便运维的实现。

docker架构
三个基本概念:

《1》镜像(Image):Docker 镜像(Image),就相当于是一个 root 文件系统。比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系统的 root 文件系统。
《2》容器(Container):镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
《3》仓库(Repository):仓库可看成一个代码控制中心,用来保存镜像。

组成:

1、底层使用物理硬件(需要CPU的虚拟化支持V-t)
2、中间使用操作系统服务
3、操作系统之上是Docker容器
4、Docker容器中安装不同的app应用。
在这里插入图片描述

其它说明:

Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。

Docker 容器通过 Docker 镜像来创建。

容器与镜像的关系类似于面向对象编程中的对象与类。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
概念 说明
1.Docker 镜像(Images):

Docker 镜像是用于创建 Docker 容器的模板,比如 Ubuntu 系统。

2.Docker 容器(Container):

容器是独立运行的一个或一组应用,是镜像运行时的实体。

3.Docker 客户端(Client):

Docker 客户端通过命令行或者其他工具使用 Docker SDK Docker 的守护进程通信。

4.Docker 主机(Host):

一个物理或者虚拟的机器用于执行 Docker 守护进程和容器。

5.Docker Registry

Docker 仓库用来保存镜像,可以理解为代码控制中的代码仓库。

Docker Hub 提供了庞大的镜像集合供使用。

一个 Docker Registry 中可以包含多个仓库(Repository);每个仓库可以包含多个标签(Tag);每个标签对应一个镜像。

通常,一个仓库会包含同一个软件不同版本的镜像,而标签就常用于对应该软件的各个版本。我们可以通过 <仓库名>:<标签> 的格式来指定具体是这个软件哪个版本的镜像。如果不给出标签,将以 latest 作为默认标签。

6.Docker Machine

Docker Machine是一个简化Docker安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装Docker,比如VirtualBox、 Digital Ocean、Microsoft Azure。

Centos8安装

卸载旧版本

较旧的 Docker 版本称为 docker 或 docker-engine 。如果已安装这些程序,请卸载它们以及相关的依赖项。

yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
安装社区版 【Docker仓库进行安装】
1.安装所需的软件包

yum-utils 提供了 yum-config-manager ,并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2。

yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
2.配置源地址

官方的比较慢,一般用清华园,阿里云的
阿里云:

yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

清华园:

yum-config-manager \
    --add-repo \
    https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
3.安装最新版本的 Docker Engine-Community 和 containerd
yum install docker-ce docker-ce-cli containerd.io

Docker 安装完默认未启动。并且已经创建好 docker 用户组,但该用户组下没有用户。

4.验证
查看版本
docker version
或者运行镜像验证
docker pull hello-world
docker run hello-world

在这里插入图片描述

[root@docker ~]# docker pull hello-world
Using default tag: latest

latest: Pulling from library/hello-world
Digest: sha256:f2266cbfc127c960fd30e76b7c792dc23b588c0db76233517e1891a4e357d519
Status: Image is up to date for hello-world:latest
docker.io/library/hello-world:latest
[root@docker ~]# 
[root@docker ~]# docker run hello-world

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/

[root@docker ~]# 

5.开启docker服务并设置开机自启
systemctl start docker
systemctl enable docker
6.卸载docker
#删除安装包
yum remove docker-ce
# 删除镜像,容器,配置等
rm -rf /var/lib/docker

Docker入门命令使用

Docker客户端命令

docker客户端非常简单,我们可以直接输入docker 命令来查看到Docker客户端的所有命令选项。

[root@docker ~]# docker

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  app*        Docker App (Docker Inc., v0.9.1-beta3)
  builder     Manage builds
  buildx*     Build with BuildKit (Docker Inc., v0.5.1-docker)
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  scan*       Docker Scan (Docker Inc.)
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
[root@docker ~]# 

可以通过命令docker 命令 --help更深入的了解指定的Docker命令使用方法。

[root@docker ~]# docker stats --help

Usage:  docker stats [OPTIONS] [CONTAINER...]

Display a live stream of container(s) resource usage statistics

Options:
  -a, --all             Show all containers (default shows just running)
      --format string   Pretty-print images using a Go template
      --no-stream       Disable streaming stats and only pull the first result
      --no-trunc        Do not truncate output
[root@docker ~]# 

常用命令
(1) docker search :在docker index中搜索image

搜索docker的镜像
例如,

docker  search  HUB

搜索ubuntu镜像

docker  search  ubuntu
docker  search  library/hello-world
(2)docker pull :从docker registry server 中下拉image

从镜像服务器拉取镜像文件
docker pull 镜像文件

例如:在docker中拉取ubuntu

docker   pull   ubuntu

拉取hello-world镜像

docker  pull  library/hello-world
(3)其他的docker命令
docker ps 默认显示正在运行中的container
docker ps -a 列出所有的的容器,包括未运行的
docker rm <container…> 删除一个或多个container
docker rm docker ps -a -q 删除所有的container
docker rmi <image…> 删除一个或多个image
docker start/stop/restart 开启/停止/重启container
(4)查看docker的帮助
docker  --help
拉取镜像
1、配置加速器

国内连接docker官网很慢修改docker中配置,添加对应中国docker加速器。

vi  /etc/docker/daemon.json
{
    "registry-mirrors": ["https://registry.docker-cn.com"],
    "live-restore": true
}
systemctl daemon-reload
systemctl restart docker
2、拉取hello-world

运行以下命令(本处以hello-world为例),将image文件从仓库拉取到本地。

docker pull library/hello-world

其中拉取命令为docker pullhello-world为image为名称,library为hello-world镜像所在组。
执行完成后执行docker images可以查询。

3、运行拉取的镜像

docker run hello-world

4、停止镜像的运行

docker stop  镜像名称
docker stop hello-world
[root@docker ~]# docker pull library/hello-world
Using default tag: latest
latest: Pulling from library/hello-world
Digest: sha256:f2266cbfc127c960fd30e76b7c792dc23b588c0db76233517e1891a4e357d519
Status: Image is up to date for hello-world:latest
docker.io/library/hello-world:latest
[root@docker ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    d1165f221234   8 weeks ago   13.3kB
ubuntu        15.10     9b9cb95443b5   4 years ago   137MB
[root@docker ~]# docker run hello-world

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/

[root@docker ~]# docker stop hello-world
查看docker容器镜像目录结构
1、进入目录

使用root身份进入docker安装目录

[root@docker ~]# cd /var/lib/docker
[root@docker docker]# ls
buildkit  containers  image  network  overlay2  plugins  runtimes  swarm  tmp  trust  volumes
[root@docker docker]# 

2.查看容器containers

进入容器目录查看指定容器ID的目录:

[root@docker docker]# cd containers/
[root@docker containers]# ls
0384d1f7073852dcd227975570fd18eccab87dd369f72ea67900885410b92a2c  25ac9ec1478be04b272dd84318337506cbf9b2a90d1709b5bd70bb76aaf30f7f
0fb39bee94515fe59115abbd5ea64cd3a8687860af7892cdeb9749bdf82fce1b  349ef2e56511e6fafad004e874fc0016473b3a3cf02acca3fe6b6d36ae705393
[root@docker containers]# ls -a
.   0384d1f7073852dcd227975570fd18eccab87dd369f72ea67900885410b92a2c  25ac9ec1478be04b272dd84318337506cbf9b2a90d1709b5bd70bb76aaf30f7f
..  0fb39bee94515fe59115abbd5ea64cd3a8687860af7892cdeb9749bdf82fce1b  349ef2e56511e6fafad004e874fc0016473b3a3cf02acca3fe6b6d36ae705393
[root@docker containers]# cd 0384d1f7073852dcd227975570fd18eccab87dd369f72ea67900885410b92a2c/
[root@docker 0384d1f7073852dcd227975570fd18eccab87dd369f72ea67900885410b92a2c]# ls -l
总用量 28
-rw-r-----. 1 root root 2347 5月   3 23:34 0384d1f7073852dcd227975570fd18eccab87dd369f72ea67900885410b92a2c-json.log
drwx------. 2 root root    6 5月   3 23:34 checkpoints
-rw-------. 1 root root 2474 5月   3 23:34 config.v2.json
-rw-r--r--. 1 root root 1472 5月   3 23:34 hostconfig.json
-rw-r--r--. 1 root root   13 5月   3 23:34 hostname
-rw-r--r--. 1 root root  174 5月   3 23:34 hosts
drwx-----x. 2 root root    6 5月   3 23:34 mounts
-rw-r--r--. 1 root root   76 5月   3 23:34 resolv.conf
-rw-r--r--. 1 root root   71 5月   3 23:34 resolv.conf.hash
[root@docker 0384d1f7073852dcd227975570fd18eccab87dd369f72ea67900885410b92a2c]# 

3.查看image目录中的镜像文件

查看里面的json文件,可以看到有拉取的镜像的仓库名称,tag标签,容器id等等信息
在这里插入图片描述
在这里插入图片描述

[root@docker docker]# ls -l
总用量 8
drwx--x--x.  4 root root  120 4月  30 11:26 buildkit
drwx-----x.  6 root root 4096 5月   3 23:34 containers
drwx------.  3 root root   22 4月  30 11:26 image
drwxr-x---.  3 root root   19 4月  30 11:26 network
drwx-----x. 16 root root 4096 5月   3 23:34 overlay2
drwx------.  4 root root   32 4月  30 11:26 plugins
drwx------.  2 root root    6 5月   3 21:22 runtimes
drwx------.  2 root root    6 4月  30 11:26 swarm
drwx------.  2 root root    6 5月   3 23:24 tmp
drwx------.  2 root root    6 4月  30 11:26 trust
drwx-----x.  2 root root   50 5月   3 21:22 volumes
[root@docker docker]# cd image/
[root@docker image]# ls -a
.  ..  overlay2
[root@docker image]# cd overlay2/
[root@docker overlay2]# ls -l
总用量 4
drwx------. 4 root root  58 5月   3 22:46 distribution
drwx------. 4 root root  37 4月  30 11:26 imagedb
drwx------. 5 root root  45 5月   3 22:47 layerdb
-rw-------. 1 root root 544 5月   3 23:24 repositories.json
[root@docker overlay2]# cat repositories.json 
{"Repositories":{"hello-world":{"hello-world:latest":"sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726","hello-world@sha256:f2266cbfc127c960fd30e76b7c792dc23b588c0db76233
517e1891a4e357d519":"sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726"},"ubuntu":{"ubuntu:15.10":"sha256:9b9cb95443b5f846cd3c8cfa3f64e63b6ba68de2618a08875a119c81a8f96698","ubuntu@sha256:02521a2d079595241c6793b2044f02eecf294034f31d6e235ac4b2b54ffc41f3":"sha256:9b9cb95443b5f846cd3c8cfa3f64e63b6ba68de2618a08875a119c81a8f96698"}}}[root@docker overlay2]# 
Docker 本地镜像
1、导入本地的镜像文件
docker  import input  本地镜像文件

例如从阿里云上下载了alibaba-rocketmq-3.2.6.tar.gz镜像文件
cat alibaba-rocketmq-3.2.6.tar.gz | docker import - rocketmq:3.2.6(镜像名自己定义)

2、保存镜像
docker  save -o  目标镜像名称   源镜像名称

例如:
将刚刚导入的镜像保存到本地

docker  save –o  rocketmq.tar  rocketmq

ls查看保存的镜像

3、加载镜像文件
docker  load –input  镜像名称 
或者
docker  load < 镜像名称

例如,导入刚刚保存的镜像rocketmq.tar

docker  laod  --input  rocketmq.tar
[root@docker soft]# cat alibaba-rocketmq-3.2.6.tar.gz | docker import - rocketmq:3.2.6
sha256:453ef603e5f8c17f42660a82686ff99bf320f37edd26b9c52510bb0434a2f412
[root@docker soft]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
rocketmq      3.2.6     453ef603e5f8   13 seconds ago   12.2MB
hello-world   latest    d1165f221234   8 weeks ago      13.3kB
ubuntu        15.10     9b9cb95443b5   4 years ago      137MB
[root@docker soft]# docker save -o rocketmq.tar rocketmq
[root@docker soft]# ls
alibaba-rocketmq-3.2.6.tar.gz  rocketmq.tar
[root@docker soft]# docker load < rocketmq.tar 
Loaded image: rocketmq:3.2.6
[root@docker soft]# 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值