栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

Docker零基础教程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Docker零基础教程

文章目录
  • 前言
  • 一、安装Docker
    • 1.安装gcc相关
    • 2.安装docker需要的repository
    • 3.更新yum软件包索引
    • 4.安装Docker引擎
    • 5.启动Docker
    • 6.查看Docker版本
    • 7.运行hello world镜像
    • 8.停用并卸载Docker(了解即可)
  • 二、阿里云镜像加速
    • 1.镜像加速器
    • 2.配置镜像加速器
  • 三、Docker常用命令
    • 1.帮助启动类命令
    • 2.镜像命令
    • 3.容器命令
  • 四、镜像
    • 1.提交容器副本使之成为一个新的镜像
  • 总结


前言

在开始Docker的实操前,先熟悉下Docker的三要素:iamge镜像、container容器、repository仓库。仓库是存放镜像的地方,通过镜像可以run出一个又一个容器,举个Java例子来理解下镜像和容器的关系,镜像就好比Java的实体类,通过Java的new语法,可以通过实体类new出多个对象实例,而这个对象实例就好比Docker的容器。


一、安装Docker

通过 Docker的官网 提供了Docker的安装方法 ,大家可以阅读Docker官网上提供的方法,然后根据该教程一步一步安装。

1.安装gcc相关
yum -y install gcc
yum -y install gcc-c++
2.安装docker需要的repository
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

注意: yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 命令与官网提供的命令有区别。因为Docker来自国外,使用国外的repository下载会超级慢甚至连接超时,此处我们使用阿里云提供的镜像。

3.更新yum软件包索引
# centos7使用 yum makecache fast 命令
yum makecache fast
# centos8使用 yum makecache 命令
yum makecache
4.安装Docker引擎
yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
5.启动Docker
systemctl start docker
6.查看Docker版本
docker version
7.运行hello world镜像
docker run hello-world

如果查看Docker版本,运行hello-world镜像都可以打印正常信息,证明Docker安装并启动成功。

8.停用并卸载Docker(了解即可)
systemctl stop docker
yum remove docker-ce docker-ce-cli containerd.io docker-compose-plugin
rm -rf /var/lib/docker
rm -rf /var/lib/containerd

二、阿里云镜像加速 1.镜像加速器

使用支付宝账户登陆阿里云官网,找到 容器镜像服务 ,获取镜像加速器的地址。

2.配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://xxxxxx.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

注意:根据阿里云容器镜像服务提供的linux命令来配置Docker的加速地址时。路径要使用你自己的,而不是文章中的!


三、Docker常用命令 1.帮助启动类命令
  • 启动docker:systemctl start docker
  • 停止docker:systemctl stop docker
  • 重启docker:systemctl restart docker
  • 查看docker状态:systemctl status docker
  • 开启启动:systemctl enable docker
  • 查看docker概要信息:docker info
  • 查看docker总体帮助文档:docker --help
  • 查看docker命令帮助文档:docker 具体命令 --help

这类命令比较简单,运行docker后,使用一下就全明白了。


2.镜像命令
  • 列出本地主机上的镜像:docker images
[root@hecs-340056 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
ubuntu        latest    ba6acccedd29   9 months ago    72.8MB
hello-world   latest    feb5d9fea6a5   10 months ago   13.3kB

各个选项说明:
REPOSITORY:表示镜像的仓库源
TAG:镜像的标签版本号
IAMGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小


  • 查询某个镜像:docker search 某个镜像名字
[root@hecs-340056 /]# docker search redis
NAME                                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
redis                                       Redis is an open source key-value store that…   11202     [OK]       
bitnami/redis                               Bitnami Redis Docker Image                      227                  [OK]
bitnami/redis-sentinel                      Bitnami Docker Image for Redis Sentinel         39                   [OK]
bitnami/redis-cluster                                                                       34                   
rapidfort/redis                             RapidFort optimized, hardened image for Redi…   15                   
rapidfort/redis-cluster                     RapidFort optimized, hardened image for Redi…   15                   
circleci/redis                              CircleCI images for Redis                       14                   [OK]
ubuntu/redis                                Redis, an open source key-value store. Long-…   10                   
redis/redis-stack                           redis-stack installs a Redis server with add…   10                   
bitnami/redis-exporter                                                                      9                    
redis/redis-stack-server                    redis-stack-server installs a Redis server w…   8                    
clearlinux/redis                            Redis key-value data structure server with t…   4                    
ibmcom/redis-ha                                                                             1                    
bitnami/redis-sentinel-exporter                                                             1                    
ibmcom/ibm-cloud-databases-redis-operator   Container image for the IBM Operator for Red…   1                    
vmware/redis-photon                                                                         0                    
rapidfort/redis6-ib                         RapidFort optimized, hardened image for Redi…   0                    
greenbone/redis-server                      A redis service container image for the Gree…   0                    
cimg/redis                                                                                  0                    
rancher/redislabs-ssl                                                                       0                    
drud/redis                                  redis                                           0                    [OK]
corpusops/redis                             https://github.com/corpusops/docker-images/     0                    
blackflysolutions/redis                     Redis container for Drupal and CiviCRM          0                    
ibmcom/redisearch-ppc64le                                                                   0                    
ibmcom/redis-ppc64le                                                                        0                    
[root@hecs-340056 /]# 

各个选项说明:
NAME:镜像名称
DESCRIPTION:镜像说明
STARS:点赞数量
OFFICIAL:是否是官方的
AUTOMATED:是否是自动构建的


  • 下载镜像:docker pull 某个镜像名字
[root@hecs-340056 /]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
a2abf6c4d29d: Pull complete 
c7a4e4382001: Pull complete 
4044b9ba67c9: Pull complete 
c8388a79482f: Pull complete 
413c8bb60be2: Pull complete 
1abfd3011519: Pull complete 
Digest: sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
[root@hecs-340056 /]# 

如果输入docker pull redis 命令,默认下载最新版本redis镜像;如果想要指定redis版本,则 docker pull redis:6.0.8。


  • 查看镜像/容器/数据卷所占的空间:docker system df
[root@hecs-340056 /]# docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          3         2         185.5MB   112.7MB (60%)
Containers      6         1         8B        8B (100%)
Local Volumes   0         0         0B        0B
Build Cache     0         0         0B        0B
[root@hecs-340056 /]# 

  • 删除镜像: docker rmi 某个镜像名字或ID
[root@hecs-340056 /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
redis         latest    7614ae9453d1   7 months ago    113MB
ubuntu        latest    ba6acccedd29   9 months ago    72.8MB
hello-world   latest    feb5d9fea6a5   10 months ago   13.3kB
[root@hecs-340056 /]# docker rmi redis
Untagged: redis:latest
Untagged: redis@sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Deleted: sha256:7614ae9453d1d87e740a2056257a6de7135c84037c367e1fffa92ae922784631
Deleted: sha256:49c70179bc923a7d48583d58e2b6c21bde1787edf42ed1f8de9e9b96e2e88e65
[root@hecs-340056 /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
ubuntu        latest    ba6acccedd29   9 months ago    72.8MB
hello-world   latest    feb5d9fea6a5   10 months ago   13.3kB
[root@hecs-340056 /]# 

本文使用redis的名称来删除,还可以使用redis的ID删除。


3.容器命令
  • 新建+启动容器:docker run [OPTIONS] 镜像 [COMMAND] [ARG...]

OPTIONS说明: 注意:有些是一个减号,有些是两个减号
–name=容器新名字 为容器指定一个名称(–name是两个减号)
-d 后台运行容器并返回容器ID,即启动守护式容器,后台运行
-i 以交互模式运行容器,通常与 -t 同时使用
-t 为容器重新分配一个伪输入终端,通常与 -i 同时使用,即启动交互式容器
-P 随机端口映射
-p 指定端口映射

观察下列示例,执行docker run -it ubuntu /bin/bash命令,以前台交互式启动乌班图实例,在容器内执行/bin/bash命令。要退出乌班图终端,直接输入exit即可。

[root@hecs-340056 ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
ubuntu        latest    ba6acccedd29   9 months ago    72.8MB
hello-world   latest    feb5d9fea6a5   10 months ago   13.3kB
[root@hecs-340056 ~]# docker run -it ubuntu /bin/bash
root@118a8c76a228:/# ls   //此时已经进入到了乌班图终端
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@118a8c76a228:/# 

  • 列出当前所有正在运行的容器:docker ps [OPTIONS]

OPTIONS说明:
-a 列出当前所有正在运行的和历史上运行过的容器
-l 显示最近创建的容器
-n 显示最近n个创建的容器
-q 静默模式,只显示容器编号

[root@hecs-340056 /]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
118a8c76a228   ubuntu    "/bin/bash"   22 seconds ago   Up 21 seconds             funny_gagarin
[root@hecs-340056 /]# 

  • 退出容器:exit或者ctrl+p+q
    两种退出方式的区别:
    exit:run启动交互式容器,exit退出,容器停止
    ctrl+p+q:run启动交互式容器,ctrl+p+q退出,容器不停止
  • 启动已停止运行的容器:docker start 容器ID或者容器名
  • 重启容器:docker restart 容器ID或者容器名
  • 停止容器:docker stop 容器ID或者容器名
  • 强制停止容器:docker kill 容器ID或者容器名
  • 删除已停止的容器:docker rm 容器ID或者容器名

  • 启动守护式容器:docker run -d 镜像名
    本文以后台守护式启动redis容器,启动成功会返回容器ID。这个命令是日常工作中最常使用的。
[root@hecs-340056 ~]# docker run -d redis:6.0.8
2bdb979a4fc7b65cce1cfed3cfbe924bca675b698ac508812af99b290a4fd726
[root@hecs-340056 ~]# 

  • 启动交互式容器:docker run -it 镜像名

本文以前台交互式启动redis容器,当使用exit或者ctrl+c会停止并退出容器;使用ctrl+p+q退出但不停止容器。

[root@hecs-340056 ~]# docker run -it redis:6.0.8
1:C 09 Aug 2022 03:18:34.749 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 09 Aug 2022 03:18:34.749 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 09 Aug 2022 03:18:34.749 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.8 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

1:M 09 Aug 2022 03:18:34.750 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 09 Aug 2022 03:18:34.750 # Server initialized
1:M 09 Aug 2022 03:18:34.750 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 09 Aug 2022 03:18:34.750 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
1:M 09 Aug 2022 03:18:34.751 * Ready to accept connections

  • 进入正在运行的容器并与命令行交互:docker exec -it 容器ID /bin/bash
[root@hecs-340056 ~]# docker exec -it 2bdb979a4fc7 /bin/bash
root@2bdb979a4fc7:/data# redis-cli -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

  • 查看容器日志:docker logs 容器ID
  • 查看容器内运行的进程:docker top 容器ID
  • 查看容器内部细节:docker inspect 容器ID
  • 从容器内拷贝文件到主机上:docker cp 容器ID:容器内文件路径 目的主机路径

  • 导出容器:docker export 容器ID > 文件名.tar
    export导出容器的内容留作为一个tar归档文件,相当于备份一个容器
[root@hecs-340056 /]# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED       STATUS       PORTS      NAMES
2bdb979a4fc7   redis:6.0.8   "docker-entrypoint.s…"   3 hours ago   Up 3 hours   6379/tcp   adoring_galois
[root@hecs-340056 /]# ls
 bin  home  root  '启动命令 .txt'   
[root@hecs-340056 /]# docker export 2bdb979a4fc7 > abcd.tar
[root@hecs-340056 /]# ls
 abcd.tar   bin   home    root  '启动命令 .txt'
[root@hecs-340056 /]# 

  • 导入容器:cat 文件名.tar | docker import - 镜像用户/镜像名:镜像版本号
    import从tar包中的内容创建一个新的文件系统再导入为镜像,相当于恢复之前备份的容器
[root@hecs-340056 /]# cat abcd.tar | docker import - suibian/redis:6.0.8
sha256:80190400e705843ce0816e474ac16e4c1ca3581e9f2d4c53788ecf7606cea849
[root@hecs-340056 /]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED          SIZE
suibian/redis   6.0.8     80190400e705   16 seconds ago   101MB
redis           latest    7614ae9453d1   7 months ago     113MB
ubuntu          latest    ba6acccedd29   9 months ago     72.8MB
hello-world     latest    feb5d9fea6a5   10 months ago    13.3kB
redis           6.0.8     16ecd2772934   21 months ago    104MB

四、镜像 1.提交容器副本使之成为一个新的镜像

docker commit -m="提交的描述信息" -a="作者" 容器ID 要创建的目标镜像名:标签名
描述:假如在docker中下载并启动了centos镜像或者rabbitmq镜像,新启动的centos或者rabbitmq容器是最简洁版的,缺少很多插件,比如centos缺少vim命令、或者rabbitmq缺少第三方插件。如果我们给centos容器安装了vim命令、或者给rabbitmq安装了某些插件后,想把安装插件后的容器生成一个镜像时,就需要使用该命令。

[root@hecs-340056 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
a7c44182cfb5   centos    "/bin/bash"   7 minutes ago   Up 7 minutes             pedantic_chaum
[root@hecs-340056 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       latest    5d0da3dc9764   10 months ago   231MB
[root@hecs-340056 ~]# docker commit -m '随便写点什么' -a="小Y" a7c44182cfb5 mycentos:1.0.0
sha256:9db67ed69c43af6017333644b25647016747ef25aef4e1480dd5b7347ace8cca
[root@hecs-340056 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
mycentos     1.0.0     9db67ed69c43   5 seconds ago   233MB
centos       latest    5d0da3dc9764   10 months ago   231MB
[root@hecs-340056 ~]# 

总结

先发布一部分,剩下的我会抽时间慢慢完善…

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1039354.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号