Docker安装及基本操作!!!超详细

一,Docker概述

1.1 docker是什么
  • Docker时Docker.Lnc公司开源的一个基于LXC技术之上搭建的Container容器引擎,源代码托管在Github上,基于Go语言并遵从Apache2.0协议开源。
  • Docker属于Linux容器的一种封装,提供简单易用的容器使用接口。
  • Docker将应用程序与该程序的依赖,打包在一个文件里面。运行这个文件,就会生成一个虚拟容器。程序在这个虚拟容器里运行,就好像在真实的物理机上运行一样。有了Docker,就不用担心环境问题。
  • 总体来说,Docker的接口相当简单,用户可以方便地创建和使用容器,把自己的应用放入容器。容器还可以进行版本管理、复制、分享、修改,就像管理普通的代码一样。
1.2 docker与虚拟机 的区别
Docker虚拟机
占用资源较少较多
启动时间短,十几毫秒较长,分钟来算
服务一般一个容器运行一个服务可以运行多个服务
安全共享内核,不安全系统逻辑隔离,安全
是否需要系统需要不需要

1.3 docker原理图

在这里插入图片描述

二,安装docker

1, YUM本地仓库优化(因为我用的是本地仓库,需要在线网源)

 [root@localhost ~]# cd /etc/yum.repos.d/
 [root@localhost yum.repos.d]# mv bakup/CentOS-Base.repo ./
 [root@localhost yum.repos.d]# yum clean all
 [root@localhost yum.repos.d]# yum makecache

2,安装依赖包

  [root@localhost ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
  #yum-utils提供了yum-config-manager
  #device mapper存储驱动程序需要device-mapper-persistent-data和lvm2
  #device mapper是Linux2.6 内核中支持逻辑卷管理的通用设备映射机制,它为实现用于存储资源管理的块设备驱动提供了一个高度模块化的内核架构。 

3 ,配置阿里云镜像源

 [root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4, 安装docker-测,社区版

 [root@localhost ~]# yum -y install docker-ce    ## 安装
 [root@localhost ~]# service docker start	     ## 启动docker服务
 [root@localhost ~]# service docker enable	     ## 启动docker服务

5, 加载镜像加速功能(去阿里云官网获取)

[root@localhost ~]# cd /etc/docker/
[root@localhost docker]# ll
总用量 4
-rw-------. 1 root root 244 11月 25 03:29 key.json
[root@localhost docker]# tee /etc/docker/daemon.json <<-'EOF'   ##加速的文件包
> {
>   "registry-mirrors": ["https://hd7w96z1.mirror.aliyuncs.com"]
> }
> EOF
{
  "registry-mirrors": ["https://hd7w96z1.mirror.aliyuncs.com"]
}
[root@localhost docker]# systemctl daemon-reload 
[root@localhost docker]# systemctl restart docker
-rw-------. 1 root root 244 11月 25 03:29 key.json         
[root@localhost docker]# docker version        ##查看docker版本及相关信息
Client: Docker Engine - Community
 Version:           19.03.13
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        4484c46d9d
 Built:             Wed Sep 16 17:03:45 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.13
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       4484c46d9d
  Built:            Wed Sep 16 17:02:21 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.3.7
  GitCommit:        8fba4e9a7d01810a393d5d25a3621dc101981175
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
 

6,网络优化

[root@localhost docker]# vi /etc/sysctl.conf
net.ipv4.ip_forward = 1      ###最后一行添加
[root@localhost docker]# sysctl -p
net.ipv4.ip_forward = 1

三,docker基础操作命令

3.1 镜像相关命令

1 , 查看镜像命令

[root@localhost ~]# docker images   ##可以看出是空的
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
 镜像名              标签                 镜像id               下载的时间           容量大小

2,在仓库里查找镜像( docker search 镜像名 )

[root@localhost ~]# docker search nginx    ##查找包含nginx的镜像        
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        14063               [OK]                
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1912                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   795                                     [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   131                                     
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   115                                     
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   105                                     [OK]
bitnami/nginx                      Bitnami nginx Docker Image                      90                                      [OK]
alfg/nginx-rtmp                    NGINX, nginx-rtmp-module and FFmpeg from sou…   80                                      [OK]
jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        72                                      [OK]
nginxdemos/hello                   NGINX webserver that serves a simple page co…   63                                      [OK]
nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         45                                      
privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & Al…   40                                      [OK]
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  20                                      
schmunk42/nginx-redirect           A very simple container to redirect HTTP tra…   19                                      [OK]
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       15                                      
centos/nginx-112-centos7           Platform for running nginx 1.12 or building …   15                                      
staticfloat/nginx-certbot          Opinionated setup for automatic TLS certs lo…   14                                      [OK]
raulr/nginx-wordpress              Nginx front-end for the official wordpress:f…   13                                      [OK]
centos/nginx-18-centos7            Platform for running nginx 1.8 or building n…   13                                      
mailu/nginx                        Mailu nginx frontend                            8                                       [OK]
bitwarden/nginx                    The Bitwarden nginx web server acting as a r…   7                                       
flashspys/nginx-static             Super Lightweight Nginx Image                   7                                       [OK]
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr…   6                                       [OK]
wodby/nginx                        Generic nginx                                   1                                       [OK]
ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                                       [OK]

  镜像项目名字/镜像名称                                    镜像描述                                   镜像的下载热度

3, 下载镜像 docker pull 镜像名

[root@localhost ~]# docker pull nginx   ##下载nginx镜像
Using default tag: latest
latest: Pulling from library/nginx
852e50cd189d: Pull complete 
571d7e852307: Pull complete 
addb10abd9cb: Pull complete 
d20aa7ccdb77: Pull complete 
8b03f1e11359: Pull complete 
Digest: sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# docker images    ##查看有那些镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        7 hours ago         133MB

4,下载的镜像存放的位置

[root@localhost overlay2]# pwd
/var/lib/docker/image/overlay2      ###存放的路径
[root@localhost overlay2]# ll
总用量 4
drwx------. 4 root root  58 11月 25 04:55 distribution
drwx------. 4 root root  37 11月 25 03:29 imagedb
drwx------. 4 root root  31 11月 25 04:55 layerdb
-rw-------. 1 root root 271 11月 25 04:55 repositories.json

5 ,查看所有镜像的相关信息

[root@localhost ~]# cat /var/lib/docker/image/overlay2/repositories.json 

6,查看某个镜像的相关的信息( docker inspect 镜像id )

[root@localhost overlay2]# docker inspect bc9a0695f571

7, 为镜像添加新的标签( docker tag 镜像名字:旧标签 镜像名字:新标签

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        8 hours ago         133MB
[root@localhost ~]# docker tag nginx:latest nginx:test
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        8 hours ago         133MB
nginx               test                bc9a0695f571        8 hours ago         133MB

8, 删除镜像
( docker rmi 镜像id )
( docker rmi 镜像:标签 )
( docker rmi 镜像id -f )

注意:当有两个相同的镜像时,删除镜像id会报错,只能删除-镜像:标签才可以把指定的镜像删除掉。
     当只存在一个镜像时才可以删除镜像id

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        8 hours ago         133MB
nginx               test                bc9a0695f571        8 hours ago         133MB
[root@localhost ~]# docker rmi bc9a0695f571
Error response from daemon: conflict: unable to delete bc9a0695f571 (must be forced) - image is referenced in multiple repositories
[root@localhost ~]# docker rmi nginx:test 
Untagged: nginx:test
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        8 hours ago         133MB 
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        8 hours ago         133MB
nginx               test                bc9a0695f571        8 hours ago         133MB

用docker rmi  镜像id  -f 可以删除两个别名
[root@localhost ~]# docker rmi bc9a0695f571 -f
Untagged: nginx:latest
Untagged: nginx:test
Untagged: nginx@sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3
Deleted: sha256:bc9a0695f5712dcaaa09a5adc415a3936ccba13fc2587dfd76b1b8aeea3f221c
Deleted: sha256:a6862ade3b91fdde2aa8a3d77fdcc95b1eb6c606be079c11b7f97f249d0e731d
Deleted: sha256:32bcbe3740b68d0625744e774b404140366c0c4a2b2eadf32280d66ba001b4fb
Deleted: sha256:2dc5e43f496e41a18c016904b6665454a53be22eb4dcc1b468d864b4e2d1f311
Deleted: sha256:5fe6a7c579cd9fbcfa604810974c4c0c16893f4c40bc801545607ebd0accea74
Deleted: sha256:f5600c6330da7bb112776ba067a32a9c20842d6ecc8ee3289f1a713b644092f8
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE


9,docker save -o 保存的镜像名称 镜像名称:标签,以文件的形式保存镜像,可以恢复。

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        8 hours ago         133MB
centos              latest              0d120b6ccaa8        3 months ago        215MB
[root@localhost ~]# docker save -o /opt/centos7 centos:latest 
[root@localhost ~]# cd /opt/
[root@localhost opt]# ll
总用量 217156
-rw-------. 1 root root 222366720 11月 25 05:40 centos7
drwx--x--x. 4 root root        28 11月 25 03:29 containerd
drwxr-xr-x. 2 root root         6 3月  26 2015 rh

10, docker save -o 保存的镜像名称 镜像名称:标签,以文件的形式保存镜像,可以恢复。

[root@localhost opt]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        8 hours ago         133MB
centos              latest              0d120b6ccaa8        3 months ago        215MB
[root@localhost opt]# docker rmi centos:latest 
Untagged: centos:latest
Untagged: centos@sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Deleted: sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566
Deleted: sha256:291f6e44771a7b4399b0c6fb40ab4fe0331ddf76eda11080f052b003d96c7726
[root@localhost opt]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        8 hours ago         133MB
[root@localhost opt]# docker load < /opt/centos7
291f6e44771a: Loading layer  222.4MB/222.4MB
Loaded image: centos:latest
[root@localhost opt]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        8 hours ago         133MB
centos              latest              0d120b6ccaa8        3 months ago        215MB

3.2 ,容器相关命令

1, 创建容器

[root@localhost ~]# docker create -it nginx:latest /bin/bash
628c218a899ee80b8d9eee1942d2affa3a8ca16181b2be28be4ec2fd92aaefcf
-i:让容器的标准输入保持打开
-t:让Docker分配一个伪终端

2, 开启容器,查看容器相关的信息(docker ps -a ) (docker start 容器id)

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
628c218a899e        nginx:latest        "/docker-entrypoint.…"   20 seconds ago      Created                                 sleepy_rhodes
[root@localhost ~]# docker start 628c218a899e
628c218a899e
您在 /var/spool/mail/root 中有新邮件
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
628c218a899e        nginx:latest        "/docker-entrypoint.…"   2 minutes ago       Up 3 seconds        80/tcp              sleepy_rhodes
容器id              镜像名称            当前执行命令               创建时间              当前状态

3,docker rm 容器ID号,删除容器,必须要先关闭容器才能删除。关闭的命令:docker stop 容器ID号。

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
628c218a899e        nginx:latest        "/docker-entrypoint.…"   2 minutes ago       Up 3 seconds        80/tcp              sleepy_rhodes
[root@localhost ~]# docker stop 628c218a899e
628c218a899e
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
628c218a899e        nginx:latest        "/docker-entrypoint.…"   7 minutes ago       Exited (137) 21 seconds ago                       sleepy_rhodes
                                                 
                                                                                   关闭状态                                                      
[root@localhost ~]# docker rm 628c218a899e
628c218a899e
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

4,run命令,执行完成会关闭,状态是Exited

[root@localhost ~]# docker run centos /bin/bash -c ls /
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED              STATUS                      PORTS               NAMES
d31bce13f960        centos              "/bin/bash -c ls /"   44 seconds ago       Exited (0) 44 seconds ago                       blissful_hypatia

5 进入容器(该容器一定要在Up状态)

[root@localhost ~]# docker exec -it 4a99916fa408 /bin/bash
[root@4a99916fa408 /]# exit
exit

6,docker export 容器ID号 > 容器文件存放路径,将容器导出作为文件

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS           
d31bce13f960        centos              "/bin/bash -c ls /"   14 minutes ago      Exited (0) 4 minu
4a99916fa408        centos              "/bin/bash"           15 minutes ago      Up 14 minutes    
[root@localhost ~]# docker export d31bce13f960 > /opt/centos7
[root@localhost ~]# docker export 4a99916fa408 > /opt/centos77
[root@localhost ~]# ls /opt/
centos7  centos77  containerd  rh

7,导入容器(会生成镜像,而不会创建容器)

[root@localhost ~]# cat /opt/centos7 | docker import - centos
sha256:691ddd74524349c05378caeaf7528eff3a5388aed30c79cbec4d4bb10089fe09
[root@localhost ~]# cat /opt/centos77 | docker import - centos
sha256:df920517c3f26cb5a8c03e10181a71ab588c0f85c32ee9d9001732e8a904e836

8,批量删除容器(必须状态是关闭的)

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
4a99916fa408        0d120b6ccaa8        "/bin/bash"         About an hour ago   Exited (0) 5 seconds ago                       musing_leavitt
[root@localhost ~]# docker ps -a | awk '{print "docker rm "$1}' | bash
Error: No such container: CONTAINER
4a99916fa408
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值