(一):centos7 安装 docker-ce

96
木夕月_fc7b
2018.09.19 15:17* 字数 582

简介

描述

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。

相关组件

  • [x] docker images
  • docker images 就是一个只读的模板,一个 image 可以包含一个 ubuntu的操作系统,里面安装了具体的应用程序。images 可以用来创建 docker containers。
  • [x] docker registries
  • docker 仓库,它有公有仓库(Docker Hub)和私有仓库 2种形式,他们都可以用来让你上传和下载 images。
  • [x] docker containers
  • docker 容器,容器是从 image 镜像创建的。它可以被启动、开始、停止、删除。
    每个容器都是相互隔离的、安全的平台。

安装

宿主机环境信息

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

centos和redhat版本对应表

版本 平台 RHEL版本 发布日期 RHEL 发布日期
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]

参考安装手册

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

具体步骤

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

安装前准备

删除旧的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

实际安装:

  • [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. 可选,可以开启非稳定版本

##开启
sudo yum-config-manager --enable docker-ce-edge
sudo yum-config-manager --enable docker-ce-test
##废弃非稳定版本
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.启动

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

  • [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 需要安装两个包

##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. 启动

sudo systemctl start docker

4 升级

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

系统关闭selinux

查看SELinux状态:

/usr/sbin/sestatus -v     
##如果SELinux status参数为enabled即为开启状态
SELinux status: enabled
##也可以用这个命令检查
[root@node146 ~]# getenforce
Permissive          

关闭SELinux:

1、临时关闭(不用重启机器):

##设置SELinux 成为permissive模式
setenforce 0                  

##设置SELinux 成为enforcing模式
setenforce 1 

2、修改配置文件需要重启机器:

修改/etc/selinux/config 文件
将SELINUX=enforcing改为SELINUX=disabled
重启机器即可

加入开机自启

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的启动、停止、重启

service docker restart
service docker stop
service docker start

创建容器


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

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

OPTIONS说明:

-a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;

-d: 后台运行容器,并返回容器ID;

-i: 以交互模式运行容器,通常与 -t 同时使用;

-p: 端口映射,格式为:主机(宿主)端口:容器端口

-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;

-v:是挂在宿机目录, /local_path是宿机目录,/container_path是当前docker容器的目录,宿机目录必须是绝对的。

--name="nginx-lb": 为容器指定一个名称;

--dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;

--dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致;

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

-e username="ritchie": 设置环境变量;

--env-file=[]: 从指定文件读入环境变量;

--cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定CPU运行;

-m :设置容器使用内存最大值;

--net="bridge": 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;

--link=[]: 添加链接到另一个容器;

--expose=[]: 开放一个端口或一组端口;

查看容器列表

## 启动的容器列表
docker ps

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

启动、停止、重启某个docker 容器

docker start nginx
docker stop nginx
docker restart nginx

查看容器日志

docker logs -f nginx

删除容器

docker rm nginx

## 强制删除
docker rm -f nginx

删除所有容器

docker rm $(docker ps -a -q)
docker
Web note ad 1