Docker:Docker安装及启动一个CentOS容器

一:背景:

1:公司近期准备使用Docker来部署工程,趁着机会了解下。

2:十月一之后我把自己在大学期间的笔记本给刷成了centos7,大体的配置是,酷睿i5,单核,8g运存,40g硬盘。刷机过程是痛苦的,相关资料也不多,跌跌撞撞也成功了,具体就不说了。

二:开始安装

1:Docker 要求 Centos 系统的内核版本要大于3.10,所以首先确认自己的机器能不能安装docker。

在机器中执行以下命令:

[root@02a854a7db8d /]#  uname -r
3.10.0-862.14.4.el7.x86_64
[root@02a854a7db8d /]# 

2:使用root登录,或者提供给一个账号sudo权限,应该都是可以的

先更新yum:

[root@localhost ~]# yum update -y

Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.njupt.edu.cn
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
......

其中-y代表自动回复yes,以防止这样

......
Install              ( 2 Dependent packages)
Upgrade  65 Packages

Total download size: 59 M
Is this ok [y/d/N]: 

经过一大串爽歪歪的linux信息刷屏过后,就可以正式开始安装docker了

[root@localhost ~]# yum install docker -y
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.njupt.edu.cn
 * updates: mirrors.aliyun.com
Resolving Dependencies
......

PS:

Docker从1.13版本之后采用时间线的方式作为版本号,分为社区版CE和企业版EE。

社区版是免费提供给个人开发者和小型团体使用的,企业版会提供额外的收费服务,比如经过官方测试认证过的基础设施、容器、插件等。

社区版按照stable和edge两种方式发布,每个季度更新stable版本,如17.06,17.09;每个月份更新edge版本,如17.09,17.10。

我现在的安装方式并不是最新的安装方式,而是Docker分版之前的最后一个版本。可以用:

[root@localhost ~]# docker --version
Docker version 1.13.1, build 07f3374/1.13.1

接下来启动docker

systemctl status docker.service

完成后

[root@localhost Desktop]# docker search centos
INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/centos                             The official build of CentOS.                   5018      [OK]       
docker.io   docker.io/ansible/centos7-ansible            Ansible on Centos7                              119                  [OK]
docker.io   docker.io/jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x...   102                  [OK]
docker.io   docker.io/consol/centos-xfce-vnc             Centos container with "headless" VNC sessi...   73                   [OK]
docker.io   docker.io/imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              48                   [OK]
docker.io   docker.io/centos/mysql-57-centos7            MySQL 5.7 SQL database server                   43                   
docker.io   docker.io/tutum/centos                       Simple CentOS docker image with SSH access      43                   
docker.io   docker.io/gluster/gluster-centos             Official GlusterFS Image [ CentOS-7 +  Glu...   37                   [OK]
docker.io   docker.io/openshift/base-centos7             A Centos7 derived base image for Source-To...   36                   
docker.io   docker.io/centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relationa...   35                   

查找镜像这个命令连接一直不稳定,大家可以百度下有什么好办法,我也没解决,

然后下载需要的版本,本次我下载和我本机一样的版本

[root@localhost Desktop]# docker pull centos:7.5.1804
Trying to pull repository docker.io/library/centos ... 
7.5.1804: Pulling from docker.io/library/centos
7dc0dca2b151: Pull complete 
Digest: sha256:adbf1369c8dbeaa2bf9df0b891394e3ca09b5272a2fb54e0fadc6b14dd93fcad
Status: Downloaded newer image for docker.io/centos:7.5.1804

可以查看已下载的镜像:

[root@localhost Desktop]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
docker.io/wordpress   latest              67daae3fda61        7 days ago          408 MB
docker.io/mysql       latest              f991c20cb508        2 weeks ago         486 MB
docker.io/centos      7.5.1804            76d6bc25b8a5        8 weeks ago         200 MB
[root@localhost Desktop]# 

然后是启动这个镜像:

[root@localhost Desktop]# docker run  -itd --privileged -p 20010:22 --name="centos"  76d6bc25b8a5    /usr/sbin/init
8f9fbda9bb48172ff9ea02942f7bc7165849518549fb687099e09e7a7d873233
[root@localhost Desktop]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMES
8f9fbda9bb48        76d6bc25b8a5        "/usr/sbin/init"    5 seconds ago       Up 4 seconds        0.0.0.0:20010->22/tcp   centos

docker的run命令大家可以查一下,需要注意的是如果命令中

docker run -itd --privileged -p 20010:22 --name=“centos” 76d6bc25b8a5 /usr/sbin/init

没有下划线的部分,等你运行systemctl时会报这个错误:

Failed to get D-Bus connection: Operation not permitted

这个的解释可以查看这个文章:https://blog.csdn.net/zhenliang8/article/details/78330658

剩下的进入这个容器:

[root@localhost Desktop]# docker exec -it centos bash
[root@8f9fbda9bb48 /]# 
[root@8f9fbda9bb48 /]# 
[root@8f9fbda9bb48 /]# ll
total 0
lrwxrwxrwx.   1 root root    7 May 31  2018 bin -> usr/bin
drwxr-xr-x.  20 root root 3460 Dec  6 15:06 dev
drwxr-xr-x.   1 root root   52 Dec  6 15:06 etc
drwxr-xr-x.   2 root root    6 Apr 11  2018 home
lrwxrwxrwx.   1 root root    7 May 31  2018 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 May 31  2018 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 Apr 11  2018 media
drwxr-xr-x.   2 root root    6 Apr 11  2018 mnt
drwxr-xr-x.   2 root root    6 Apr 11  2018 opt
dr-xr-xr-x. 231 root root    0 Dec  6 15:06 proc
dr-xr-x---.   2 root root  114 May 31  2018 root
drwxr-xr-x.  14 root root  300 Dec  6 15:06 run
lrwxrwxrwx.   1 root root    8 May 31  2018 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root    0 Dec  6 14:24 sys
drwxrwxrwt.   7 root root  140 Dec  6 15:06 tmp
drwxr-xr-x.  13 root root  155 May 31  2018 usr
drwxr-xr-x.   1 root root   44 Dec  6 15:06 var

成功!

接下来可以安装各种软件,及安装ssh后,就可以通过ssh登陆了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值