天天看點

Docker docker部署以及運作你的第一個容器

安裝 Docker

本節我們将在centos7.4中安裝 Docker。

Docker 支援幾乎所有的 Linux 發行版,也支援 Mac 和 Windows。各作業系統的安裝方法可以通路:https://docs.docker.com/engine/installation/

Docker 分為開源免費的 CE(Community Edition)版本和收費的 EE(Enterprise Edition)版本。下面我們将按照文檔,通過以下步驟安裝 Docker CE 版本。

配置docker ce版本的repo源

#擴充yum功能
[[email protected] ~]# yum install -y yum-utils  

#添加docker-ce yum源   添加軟體源資訊
[[email protected] ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  

#自動選擇最快yum倉庫源
[[email protected] ~]# yum makecache fast   

#檢視目前可以安裝docker-ce版本
[[email protected] ~]# yum list docker-ce --showduplicates | sort -r
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
 * updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
 * extras: mirrors.aliyun.com
 * epel: mirrors.bfsu.edu.cn
docker-ce.x86_64            3:19.03.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.3-3.el7                     docker-ce-stable
           

使用yum源安裝docker 

[[email protected] ~]# yum install epel-release -y
[[email protected] ~]# ll /etc/yum.repos.d/epel*
-rw-r--r-- 1 root root 1050 Sep 17  2019 /etc/yum.repos.d/epel.repo
-rw-r--r-- 1 root root 1149 Sep 17  2019 /etc/yum.repos.d/epel-testing.repo


yum安裝軟體包,我這裡安裝CE版本不安裝1.13版本
[[email protected] ~]# yum install docker* -y 1.13版本

# yum -y install docker-ce-[VERSION]    //安裝指定版本的格式 ,注意3:xxx 請移除3:
[[email protected] ~]# yum install docker-ce-19.03.9-3.el7  -y

檢擦docker軟體包是否部署成功
[[email protected] ~]# rpm -qa | grep docker
[[email protected] ~]# yum list -C docker

啟動docker引擎服務,并且檢視版本
[[email protected] ~]# systemctl start docker
[[email protected] ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:46:54 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.9
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       9d988398e7
  Built:            Fri May 15 00:24:05 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

可以看到和mysql一樣C/S架構,有用戶端和服務端。通過下面socket檔案來進行本地通信
[[email protected] ~]# ps -ef | grep docker
root      23788      1  0 05:25 ?        00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
           
[[email protected] ~]# docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        13433               [OK] 


将鏡像拉取到本地
[[email protected] ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
8559a31e96f4: Downloading  1.948MB/27.1MB
8d69e59170f7: Download complete 
3f9f1ec1d262: Download complete 
d1f5ff4f210d: Download complete 
1e22bfa8652e: Download complete 

或者将nginx.tar鏡像包導入到你的鏡像清單裡面,因為拉取鏡像很慢,我将tar包上傳了
如果nginx鏡像是通過windows上傳nginx.tar,導入的nginx鏡像指令:
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[[email protected] ~]# ls
anaconda-ks.cfg  nginx.tar
[[email protected] ~]# docker load <nginx.tar 
1c95c77433e8: Loading layer  72.47MB/72.47MB
002a63507c1c: Loading layer  57.31MB/57.31MB
12fdf55172df: Loading layer  3.584kB/3.584kB
Loaded image: nginx:latest
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              5a3221f0137b        10 months ago       126MB
[[email protected] ~]# docker run -itd -p 80:80 nginx
314d1d01c94136a35dcb07016814437666331d8f7b441f5d0a1f66863848c413
[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
314d1d01c941        nginx               "nginx -g 'daemon of??   6 seconds ago       Up 4 seconds        0.0.0.0:80->80/tcp   happy_mirzakhani


或者這種方式導入鏡像也可以,但是不建議,建立容器報錯
[[email protected] ~]# cat nginx.tar | docker import - nginx:v1
sha256:0dd1a59f537a0760b91897283e7e568bbc29b3971337c31a630042a1a049fd7c
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               v1                  0dd1a59f537a        2 seconds ago       130MB
nginx               latest              b2ebe65d87c7        2 minutes ago       130MB

[[email protected] ~]# docker run -itd -p 81:80 nginx:v1
docker: Error response from daemon: No command specified.



Run,全新建立一台容器;
-i,interactive互動模式;
-t,tty打開登陸終端;
-d,detach背景運作、啟動;
-p,publish釋出端口,将主控端80(第一個)DNAT映射至容器的80;第一個80端口是主控端的端口,這個端口是主控端上面需要開啟的,如果主控端上80端口被占用了是不行的,通過DNAT映射
           
Docker docker部署以及運作你的第一個容器