原创

Docker介绍及配置安装(一)

一、Docker介绍

Docker是Docker.lnc公司开源的一个基于LXC技术之上构建的Container容器引擎,源代码托管在Github上,基于Go语言并遵从Apache2.0协议开源

Docker是通过内核虚拟化技术(namespaces及cgroups等)来提供容器的资源隔离与安全保障等。由于Docker通过操作系统层的虚拟化实现隔离,所以Docker容器在运行时,不需要类似虚拟机(VM)额外的操作系统开销,提高资源利用率


二、Docker组件

镜像:Image

        镜像介绍:类似于虚拟机,作用和虚拟机是一样的,唯独是组成部分会有些区别。简单的说如果我们想启动一个容器就必须要有镜像

容器:Container

        容器介绍:docker是通过容器来运行业务的,就像运行一个kvm虚拟机是一样的。容器其实就是从镜像创建的一个实例。

仓库:Repository

        容器介绍:docker是通过容器来运行业务的,就像运行一个kvm虚拟机是一样的。容器其实就是从镜像创建的一个实例。

三、Docker作用

1、简化配置

Docker公司宣传的Docker的主要使用场景。虚拟机的最大好处是能在你的硬件设施上运行各种配置不一样的平 台(软件、系统),Docker在降低额外开销的情况下提供了同样的功能。它能让你将运行环境和配置放在代码中然后部署,同一个Docker的配置可以在 不同的环境中使用,这样就降低了硬件要求和应用环境之间耦合度。

2、代码流水线管理

前一个场景对于管理代码的流水线起到了很大的帮助。代码从开发者的机器到最终在生产环境上的部署,需要经过很多的中间环境。而每一个中间环境都有自己微小的差别,Docker给应用提供了一个从开发到上线均一致的环境,让代码的流水线变得简单不少。

3、整个服务器

Docker隔离应用的能力使得Docker可以整合多个服务器以降低成本。由于没有多个操作系统的内存占用,以及能在多个实例之间共享没有使用的内存,Docker可以比虚拟机提供更好的服务器整合解决方案

4、快速部署

在虚拟机之前,引入新的硬件资源需要消耗几天的时间。Docker的虚拟化技术将这个时间降到了几分钟,Docker只是创建一个容器进程而无需启动操作系统,这个过程只需要秒级的时间。这正是Google和Facebook都看重的特性。


三、Docker简单安装、操作

1、系统环境

[root@bogon ~]# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core) 
[root@bogon ~]# uname -r
3.10.0-693.el7.x86_64

2、安装Docker(社区版)

yum update
# 卸载旧的包
yum remove docker  docker-common docker-selinux docker-engine
# 安装所需依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
# 设置docker的yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 安装Docker
yum install docker-ce
# 查看版本
[root@bogon ~]# docker -v
Docker version 20.10.12, build e91ed57

# 安装后,修改net.ipv4.ip_forward=1
vim /etc/sysctl.conf
net.ipv4.ip_forward=1

3、启动

[root@bogon ~]# systemctl start docker
[root@bogon ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since 一 2021-12-27 16:48:51 CST; 7s ago
     Docs: https://docs.docker.com
 Main PID: 58428 (dockerd)
    Tasks: 8
   Memory: 30.8M
   CGroup: /system.slice/docker.service
           └─58428 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

12月 27 16:48:51 bogon dockerd[58428]: time="2021-12-27T16:48:51.502525101+08:00" level=info msg="scheme \"unix\" not registered, fallback to default scheme" module=grpc
12月 27 16:48:51 bogon dockerd[58428]: time="2021-12-27T16:48:51.502555454+08:00" level=info msg="ccResolverWrapper: sending update to cc: {[{unix:///run/containerd/containerd.sock  <nil> 0 <nil>}] <nil> <nil>}" module=grpc
12月 27 16:48:51 bogon dockerd[58428]: time="2021-12-27T16:48:51.502567605+08:00" level=info msg="ClientConn switching balancer to \"pick_first\"" module=grpc
1227 16:48:51 bogon dockerd[58428]: time="2021-12-27T16:48:51.522678992+08:00" level=info msg="Loading containers: start."
1227 16:48:51 bogon dockerd[58428]: time="2021-12-27T16:48:51.755529111+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address"
1227 16:48:51 bogon dockerd[58428]: time="2021-12-27T16:48:51.814067711+08:00" level=info msg="Loading containers: done."
1227 16:48:51 bogon dockerd[58428]: time="2021-12-27T16:48:51.833001311+08:00" level=info msg="Docker daemon" commit=459d0df graphdriver(s)=overlay2 version=20.10.12
1227 16:48:51 bogon dockerd[58428]: time="2021-12-27T16:48:51.833331453+08:00" level=info msg="Daemon has completed initialization"
1227 16:48:51 bogon systemd[1]: Started Docker Application Container Engine.
1227 16:48:51 bogon dockerd[58428]: time="2021-12-27T16:48:51.857815414+08:00" level=info msg="API listen on /var/run/docker.sock"
[root@bogon ~]# 

docker会自动生成一个网桥 docker0,如下:

[root@bogon ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:04:3e:ca:ca  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.30.132  netmask 255.255.255.0  broadcast 192.168.30.255
        inet6 fe80::b4b0:2c0e:ce9b:6650  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:46:2d:af  txqueuelen 1000  (Ethernet)
        RX packets 349668  bytes 477296341 (455.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 221025  bytes 18307635 (17.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 68  bytes 5916 (5.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 5916 (5.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

4、docker命令

①查看镜像

[root@bogon ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
nginx        latest    f6987c8d6ed5   6 days ago   141MB

②查找镜像

[root@bogon ~]# docker search nginx
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                             Official build of Nginx.                        16023     [OK]       
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2104                 [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of820                  [OK]
jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   298                  
linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   161                  
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   148                  [OK]
jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        147                  [OK]
alfg/nginx-rtmp                   NGINX, nginx-rtmp-module and FFmpeg from sou…   112                  [OK]
nginxdemos/hello                  NGINX webserver that serves a simple page co…   81                   [OK]
privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   61                   [OK]
nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo…   59                   
nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  56                   
nginxproxy/nginx-proxy            Automated Nginx reverse proxy for docker con…   32                   
staticfloat/nginx-certbot         Opinionated setup for automatic TLS certs lo…   25                   [OK]
nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NGIN…   22                   
schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   19                   [OK]
centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   16                   
centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   13                   
bitwarden/nginx                   The Bitwarden nginx web server acting as a r…   12                   
flashspys/nginx-static            Super Lightweight Nginx Image                   11                   [OK]
mailu/nginx                       Mailu nginx frontend                            10                   [OK]
webdevops/nginx                   Nginx container                                 9                    [OK]
sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   7                    [OK]
ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          3                    [OK]
wodby/nginx                       Generic nginx                                   1                    [OK]

③下载镜像    docker pull 镜像

[root@bogon ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete 
f3409a9a9e73: Pull complete 
9919a6cbae9c: Pull complete 
fc1ce43285d7: Pull complete 
1f01ab499216: Pull complete 
13cfaf79ff6d: Pull complete 
Digest: sha256:366e9f1ddebdb844044c2fafd13b75271a9f620819370f8971220c2b330a9254
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

④导出镜像

[root@bogon data]# docker save -o nginx.tar nginx
[root@bogon data]# ll
总用量 142476
-rw-------. 1 root root 145894912 12月 27 17:02 nginx.tar

⑤删除镜像    docker rmi  [IMAGE ID]

[root@bogon data]# docker images;
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
nginx        latest    f6987c8d6ed5   6 days ago   141MB
[root@bogon data]# docker rmi f6987c8d6ed5
Untagged: nginx:latest
Untagged: nginx@sha256:366e9f1ddebdb844044c2fafd13b75271a9f620819370f8971220c2b330a9254
Deleted: sha256:f6987c8d6ed59543e9f34327c23e12141c9bad1916421278d720047ccc8e1bee
Deleted: sha256:7a9fa9abb8f38aafaef0a3d61bc83022234ab9319cfb808cbc1c3ef54871e169
Deleted: sha256:ca445a5b504f4cc47b9770500a1ed358052e1ad84a5b668eb0ea3094cc3d381f
Deleted: sha256:11fb178a72e8116c0d9e200045d287acb4f97386126c21dc66ce23ff75e64616
Deleted: sha256:bcfff1a0df2b9155f1a2e61f811e9e47fdc22b8ddf590c044959e5680233e230
Deleted: sha256:8f50608b8231a17100a2b41350fb797999a366b138b483fec7a5ceee86deff91
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
[root@bogon data]#

提示:如果已经创建了容器,将无法进行删除

⑥导入镜像

[root@bogon data]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@bogon data]# docker load < nginx.tar
2edcec3590a4: Loading layer [==================================================>]  83.86MB/83.86MB
5f5f780b24de: Loading layer [==================================================>]  61.99MB/61.99MB
4270b63061e5: Loading layer [==================================================>]  3.072kB/3.072kB
32359d2cd6cd: Loading layer [==================================================>]  4.096kB/4.096kB
4ded77d16e76: Loading layer [==================================================>]  3.584kB/3.584kB
51a4ac025eb4: Loading layer [==================================================>]  7.168kB/7.168kB
Loaded image: nginx:latest
[root@bogon data]# docker images;
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
nginx        latest    f6987c8d6ed5   6 days ago   141MB

⑦启动容器

简单启动:docker run [镜像]

# 下面是增加参启动

# --name docker启动名称

# -p 启动端口:映射端口

# -d 后台运行

[root@bogon data]# docker run --name nginx_test -p 80:80 -t -i -d nginx /bin/bash
f314411b47ff0758059401a12a46867181d47a7d51ba66dad6af27c318862c1b
[root@bogon data]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                               NAMES
f314411b47ff   nginx     "/docker-entrypoint.…"   52 seconds ago   Up 51 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx_tes

⑧查看启动镜像

[root@bogon data]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                               NAMES
de20ef1f254b   nginx     "/docker-entrypoint.…"   7 seconds ago   Up 6 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx_test

⑨进入容器

[root@bogon data]# docker attach nginx_test
root@97741828fb82:/# nginx -t   
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
正文到此结束
评论

登录后才能发表评论 登录/注册

0评论
  • 还没有评论,快来抢沙发吧!