CentOS Docker 部署

Docker 的安装

Docker 支持以下的CentOS版本:
CentOS 7 (64-bit)
CentOS 6.5 (64-bit) 或更高的版本

前提条件

目前 CentOS 仅发行版本中的内核支持Docker
Docker 运行在CentOS 7上,要求系统为64位、系统内核版本为 3.10 以上
Docker 运行在CentOS-6.5 或更高的版本的CentOS 上,要求系统为64位、系统内核版本为2.6.32-431 或者更高版本


使用yum 安装(CentOS 7下)
Docker 要求 CentOS 系统的内核版本高于3.10,查看本页面的前提条件来验证你的CentOS版本是否支持Docker

[root@localhost ~]# uname -r 
3.10.0-693.el7.x86_64

配置安装yum 源
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

[root@localhost yum.repos.d]# yum install -y epel-release
[root@localhost yum.repos.d]# ls    
CentOS-Base.repo  docker-ce.repo  epel.repo  epel-testing.repo

安装依赖包

[root@localhost ~]# yum install -y lvm2-2.02.180-10.el7_6.3.x86_64
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * epel: mirror.xeonbd.com
Resolving Dependencies
--> Running transaction check
---> Package lvm2.x86_64 7:2.02.180-10.el7_6.3 will be installed
--> Finished Dependency Resolution

安装docker-ce (社区免费版)

[root@localhost yum.repos.d]# yum install docker-ce

启动docker 服务

[root@localhost ~]# systemctl start docker 

docker 默认需要去访问DockerHub 拉去镜像
在这里插入图片描述
这个站点在国外,如果按一般的速度拉取很慢,所以需要配置一个加速器,安装完docker-ce 后,创建文件/etc/docker/daemon.json,将以下内容编辑进去

[root@localhost ~]# cat /etc/docker/daemon.json 
{
	"registry-mirrors":["https://registry.docker-cn.com"]
}

重启docker 服务

[root@localhost ~]# systemctl restart docker 
[root@localhost ~]# systemctl enable docker  

看到以下内容就说明成功了

[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

查看 docker 信息

`[root@localhost ~]# docker version 
Client:
 Version:           18.09.0
 API version:       1.39
 Go version:        go1.10.4
 Git commit:        4d60db4
 Built:             Wed Nov  7 00:48:22 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.0
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.4
  Git commit:       4d60db4
  Built:            Wed Nov  7 00:19:08 2018
  OS/Arch:          linux/amd64
  Experimental:     false

搜索镜像
命令: docker search 镜像名称
如 : docker search mysql

[root@localhost ~]# docker search  mysql 
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                                                  MySQL is a widely used, open-source relation…   7332                [OK]                
mariadb                                                MariaDB is a community-developed fork of MyS…   2359                [OK]                
mysql/mysql-server                                     Optimized MySQL Server Docker images. Create…   540                                     [OK]
percona                                                Percona Server is a fork of the MySQL relati…   388                 [OK]                
zabbix/zabbix-server-mysql                             Zabbix Server with MySQL database support       141                                     [OK]
hypriot/rpi-mysql                                      RPi-compatible Docker Image with Mysql          100                                     

从 docker hub 上拉去镜像

[root@localhost ~]# docker image  pull nginx 
Using default tag: latest
latest: Pulling from library/nginx
f17d81b4b692: Pull complete 
82dca86e04c3: Pull complete 

查看下载的镜像

[root@localhost ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              62f816a209e6        9 days ago          109MB

启动 docker 后,会自动产生一个docker 网桥

 docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:15:4b:47:df brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever

启动一个容器

[root@localhost ~]# docker ps -a        //查看所有容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS               NAMES
a1efdc4ca35f        nginx               "/bin/bash"         About an hour ago   Exited (0) About an hour ago                       practical_bhaskara
[root@localhost ~]# docker start a1efdc4ca35f                     //启动这个容器
a1efdc4ca35f

运行一个容器,并进入到容器

[root@localhost ~]# docker run -it nginx /bin/bash 
root@a1efdc4ca35f:/#            //进入到容器后标识符会改变                   

##不建议用以上方法启动

在实际的生产环境中一般用nsenter 工具来启动容器
在 util-Linux 软件包版本中2.23+中包含nsenter 工具,如果系统中的util-linux包没有该命令,可以按照以下方法安装

wget https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz

[root@localhost opt]# ls
containerd  util-linux-2.24.tar.gz
[root@localhost opt]# tar xf util-linux-2.24.tar.gz 
[root@localhost opt]# cd util-linux-2.24
[root@localhost util-linux-2.24]# ./configure --without-ncurses 
[root@localhost util-linux-2.24]# make nsenter && cp nsenter /usr/local/bin

显示该容器第一个进行的PID可以使用如下方式

[root@localhost ~]# docker inspect -f {{.State.Pid}} a1efdc4ca35f
22955 

拿到PID 之后我们就可以使用nsenter 命令访问该容器了

[root@localhost ~]# nsenter --target 22955 --mount --uts --ipc --net --pid
root@a1efdc4ca35f:/# 

也可以直接用脚本执行更加方便

[root@localhost script]# cat pid.sh 
#!/bin/bash 
PiD=$(docker inspect -f {{.State.Pid}} $1) 
nsenter --target $PiD --mount --uts --ipc --net --pid 

[root@localhost script]# bash pid.sh a1efdc4ca35f
root@a1efdc4ca35f:/# 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值