Docker容器

1.容器簡介linux


1.1Linux容器docker

    Linux容器與系統其餘部分隔離開的一系列進程,從另外一個鏡像運行,並由該鏡像提供支持進程所需的所有文件。容器提供的鏡像包含了應用的全部依賴項,於是從開發到測試再到生產的整個過程當中,它都具備可移植性和一致性。json


更加詳細地來講,請您假定您在開發一個應用。您使用的是一臺筆記本電腦,並且您的開發環境具備特定的配置。其餘開發人員身處的環境配置可能稍有不一樣。您正在開發的應用依賴於您當前的配置,還要依賴於某些特定文件。與此同時,您的企業還擁有標準化的測試和生產環境,且具備自身的配置和一系列支持文件。您但願儘量多在本地模擬這些環境,而不產生從新建立服務器環境的開銷。centos

 所以,您要如何確保應用可以在這些環境中運行和經過質量檢測,而且在部署過程當中不出現使人頭疼的問題,也無需從新編寫代碼和進行故障修復?答案就是使用容器。容器能夠確保您的應用擁有必需的配置和文件,使得這些應用可以在從開發到測試、再到生產的整個流程中順利運行,而不出現任何不良問題。這樣能夠避免危機,作到皆大歡喜。bash


1.2容器與虛擬化的區別服務器

  1)虛擬化使得許多操做系統可同時在單個系統上運行ssh

  2)容器能夠共享同一個操做系統內核,將應用進程與系統其餘部分隔離開curl


2.Docker簡介ide


2.1Dockeroop

  1)容器化技術,用於支持建立和使用Linux容器

  2)開源的Docker社區致力於改進這類技術,並免費提供給全部用戶


3.Docker安裝部署


3.1環境準備

[root@localhost ~]# cat /etc/redhat-release 

CentOS Linux release 7.4.1708 (Core) 

[root@localhost ~]# uname -r

3.10.0-693.el7.x86_64

[root@localhost ~]# getenforce 

Disabled

[root@localhost ~]# systemctl status firewalld.service 

● firewalld.service - firewalld - dynamic firewall daemon

   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)

   Active: inactive (dead)

     Docs: man:firewalld(1)

[root@localhost ~]# ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 10.0.0.103  netmask 255.255.255.0  broadcast 10.0.0.255

        inet6 fe80::af24:1fe3:9292:9747  prefixlen 64  scopeid 0x20<link>

        ether 00:0c:29:bb:d5:4a  txqueuelen 1000  (Ethernet)

        RX packets 288  bytes 56978 (55.6 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 111  bytes 13567 (13.2 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        ether 00:0c:29:bb:d5:54  txqueuelen 1000  (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


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 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


3.2安裝相關依賴包

[root@localhost ~]# yum -y install yum-utils device-maper-persistent-data lvm2


3.3添加軟件源

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

[root@localhost ~]# yum makecache fast


說明:軟件源下載後,沒法安裝docker,請將docker源下載到Windows上,再上傳到linux中


3.4安裝Docker

[root@localhost ~]# yum install -y docker-ce-17.09.0.ce


3.5啓動Docker

[root@localhost ~]# systemctl start docker.service


4.Docker命令操做


4.1查看docker相關信息

[root@localhost ~]# docker version 

Client:

 Version:      17.09.0-ce

 API version:  1.32

 Go version:   go1.8.3

 Git commit:   afdb6d4

 Built:        Tue Sep 26 22:41:23 2017

 OS/Arch:      linux/amd64


Server:

 Version:      17.09.0-ce

 API version:  1.32 (minimum version 1.12)

 Go version:   go1.8.3

 Git commit:   afdb6d4

 Built:        Tue Sep 26 22:42:49 2017

 OS/Arch:      linux/amd64

 Experimental: false


4.2docker鏡像加速

[root@localhost ~]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://14c15248.m.daocloud.io

docker version >= 1.12

{"registry-mirrors": ["http://14c15248.m.daocloud.io"]}

Success.

You need to restart docker to take effect: sudo systemctl restart docker 

[root@localhost ~]# systemctl restart docker.service 


4.3運行docker

[root@localhost ~]# docker run -it centos


4.4搜索官方倉庫鏡像

[root@localhost ~]# docker search centos


4.5根據鏡像名拉取鏡像

[root@localhost ~]# docker pull centos


4.6查看當前主機鏡像列表

[root@localhost ~]# docker images 


4.7導出鏡像

[root@localhost ~]# docker image save centos > docker-centos.tar.gz


4.8刪除鏡像

[root@localhost ~]# docker image rm centos:latest


4.9導入鏡像

[root@localhost ~]# docker image load -i docker-centos.tar.gz


4.10建立容器

[root@localhost ~]# docker create centos:later /bin/bash

[root@localhost ~]# docker start stupefied_nobel


4.11查看正在運行的容器

[root@localhost ~]# docker ps


4.12查看全部容器

[root@localhost ~]# docker ps -a


4.13刪除全部容器

[root@localhost ~]# docker rm -f `docker ps -a -q`


5.手動將容器保存爲鏡像


5.1啓動一個centos6.9的鏡像

[root@localhost ~]# docker pull centos:6.9

[root@localhost ~]# docker run -it -p 1022:22 centos:6.9 /bin/bash

[root@58201b2b92b ~]# yum install -y openssh-server

[root@58201b2b92b ~]# echo "root:123456" | chpasswd

[root@58201b2b92b ~]# /etc/init.d/sshd start


5.2將容器提交爲鏡像

[root@localhost ~]# docker commit brave_mcclintock centos-ssh


6.docker 倉庫


6.1建立倉庫

docker run -d -p 5000:5000 --restart=always --name registry -v

/opt/myregistry:/var/lib/registry registry


6.2修改配置文件,使之支持http

[root@localhost ~]# cat /etc/docker/daemon.json

{

    "registry-mirrors": ["https://registry.docker-cn.com"],

    "insecure-registries": ["10.0.0.103:5000"]

}


6.3重啓docker 

[root@localhost ~]# systemctl restart docker.service


6.4修改鏡像標籤

[root@localhost ~]# docker tag busybox:latest 10.0.0.103:5000/clsn/busybox:1.0

[root@localhost ~]# docker images

REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE

centos6-ssh                                latest              3c2b1e57a0f5      18 hours ago       393MB

httpd                                           2.4                 2e202f453940       6 days ago          179MB

10.0.0.100:5000/clsn/busybox    1.0                 5b0d59026729      8 days ago          1.15MB


6.5將新打標籤的鏡像上傳到倉庫

[root@localhost ~]# docker push   10.0.0.100:5000/clsn/busybox

相關文章
相關標籤/搜索