(一):centos7 安裝 docker-ce

簡介

描述

Docker 是一個(gè)開源的應(yīng)用容器引擎,讓開發(fā)者可以打包他們的應(yīng)用以及依賴包到一個(gè)可移植的容器中,然后發(fā)布到任何流行的 Linux 機(jī)器上,也可以實(shí)現(xiàn)虛擬化。容器是完全使用沙箱機(jī)制,相互之間不會(huì)有任何接口。

相關(guān)組件

  • [x] docker images
  • docker images 就是一個(gè)只讀的模板,一個(gè) image 可以包含一個(gè) ubuntu的操作系統(tǒng),里面安裝了具體的應(yīng)用程序。images 可以用來創(chuàng)建 docker containers。
  • [x] docker registries
  • docker 倉庫,它有公有倉庫(Docker Hub)和私有倉庫 2種形式,他們都可以用來讓你上傳和下載 images。
  • [x] docker containers
  • docker 容器,容器是從 image 鏡像創(chuàng)建的。它可以被啟動(dòng)、開始、停止、刪除。
    每個(gè)容器都是相互隔離的、安全的平臺(tái)。

安裝

宿主機(jī)環(huán)境信息

[root@node146 ~]# cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 7.3 (Maipo)

centos和redhat版本對(duì)應(yīng)表

版本 平臺(tái) RHEL版本 發(fā)布日期 RHEL 發(fā)布日期
7-1406 x86-64 7.0 2014-07-07[81] 2014-06-10[82]
7-1503 x86-64 7.1 2015-03-31[83] 2015-03-06[84]
7-1503-AArch64 AArch64 7.1 2015-08-04[85] 2015-03-06[86]
7-1503-i386 i386 7.1 2015-10-12[87] 2015-03-06[88]
7-1511 x86-64 7.2 2015-12-14[89] 2015-11-19[90]
7-1611 x86-64 7.3 2016-12-12[91] 2016-11-03[92]
7-1708 x86-64 7.4 2017-09-13[93] 2017-08-01[94]
7-1804 x86-64 7.5 2018-05-10[95] 2018-04-10[96]

參考安裝手冊(cè)

https://docs.docker.com/install/linux/docker-ce/centos/#os-requirements

具體步驟

To install Docker CE, you need a maintained version of CentOS 7

安裝前準(zhǔn)備

刪除舊的docker

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine docker-ce -y
刪除廢棄的docker信息
  • [x] 主要避免不同版本信息的干擾
rm -rf /var/lib/docker

實(shí)際安裝:

  • [x] 兩種安裝方式,一種通過倉庫,一種通過安裝包安裝

使用REPOSITORY倉庫安裝

1. 安裝需要的包

$ sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

2.安裝倉庫地址

$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

3. 可選,可以開啟非穩(wěn)定版本

##開啟
sudo yum-config-manager --enable docker-ce-edge
sudo yum-config-manager --enable docker-ce-test
##廢棄非穩(wěn)定版本
sudo yum-config-manager --disable docker-ce-edge
sudo yum-config-manager --disable docker-ce-test

4. 查看可用版本

yum list docker-ce --showduplicates | sort -r

docker-ce.x86_64            18.06.0.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            18.03.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.0.ce-1.el7.centos             docker-ce-stable

5.安裝

sudo yum install docker-ce-18.03.1.ce -y

6.啟動(dòng)

sudo systemctl start docker
[root@node146 ~]# docker version
Client:
 Version:      18.03.1-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   9ee9f40
 Built:        Thu Apr 26 07:20:16 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
  Version:      18.03.1-ce
  API version:  1.37 (minimum version 1.12)
  Go version:   go1.9.5
  Git commit:   9ee9f40
  Built:        Thu Apr 26 07:23:58 2018
  OS/Arch:      linux/amd64
  Experimental: false

7 升級(jí)

  • [x] 卸載重新安裝

使用安裝包安裝:

1.包下載

##包下載地址:
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

##docker-ce-18.03.1.ce-1
wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-18.03.1.ce-1.el7.centos.x86_64.rpm
## 需要注意17.03.2 需要安裝兩個(gè)包

##docker-ce-selinux-17.03.2.ce-1.el7
 wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch.rpm
 
##docker-ce-17.03.2.ce-1.el7
wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-17.03.2.ce-1.el7.centos.x86_64.rpm

2 安裝

## 18.03.1.ce-1
sudo yum install  docker-ce-18.03.1.ce-1.el7.centos.x86_64.rpm
## 17.03.2.ce
sudo yum install docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch.rpm -y
sudo yum install  docker-ce-17.03.2.ce-1.el7.centos.x86_64.rpm -y
  • [x] 可以直接使用url安裝
  • [x] 如安裝17.03.2
yum install -y https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch.rpm https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-17.03.2.ce-1.el7.centos.x86_64.rpm

3. 啟動(dòng)

sudo systemctl start docker

4 升級(jí)

安裝之后
yum -y upgrade  docker-ce-18.03.1.ce-1.el7.centos.x86_64.rpm

系統(tǒng)關(guān)閉selinux

查看SELinux狀態(tài):

/usr/sbin/sestatus -v     
##如果SELinux status參數(shù)為enabled即為開啟狀態(tài)
SELinux status: enabled
##也可以用這個(gè)命令檢查
[root@node146 ~]# getenforce
Permissive          

關(guān)閉SELinux:

1、臨時(shí)關(guān)閉(不用重啟機(jī)器):

##設(shè)置SELinux 成為permissive模式
setenforce 0                  

##設(shè)置SELinux 成為enforcing模式
setenforce 1 

2、修改配置文件需要重啟機(jī)器:

修改/etc/selinux/config 文件
將SELINUX=enforcing改為SELINUX=disabled
重啟機(jī)器即可

加入開機(jī)自啟

systemctl enable docker

常用命令

命令提示

[root@node146 harbor]# docker  --help

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
  -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
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  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
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

docker的啟動(dòng)、停止、重啟

service docker restart
service docker stop
service docker start

創(chuàng)建容器


## 語法
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

## 樣例:
docker run -it -v /local_path:/container_path --name nginx nginx:latest /bin/bash

OPTIONS說明:

-a stdin: 指定標(biāo)準(zhǔn)輸入輸出內(nèi)容類型,可選 STDIN/STDOUT/STDERR 三項(xiàng);

-d: 后臺(tái)運(yùn)行容器,并返回容器ID;

-i: 以交互模式運(yùn)行容器,通常與 -t 同時(shí)使用;

-p: 端口映射,格式為:主機(jī)(宿主)端口:容器端口

-t: 為容器重新分配一個(gè)偽輸入終端,通常與 -i 同時(shí)使用;

-v:是掛在宿機(jī)目錄, /local_path是宿機(jī)目錄,/container_path是當(dāng)前docker容器的目錄,宿機(jī)目錄必須是絕對(duì)的。

--name="nginx-lb": 為容器指定一個(gè)名稱;

--dns 8.8.8.8: 指定容器使用的DNS服務(wù)器,默認(rèn)和宿主一致;

--dns-search example.com: 指定容器DNS搜索域名,默認(rèn)和宿主一致;

-h "mars": 指定容器的hostname;

-e username="ritchie": 設(shè)置環(huán)境變量;

--env-file=[]: 從指定文件讀入環(huán)境變量;

--cpuset="0-2" or --cpuset="0,1,2": 綁定容器到指定CPU運(yùn)行;

-m :設(shè)置容器使用內(nèi)存最大值;

--net="bridge": 指定容器的網(wǎng)絡(luò)連接類型,支持 bridge/host/none/container: 四種類型;

--link=[]: 添加鏈接到另一個(gè)容器;

--expose=[]: 開放一個(gè)端口或一組端口;

查看容器列表

## 啟動(dòng)的容器列表
docker ps

## 所有容器列表
docker ps -a

啟動(dòng)、停止、重啟某個(gè)docker 容器

docker start nginx
docker stop nginx
docker restart nginx

查看容器日志

docker logs -f nginx

刪除容器

docker rm nginx

## 強(qiáng)制刪除
docker rm -f nginx

刪除所有容器

docker rm $(docker ps -a -q)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容