Docker容器技術介紹(一) --- Docker安裝和簡單使用


容器技術被認為是未來部署軟件的主流趨勢之一,高性能,輕量級及跨平台是容器技術的主要特征,傳統的應用部署過程大概是:下載軟件->編譯安裝->啟動軟件,比如現在要安裝 LNMP 軟件,就得先下載Nginx+Mysql+PHP,然后一個個安裝,這對於一兩台服務器時還能接受,要是面對成千上萬的服務器集群,還不得累死嗎,有沒有辦法把這幾個軟件打包成一個軟件一次性安裝呢,容器的出現使得很好的解決了這類問題。


什么是容器?


以上是摘自Docker官網的一段話,應該說是比較權威的解釋:容器以一種特定的格式來打包軟件,這種格式可以各自獨立的運行在同一個操作系統之上,不像虛擬機,容器不會捆綁一個完整的操作系統,而僅僅打包軟件運行所需要的依賴庫和設置。這樣可以實現一個高效、輕量級、自給自足的系統,並且可以保證軟件總是以相同的方式運行,而不管它被部署在哪里。

從上可知,容器是一種打包應用的方式,可以幫你打包應用中的所有軟件和軟件所依賴的環境,並且可以實現跨平台部署。相比虛擬機,容器更高效和輕量化,一台服務器運行1000個虛擬機基本是難以想象的,但IBM的服務器據稱已經在一台服務器上運行萬級的容器。


為什么使用容器?

Docker是什么? docker是容器領導者。


Docker是世界領先的軟件容器平台,在進行多人協作開發時,開發者可以使用Docker來消除所謂“我這里運行是好的”(works on my machine)問題,運維人員使用Docker來並行的運行和管理應用來獲得更優計算密度,基於各自獨立的容器。企業使用Docker來建立敏捷的交付方式,實現更快的、更安全的和更自信的交付新功能而不管是Linux或Windos服務器。


Docker安裝:

Centos 6.x

[root@localhost ~]# yum install epel-release  
[root@localhost ~]# yum install docker-io  
[root@localhost ~]# service docker start  
輸入docker version 驗證安裝是否成功
[root@localhost ~]# docker version  
Client version: 1.7.1  
Client API version: 1.19  
Go version (client): go1.4.2  
Git commit (client): 786b29d/1.7.1  
OS/Arch (client): linux/amd64  
Server version: 1.7.1  
Server API version: 1.19  
Go version (server): go1.4.2  
Git commit (server): 786b29d/1.7.1  
OS/Arch (server): linux/amd64  

Centos 7.x

最新版的Docker需要64位CentOS 7系統,並且分社區版(CE)和企業版(EE),一般選擇免費的社區版即可

1.卸載舊版本(如果安裝了的話)

[root@localhost ~]# yum remove docker docker-common docker-selinux docker-engine  
Loaded plugins: fastestmirror  
No Match for argument: docker  
No Match for argument: docker-common  
No Match for argument: docker-selinux  
No Match for argument: docker-engine  
No Packages marked for removal  

2.安裝

最簡單的安裝方式是直接 yum install:

[root@localhost ~]# yum install docker  
如果想要安裝最新的穩定版,可以使用下面的安裝方式:
[root@localhost software]# wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-17.06.2.ce-1.el7.centos.x86_64.rpm  
[root@localhost software]# ls  
docker-ce-17.06.2.ce-1.el7.centos.x86_64.rpm  
[root@localhost software]# yum install docker-ce-17.06.2.ce-1.el7.centos.x86_64.rpm   

3.啟動Docker服務

[root@localhost software]# systemctl start docker  
[root@localhost software]# ps -aux | grep docker  
root     23624  2.0  2.5 434500 25768 ?        Ssl  06:43   0:00 /usr/bin/dockerd  
root     23627  0.1  0.7 263940  7544 ?        Ssl  06:43   0:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc  
root     23744  0.0  0.0 112660   972 pts/0    S+   06:43   0:00 grep --color=auto docker  

可以看到Docker在后台啟動了兩個守護進程:dockerd 和 docker-containerd


Docker的三個基本概念:鏡像、容器、倉庫

鏡像相當於創建容器的一個模具,一個說明書,而容器就是一個實實在在運行的鏡像實例,可以用可執行文件和進程來比喻鏡像和容器的關系,而倉庫就是用來存放鏡像的,相當於git倉庫,用來存放不同版本的鏡像。

 

Docker安裝和簡單使用

安裝運行Nginx

/*docker search 命令可以搜索鏡像,默認從官方鏡像庫hub.docker.com搜索*/
[root@localhost ~]# docker search nginx      
NAME                                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED  
nginx                                                  Official build of Nginx.                        6903      [OK]         
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker c...   1130                 [OK]  
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable ...   446                  [OK]  
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as...   227                  [OK]  
kong                                                   Open-source Microservice & API Management ...   115       [OK]         
webdevops/php-nginx                                    Nginx with PHP-FPM                              90                   [OK]  
kitematic/hello-world-nginx                            A light-weight nginx container that demons...   86                     
bitnami/nginx                                          Bitnami nginx Docker Image                      36                   [OK]  
linuxserver/nginx                                      An Nginx container, brought to you by Linu...   24                     
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          16                   [OK]  
tobi312/rpi-nginx                                      NGINX on Raspberry Pi / armhf                   11                   [OK]  
wodby/drupal-nginx                                     Nginx for Drupal                                8                    [OK]  
webdevops/nginx                                        Nginx container                                 8                    [OK]  
.....  

/* pull 鏡像 */

[root@localhost software]# docker pull nginx    
latest: Pulling from nginx  
1cb018da208f: Pull complete   
c1a0d2b79b3f: Pull complete   
0aec54b378f5: Pull complete   
b599d18a520b: Pull complete   
a1cfc1b806a4: Pull complete   
c2504687d157: Pull complete   
a00416541f84: Pull complete   
6adf11c406b6: Pull complete   
9daddd1b8b0f: Pull complete   
036477bc0d5a: Pull complete   
Digest: sha256:248f3c1a01b35a098c85b31c356787068b1c1adbbd2f902fb2d6f49b95fd380f  
Status: Downloaded newer image for nginx:latest  
/* 列出本地鏡像 */
[root@localhost software]# docker images   
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE  
nginx               latest              da5939581ac8        2 days ago          108MB  
/* 啟動一個容器,並把容器中的80端口映射到宿主機的8080端口 -d表示讓容器在后台運行 */
[root@localhost software]# docker run -d -p 8080:80 nginx       
9af52c8b4dab91643860a7355acb1f04013ed5fd8d0b313568322f26affa2c40
/* 列出當前正在運行的容器 */
[root@localhost software]# docker ps  
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                  NAMES  
9af52c8b4dab        nginx               "nginx -g 'daemon ..."   About a minute ago   Up About a minute   0.0.0.0:8080->80/tcp   xenodochial_volhard
/* 可以看到上面docker ps的信息沒有顯示全,有的被截斷了 可以在命令加上 --no-trunc 參數強制完全顯示 */
[root@localhost ~]# docker ps --no-trunc     
CONTAINER ID                                                       IMAGE               COMMAND                    CREATED             STATUS              PORTS                  NAMES  
724db6813f511ade01cb907ec82008a8930e4db0275cbaf36f6c8503a0cec7c9   nginx               "nginx -g 'daemon off;'"   16 seconds ago      Up 15 seconds       0.0.0.0:8080->80/tcp   goofy_morse   
/* 測試nginx是否正常啟動 */
[root@localhost software]# curl '127.0.0.1:8080'     
<!DOCTYPE html>  
<html>  
<head>  
<title>Welcome to nginx!</title>  
<style>  
    body {  
        width: 35em;  
        margin: 0 auto;  
        font-family: Tahoma, Verdana, Arial, sans-serif;  
    }  
</style>  
...  
/* 停止容器運行 */
[root@localhost software]# docker stop 9af52c8b4dab        
9af52c8b4dab  
[root@localhost software]# docker ps  
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
/* -a 參數會列出所有的容器,包括已停止的 */
[root@localhost ~]# docker ps -a   
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                     PORTS               NAMES  
724db6813f51        nginx               "nginx -g 'daemon of   5 minutes ago       Exited (0) 6 seconds ago                       goofy_morse   
/* docker rm 刪除容器 */
[root@localhost ~]# docker rm 724db6813f51   
724db6813f51  
[root@localhost ~]# docker ps -a  
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
/* docker rmi 刪除鏡像 */
[root@localhost ~]# docker images  
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE  
ubuntu              14.04               b44ce450cb60        12 days ago         188 MB  
nginx               latest              036477bc0d5a        13 days ago         108.3 MB  
[root@localhost ~]# docker rmi 036477bc0d5a    
Untagged: nginx:latest  
Deleted: 036477bc0d5a32c5618eb350669ebadcf082c41f1be3b0141d22284742bc796f  
Deleted: 9daddd1b8b0faf3af09a3b25704ac2cb742a9a31114c0773c69f745364ac9569  
Deleted: 6adf11c406b68fe96f4e5cbb5f8baf7123f8df13f77cda9d2439e8e3c994fc5b  
Deleted: a00416541f8403247a87f49310e7ae687397bacd613b055a644aafedd4c0cbe1  
Deleted: c2504687d1573cf99eb64283cfa4c160d4e2d15befbd617f5551e5a02242513e  
Deleted: a1cfc1b806a41781e27bef106fb38e49f8d158253d318e032927d507c0285a37  
Deleted: b599d18a520b37f6e0a587485e54dcc141c3a181f25b651fd45d071670b61b72  
Deleted: 0aec54b378f58193513ddf12e1f855d44fb11ccf5810bd93687f6e13d107e8a0  
Deleted: c1a0d2b79b3f6e909c6c621d4e33a3bda0ac46560d18602ca7ff3367360251e7  
Deleted: 1cb018da208f97b77e79f5d42a29a05246afae8f7072683d93e06576af32b470  
[root@localhost ~]# docker images  
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE  
ubuntu              14.04               b44ce450cb60        12 days ago         188 MB  

只輸入docker可以查看docker支持的所有命令,輸入docker COMMAND --help可以查看特定命令的幫助

[root@localhost software]# docker  
  
Usage:  docker COMMAND  
  
A self-sufficient runtime for containers  
  
Options:  
      --config string      Location of client config files (default "/root/.docker")  
  -D, --debug              Enable debug mode  
      --help               Print usage  
  -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:  
  config      Manage Docker configs  
  container   Manage containers  
  image       Manage images  
  network     Manage networks  
  node        Manage Swarm nodes  
  plugin      Manage plugins  
  secret      Manage Docker secrets  
  service     Manage services  
  stack       Manage Docker stacks  
  swarm       Manage Swarm  
  system      Manage Docker  
  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  
.....  
[root@localhost software]# docker run --help  
  
Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]  
  
Run a command in a new container  
  
Options:  
      --add-host list                  Add a custom host-to-IP mapping (host:ip)  
  -a, --attach list                    Attach to STDIN, STDOUT or STDERR  
      --blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)  
      --blkio-weight-device list       Block IO weight (relative device weight) (default [])  
      --cap-add list                   Add Linux capabilities  
      --cap-drop list                  Drop Linux capabilities  
      --cgroup-parent string           Optional parent cgroup for the container  
      --cidfile string                 Write the container ID to the file  
      --cpu-period int                 Limit CPU CFS (Completely Fair Scheduler) period  
      --cpu-quota int                  Limit CPU CFS (Completely Fair Scheduler) quota  
      --cpu-rt-period int              Limit CPU real-time period in microseconds  
      --cpu-rt-runtime int             Limit CPU real-time runtime in microseconds  
  -c, --cpu-shares int                 CPU shares (relative weight)  
      --cpus decimal                   Number of CPUs  
      --cpuset-cpus string             CPUs in which to allow execution (0-3, 0,1)  
......  

Docker的簡單使用就介紹到這里,想更深入了解Docker技術歡迎關注后續文章!

That‘s it ^^


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM