1. 程式人生 > >(一)Docker的安裝部署

(一)Docker的安裝部署

文章目錄

1 Docker安裝

Docker 版本:
Docker從1.13版本之後採用時間線的方式作為版本號,分為社群版CE企業版EE

  • 社群版(CE)是免費提供給個人開發者和小型團體使用的
  • 企業版(EE)會提供額外的收費服務,比如經過官方測試認證過的基礎設施、容器、外掛等

由於Docker安裝需要,最好使用64位的作業系統且核心版本大於等於3.10
Docker的安裝部署很簡單,可以先獲取到我們需要的docker-ce源,而後直接yum安裝,這裡介紹docker-ce的安裝。

wget -q https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce -y

2 Docker映象加速

docker映象加速器:

  1. docker cn(docker在中國官方的加速站點,效果一般)
  2. 阿里雲加速器(需要註冊個阿里雲賬號)
  3. 中科大加速器

加速器配置檔案:

/etc/docker/daemon.json

配置加速器:

# 這裡我用的docker-cn,阿里雲加速器需要自己去註冊個賬號,然後阿里雲會分配專用加速器
[[email protected]
~]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://registry.docker-cn.com"] } [[email protected] ~]# systemctl start docker.service

3 Docker常用操作命令

命令 作用
docker search 在docker hub中查詢映象是否存在
docker pull 從倉庫中pull映象
docker images 列出本地映象
docker create 建立一個容器
docker start 開啟一個或多個處於停止狀態的容器
docker run 指定命令執行一個新容器,若此容器在本地images中沒有,會從docker hub中pull
docker attach 進入到一個容器中
docker ps 列出所有狀態的容器
docker logs 從容器中獲取日誌
docker restart 重啟一個容器
docker stop 停止一個容器
docker kill kill一個或多個正在執行的容器
docker rm 刪除一個或多個容器

幫助

[[email protected] ~]# docker --help 
[[email protected] ~]# docker image --help

檢視版本

[[email protected] ~]# docker version
Client:
 Version:           18.06.1-ce
 API version:       1.38
 Go version:        go1.10.3
 Git commit:        e68fc7a
 Built:             Tue Aug 21 17:23:03 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.1-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       e68fc7a
  Built:            Tue Aug 21 17:25:29 2018
  OS/Arch:          linux/amd64
  Experimental:     false

更詳細的資訊

[[email protected] ~]# docker info

pull一個映象

[[email protected] ~]# docker image pull nginx:1.14-alpine
[[email protected] ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.14-alpine         14d4a58e0d2e        7 weeks ago         17.4MB
[[email protected] ~]# docker image ls nginx

刪除映象

[[email protected] ~]# docker image rm busybox
[[email protected] ~]# docker rmi busybox

建立容器

[[email protected] ~]# docker run --name b1 -it busybox:latest
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # pwd
/
/ # ls /bin

啟動httpd

/ # mkdir /data/html/ -p
/ # vi /data/html/index.html
Busybox httpd server
/ # httpd -f -h /data/html/

[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
009a12f4defc        busybox:latest      "sh"                6 minutes ago       Up 6 minutes                            b1 
[[email protected] ~]# docker inspect b1 | grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",
[[email protected] ~]# curl 172.17.0.2
Busybox httpd server

如果在docker busybox shell中按終止就會連線不上

[[email protected] ~]# curl 172.17.0.2
curl: (7) Failed connect to 172.17.0.2:80; Connection refused

啟動容器

[[email protected] ~]# docker start -ai b1
[[email protected] ~]# docker container start -ai b1

執行nginx

[[email protected] ~]# docker run --name web1 -d nginx:1.14-alpine 
[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
c859e333c65c        nginx:1.14-alpine   "nginx -g 'daemon of…"   About a minute ago   Up 59 seconds       80/tcp              web1
[[email protected] ~]# docker inspect web1 | grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",
[[email protected] ~]# curl 172.17.0.2

啟動一個沒有映象的redis,只需要把版本號寫對即可

[[email protected] ~]# docker run --name kvstor1 -d redis:4-alpine
[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
e7ca0d93c496        redis:4-alpine      "docker-entrypoint.s…"   5 seconds ago       Up 3 seconds        6379/tcp            kvstor1
c859e333c65c        nginx:1.14-alpine   "nginx -g 'daemon of…"   4 minutes ago       Up 3 minutes        80/tcp              web1

[[email protected] ~]# docker exec -it kvstor1 /bin/sh 
/data # ps
PID   USER     TIME  COMMAND
    1 redis     0:00 redis-server
   18 root      0:00 /bin/sh
   23 root      0:00 ps
/data # netstat -tnl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      
tcp        0      0 :::6379                 :::*                    LISTEN      
/data # redis-cli
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> exit
/data # exit

檢視容器日誌

[[email protected] ~]# docker logs web1
172.17.0.1 - - [02/Nov/2018:01:12:42 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"