Docker入门之CentOs6.5 Docker安装以及容器输出helloworld

  • 1.安装Docker
yum install -y docker-io

执行报错:

[root@server1 ~]# yum install -y docker-io
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.btte.net
 * extras: mirrors.btte.net
 * updates: mirrors.btte.net
base                                  | 3.7 kB     00:00     
extras                                | 3.4 kB     00:00     
updates                               | 3.4 kB     00:00     
updates/primary_db                    | 1.9 MB     00:02     
Setting up Install Process
No package docker-io available.
Error: Nothing to do

原因:我用的CentOs6.5 需要更新epel第三方软件库再安装

解决:

yum install epel-release

再次安装OK, 版本1.7.1:

Dependencies Resolved

==============================================================================
 Package                       Arch      Version            Repository   Size
==============================================================================
Installing:
 docker-io                     x86_64    1.7.1-2.el6        epel        4.6 M
Installing for dependencies:
 bridge-utils                  x86_64    1.2-10.el6         base         30 k
 libcgroup                     x86_64    0.40.rc1-23.el6    base        131 k
 lua-alt-getopt                noarch    0.7.0-1.el6        epel        6.9 k
 lua-filesystem                x86_64    1.4.2-1.el6        epel         24 k
 lua-lxc                       x86_64    1.0.9-1.el6        epel         16 k
 lxc                           x86_64    1.0.9-1.el6        epel        122 k
 lxc-libs                      x86_64    1.0.9-1.el6        epel        257 k
 rsync                         x86_64    3.0.6-12.el6       base        335 k

Transaction Summary
==============================================================================
  • 2.启动Docker
/etc/init.d/docker start 或者 service docker start

查看状态 docker启动失败:

[root@server1 ~]# service docker status
docker dead but pid file exists

查看原因:

[root@server1 ~]# tail -100 /var/log/docker 
\nThu Jul  6 09:37:50 EDT 2017\n
time="2017-07-06T09:37:50.197159063-04:00" level=warning msg="You are running linux kernel version 2.6.32-431.el6.x86_64, which might be unstable running docker. Please upgrade your kernel to 3.10.0." 
time="2017-07-06T09:37:50.214354587-04:00" level=info msg="Listening for HTTP on unix (/var/run/docker.sock)" 
/usr/bin/docker: relocation error: /usr/bin/docker: symbol dm_task_get_info_with_deferred_remove, version Base not defined in file libdevmapper.so.1.02 with link time reference
\nThu Jul  6 09:39:50 EDT 2017\n
time="2017-07-06T09:39:50.458100631-04:00" level=warning msg="You are running linux kernel version 2.6.32-431.el6.x86_64, which might be unstable running docker. Please upgrade your kernel to 3.10.0." 
/usr/bin/docker: relocation error: /usr/bin/docker: symbol dm_task_get_info_with_deferred_remove, version Base not defined in file libdevmapper.so.1.02 with link time reference

解决:
根据错误执行下面的语句

#先执行如下命令,因为yum-config-manager在yum-utils里面
yum -y install yum-utils
yum-config-manager --enable public_ol6_latest
yum install -y device-mapper-event-libs

重新启动:

[root@server1 ~]# service docker start
Starting docker:                                       [  OK  ]
[root@server1 ~]# service docker status
docker (pid  1530) is running...
  • 3.下载镜像:

查看CentOS镜像:

[root@server1 ~]# docker search centos
NAME                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                                 The official build of CentOS.                   3450      [OK]       
jdeathe/centos-ssh                     CentOS-6 6.9 x86_64 / CentOS-7 7.3.1611 x8...   75                   [OK]
consol/centos-xfce-vnc                 Centos container with "headless" VNC sessi...   27                   [OK]
nimmis/java-centos                     This is docker images of CentOS 7 with dif...   26                   [OK]
gluster/gluster-centos                 Official GlusterFS Image [ CentOS-7 +  Glu...   19                   [OK]
egyptianbman/docker-centos-nginx-php   A simple and highly configurable docker co...   9                    [OK]
torusware/speedus-centos               Always updated official CentOS docker imag...   8                    [OK]
nathonfowlie/centos-jre                Latest CentOS image with the JRE pre-insta...   6                    [OK]
centos/mariadb55-centos7                                                               5                    [OK]
darksheer/centos                       Base Centos Image -- Updated hourly             3                    [OK]
harisekhon/centos-java                 Java on CentOS (OpenJDK, tags jre/jdk7-8)       2                    [OK]
harisekhon/centos-scala                Scala + CentOS (OpenJDK tags 2.10-jre7 - 2...   2                    [OK]
blacklabelops/centos                   CentOS Base Image! Built and Updates Daily!     1                    [OK]
timhughes/centos                       Centos with systemd installed and running       1                    [OK]
vorakl/centos                          CentOS7, EPEL, tools. Updated/Tested daily!     1                    [OK]
freenas/centos                         Simple CentOS Linux interactive container       1                    [OK]
sgfinans/docker-centos                 CentOS with a running sshd and Docker           1                    [OK]
grossws/centos                         CentOS 6 and 7 base images with gosu and l...   0                    [OK]
repositoryjp/centos                    Docker Image for CentOS.                        0                    [OK]
smartentry/centos                      centos with smartentry                          0                    [OK]
vcatechnology/centos                   A CentOS Image which is updated daily           0                    [OK]
januswel/centos                        yum update-ed CentOS image                      0                    [OK]
aguamala/centos                        CentOS base image                               0                    [OK]
dmglab/centos                          CentOS with some extras - This is for the ...   0                    [OK]
kz8s/centos                            Official CentOS plus epel-release               0                    [OK]

拉取镜像:

#docker的仓库和github很像,用pull拉取centos的镜像
docker pull centos

查看本地机器上的镜像:
docker images


[root@server1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              72a210db1424        17 hours ago        192.5 MB
  • 4.使用镜像

用docker镜像执行 helloworld
docker run centos /bin/echo ‘hello world’
docker run –name lijie -it centos /bin/bash
其中 -i 容器终端保持打开 -t 开启伪终端绑定到标准输入

[root@server1 ~]# docker run centos /bin/echo 'hello world'
hello world

[root@server1 ~]# docker run --name lijie -it centos /bin/bash
[root@f6fedfb668c5 /]# ls /
anaconda-post.log  bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@f6fedfb668c5 /]# echo "hello world"
hello world
[root@f6fedfb668c5 /]# exit
exit
[root@server1 ~]# 

5.查看docker运行的进程:
docker ps -a

[root@server1 ~]# docker ps -a
CONTAINER ID        IMAGE       COMMAND                CREATED              STATUS                          PORTS   NAMES
f6fedfb668c5        centos      "/bin/bash"            About a minute ago   Exited (0) 29 seconds ago               lijie               
23fc0bfb2cb6        centos      "/bin/echo 'hello wo   About a minute ago   Exited (0) About a minute ago           gloomy_curie 

6.启动一个已经终止的容器:

docker start 容器id

7.后台运行容器:
docker run -d –name backnginx nginx

[root@server1 ~]# docker run -d --name backnginx  nginx
Unable to find image 'nginx:latest' locally   #这里在镜像里面没有,docker会自动pull一个nginx容器
latest: Pulling from nginx
1943e5bcdc2e: Pull complete 
6391f4389286: Pull complete 
687fb83caec2: Pull complete 
e3987e5b38d4: Pull complete 
a3d9ed6d7e57: Pull complete 
a36537d4476c: Pull complete 
8601894c2243: Pull complete 
427673d842a6: Pull complete 
bccedf258637: Pull complete 
11409c2f6e71: Pull complete 
Digest: sha256:87e4b276c1b87ab7a1de9df434aba1d2447bc136d767a38c56b91b7166d9bfce
Status: Downloaded newer image for nginx:latest
58f75c29cbf13dcfd5492553d0f00c1b8eb6bbf3922ddf6b7fa54c7dc08179f4

8.查看nginx容器:

[root@server1 ~]# docker ps -a
CONTAINER ID        IMAGE     COMMAND                CREATED             STATUS                       PORTS   NAMES
58f75c29cbf1        nginx     "nginx -g 'daemon of   42 seconds ago      Up 41 seconds                80/tcp  backnginx           
f6fedfb668c5        centos    "/bin/bash"            5 minutes ago       Exited (137) 3 minutes ago           lijie               
23fc0bfb2cb6        centos    "/bin/echo 'hello wo   5 minutes ago       Exited (0) 5 minutes ago             gloomy_curie

9.停止容器:

docker stop 容器id

10.删除容器:

docker rm 容器id

11.删除容器镜像:

docker rmi 镜像id

12.强制删除运行的容器:

docker rm -f 容器id

13.进入一个容器中:

#获取docker 某个容器的pid
#docker inspect --format "{{.State.Pid}}" 容器名或者容器ID

[root@server1 ~]# docker inspect --format "{{.State.Pid}}" backnginx
3410

[root@server1 ~]# nsenter --target 3410 --mount --uts --ipc --net --pid
mesg: ttyname failed: No such file or directory
root@3940205a9fd4:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

14.如果没有nsenter 命令可以yum 安装:

[root@server1 ~]# yum install -y util-linux
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值