Docker three common ways of installing Linux environment

Docker is an open source application container engine, and follow Apache2.0 agreement based on open source Go language.
Here Insert Picture Description
Docker allows developers to package their applications and dependencies to a lightweight, portable container and then posted to any popular Linux machine, can be virtualized.

The container is full use of the sandbox mechanism will not have any interface between each other (similar to the iPhone app), more important is the performance overhead is very low container.

Docker的应用场景:
Web 应用的自动化打包和发布。

自动化测试和持续集成、发布。

在服务型环境中部署和调整数据库或其他的后台应用。

从头编译或者扩展现有的OpenShift或Cloud Foundry平台来搭建自己的PaaS环境。

Docker 的优点


1、简化程序:
Docker 让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,
便可以实现虚拟化。
Docker改变了虚拟化的方式,使开发者可以直接将自己的成果放入Docker中进行管理。
方便快捷已经是 Docker的最大优势,过去需要用数天乃至数周的	
任务,在Docker容器的处理下,只需要数秒就能完成。

2、避免选择恐惧症:
如果你有选择恐惧症,还是资深患者。Docker 帮你	打包你的纠结!比如 Docker 镜像;
Docker 镜像中包含了运行环境和配置,所以 Docker 可以简化部署多种应用实例工作。
比如 Web 应用、后台应用、数据库应用、大数据应用比如 Hadoop 集群、消息队列等等都可以打包成一个镜像部署。

3、节省开支:
一方面,云计算时代到来,使开发者不必为了追求效果而配置高额的硬件,
Docker 改变了高性能必然高价格的思维定势。
Docker 与云的结合,让云空间得到更充分的利用。不仅解决了硬件管理的问题,也改变了虚拟化的方式。

目前,CentOS 仅发行版本中的内核支持 Docker。

Docker 运行在 CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上。

Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位、系统内核版本为 2.6.32-431 或者更高版本。

1) CentOS releases only

Currently, CentOS kernel supports only the release of Docker.

Docker running on CentOS 7, the system of claim 64, kernel version 3.10 or more.

Docker running on CentOS-6.5 or higher version of the CentOS, it requires 64-bit system, or kernel version 2.6.32-431 later.

[root@devops ~]# uname -r
3.10.0-327.el7.x86_64
[root@devops ~]# 

  1. Installation and start docker
yum install -y epel-release
yum install docker-io # 安装docker
# 配置文件 /etc/sysconfig/docker

chkconfig docker on  # 加入开机启动
service docker start # 启动docker服务

# 基本信息查看
docker version # 查看docker的版本号,包括客户端、服务端、依赖的Go等
docker info # 查看系统(docker)层面信息,包括管理的images, containers数等
docker pull centos 下载
docker images [ centos ] 查看
docker run -i -t centos /bin/bash


The second method

Docker CE 即社区免费版,Docker EE 即企业版,强调安全,但需付费使用。

本文介绍 Docker CE 的安装使用。

移除旧的版本:

$ 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

安装一些必要的系统工具:

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

添加软件源信息:
使用阿里云的镜像源 这个我最近刚测试了 一切正常
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

更新 yum 缓存:

sudo yum makecache fast

安装 Docker-ce:

sudo yum -y install docker-ce

启动 Docker 后台服务

sudo systemctl start docker


[root@devops ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 
Loaded plugins: fastestmirror, langpacks
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@devops ~]# yum install docker-ce
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * epel: mirror01.idc.hinet.net
 * extras: ap.stykers.moe
 * updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 3:18.09.6-3.el7 will be installed
--> Processing Dependency: container-selinux >= 2.9 for package: 3:docker-ce-18.09.6-3.el7.x86_64
--> Processing Dependency: containerd.io >= 1.2.2-3 for package: 3:docker-ce-18.09.6-3.el7.x86_64
--> Processing Dependency: docker-ce-cli for package: 3:docker-ce-18.09.6-3.el7.x86_64
--> Running transaction check
---> Package container-selinux.noarch 2:2.99-1.el7_6 will be installed
---> Package containerd.io.x86_64 0:1.2.5-3.1.el7 will be installed
---> Package docker-ce-cli.x86_64 1:18.09.6-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================
 Package                            Arch                    Version                            Repository                         Size
=======================================================================================================================================
Installing:
 docker-ce                          x86_64                  3:18.09.6-3.el7                    docker-ce-stable                   19 M
Installing for dependencies:
 container-selinux                  noarch                  2:2.99-1.el7_6                     extras                             39 k
 containerd.io                      x86_64                  1.2.5-3.1.el7                      docker-ce-stable                   22 M
 docker-ce-cli                      x86_64                  1:18.09.6-3.el7                    docker-ce-stable                   14 M

Transaction Summary
=======================================================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 55 M
Installed size: 236 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): container-selinux-2.99-1.el7_6.noarch.rpm                                                                |  39 kB  00:00:00     
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/containerd.io-1.2.5-3.1.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for containerd.io-1.2.5-3.1.el7.x86_64.rpm is not installed
(2/4): containerd.io-1.2.5-3.1.el7.x86_64.rpm                                                                   |  22 MB  00:00:17     
(3/4): docker-ce-18.09.6-3.el7.x86_64.rpm                                                                       |  19 MB  00:00:20     
(4/4): docker-ce-cli-18.09.6-3.el7.x86_64.rpm                                                                   |  14 MB  00:00:07     
---------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                  2.2 MB/s |  55 MB  00:00:25     
Retrieving key from https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
Importing GPG key 0x621E9F35:
 Userid     : "Docker Release (CE rpm) <[email protected]>"
 Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
 From       : https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 2:container-selinux-2.99-1.el7_6.noarch                                                                             1/4 
  Installing : 1:docker-ce-cli-18.09.6-3.el7.x86_64                                                                                2/4 
  Installing : containerd.io-1.2.5-3.1.el7.x86_64                                                                                  3/4 
  Installing : 3:docker-ce-18.09.6-3.el7.x86_64                                                                                    4/4 
  Verifying  : containerd.io-1.2.5-3.1.el7.x86_64                                                                                  1/4 
  Verifying  : 3:docker-ce-18.09.6-3.el7.x86_64                                                                                    2/4 
  Verifying  : 1:docker-ce-cli-18.09.6-3.el7.x86_64                                                                                3/4 
  Verifying  : 2:container-selinux-2.99-1.el7_6.noarch                                                                             4/4 

Installed:
  docker-ce.x86_64 3:18.09.6-3.el7                                                                                                     

Dependency Installed:
  container-selinux.noarch 2:2.99-1.el7_6       containerd.io.x86_64 0:1.2.5-3.1.el7       docker-ce-cli.x86_64 1:18.09.6-3.el7      

Complete!
[root@devops ~]# 


The third method: Use a script to install Docker

1、使用 sudo 或 root 权限登录 Centos。

2、确保 yum 包更新到最新。
$ sudo yum update

3、执行 Docker 安装脚本。

$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh
执行这个脚本会添加 docker.repo 源并安装 Docker。

4、启动 Docker 进程。

sudo systemctl start docker


镜像加速

鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,我们可以需要配置加速器来解决,我使用的是网易的镜像地址:http://hub-mirror.c.163.com。

新版的 Docker 使用 /etc/docker/daemon.json(Linux) 或者 %programdata%\docker\config\daemon.json(Windows) 来配置 Daemon。

请在该配置文件中加入(没有该文件的话,请先建一个):
{
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}

  1. Using the acquired image of the container
# 搜索镜像
docker search <image> # 在docker index中搜索image
# 下载镜像
docker pull <image>  # 从docker registry server 中下拉image
# 查看镜像 
    docker images: # 列出images
    docker images -a # 列出所有的images(包含历史)
    docker rmi  <image ID># 删除一个或多个image
# 使用镜像创建容器
docker run -i -t sauloal/ubuntu14.04
docker run -i -t sauloal/ubuntu14.04 /bin/bash # 创建一个容器,让其中运行 bash 应用,退出后容器关闭
docker run -itd --name centos_aways --restart=always centos #创建一个名称centos_aways的容器,自动重启
# --restart参数:always始终重启;on-failure退出状态非0时重启;默认为,no不重启

# 查看容器
    docker ps :列出当前所有正在运行的container
    docker ps -l :列出最近一次启动的container
    docker ps -a :列出所有的container(包含历史,即运行过的container)
    docker ps -q :列出最近一次运行的container ID
# 再次启动容器
    docker start/stop/restart <container> #:开启/停止/重启container
    docker start [container_id] #:再次运行某个container (包括历史container)
#进入正在运行的docker容器
    docker exec -it [container_id] /bin/bash
    docker run -i -t -p <host_port:contain_port> #:映射 HOST 端口到容器,方便外部访问容器内服务,host_port 可以省略,省略表示把 container_port 映射到一个动态端口。

# 删除容器
    docker rm <container...> #:删除一个或多个container
    docker rm `docker ps -a -q` #:删除所有的container
    docker ps -a -q | xargs docker rm #:同上, 删除所有的container

Guess you like

Origin blog.csdn.net/qq_28513801/article/details/93381492