Docker在CentOS上的安装和基本使用

 一、 Docker的安装

CentOS6x上的安装:

  官方文档要求Linux kernel至少3.8以上,且docker只能运行在64位的系统中。CentOS6x的内核版本为2.6,因此必须要先升级内核。

1. 升级内核(带aufs模块)

cd /etc/yum.repos.d 

wget http://www.hop5.in/yum/el6/hop5.repo

yum install kernel-ml-aufs kernel-ml-aufs-devel

修改grub的主配置文件/etc/grub.conf,设置default=0,表示第一个title下的内容为默认启动的kernel(一般新安装的内核在第一个位置),将1改为0

重启,再查看内核

[root@wls12c ~]$ uname -r
3.10.5-3.el6.x86_64

查看是否支持aufs

[root@wls12c ~]$ grep aufs /proc/filesystems
nodev    aufs

2. 首先关闭selinux:

setenforce 0
sed -i '/^SELINUX=/c\SELINUX=disabled' /etc/selinux/config

3. 在Fedora EPEL源中已经提供了docker-io包,下载安装epel:

rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
sed -i 's/^mirrorlist=https/mirrorlist=http/' /etc/yum.repos.d/epel.repo

4、安装docker-io:

yum -y install docker-io

  CentOS7 上安装:

yum -y install docker

5. 启动docker

[root@wls12c ~]$ service docker start
Starting docker:                                       [  OK  ]

6. 检查是否安装成功

docker run hello-world
 1 [root@app-01 ~]# docker run hello-world
 2 Unable to find image 'hello-world:latest' locally
 3 Trying to pull repository docker.io/library/hello-world ... 
 4 latest: Pulling from docker.io/library/hello-world
 5 78445dd45222: Pull complete 
 6 Digest: sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
 7 
 8 Hello from Docker!
 9 This message shows that your installation appears to be working correctly.
10 
11 To generate this message, Docker took the following steps:
12  1. The Docker client contacted the Docker daemon.
13  2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
14  3. The Docker daemon created a new container from that image which runs the
15     executable that produces the output you are currently reading.
16  4. The Docker daemon streamed that output to the Docker client, which sent it
17     to your terminal.
18 
19 To try something more ambitious, you can run an Ubuntu container with:
20  $ docker run -it ubuntu bash
21 
22 Share images, automate workflows, and more with a free Docker ID:
23  https://cloud.docker.com/
24 
25 For more examples and ideas, visit:
26  https://docs.docker.com/engine/userguide/

 

二、 docker的一些基本操作:

1、启动docker

[root@wls12c ~]$ service docker start
Starting docker:                                       [  OK  ]

2、 查看docker版本

[root@wls12c ~]$ docker version
Client version: 1.7.1
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 786b29d/1.7.1
OS/Arch (client): linux/amd64
Server version: 1.7.1
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 786b29d/1.7.1
OS/Arch (server): linux/amd64

 3、 查看命令帮助

2.3.1、直接输入docker命令来查看所有的选项和指令。

2.3.2 查看某一个命令的用法,以pull为例:

  docker pull --help

4 搜索镜像

  docker search centos

5. 下载镜像

  docker pull 镜像名:标签,如果不写标签,默认的标签是latest,以下载cirrOS为例

[root@wls12c ~]$ docker pull cirros
latest: Pulling from cirros
34fa5867b8b0: Pull complete 
c42d375c72b4: Pull complete 
500e7eced838: Pull complete 
d8ca8144dee3: Pull complete 
d8ca8144dee3: Pulling fs layer 
Digest: sha256:5926aabfd4f1a9b3289457cffdd060899ce4b77549203fb747f6c5c6efad23a8
Status: Downloaded newer image for cirros:latest

6. 查看安装的镜像:

  docker images [NAME]

[root@wls12c ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              d83a55af4e75        3 weeks ago         196.7 MB
cirros              latest              d8ca8144dee3        7 months ago        7.735 MB
[root@wls12c ~]$ docker images cirros
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
cirros              latest              d8ca8144dee3        7 months ago        7.735 MB

7. 运行镜像

docker run IMAGE [COMMAND] [ARG...]

[root@wls12c ~]$ docker run cirros echo hello,cirros
hello,cirros

8 查看容器

 查看所有容器:   docker ps -a

 查看最近生成的容器:docker ps -l

 查看正在运行的容器:docker ps 

9、显示容器的标准输出

  docker logs CONTAINERID(无需拷贝完整的id,一般写最开始的三至四个字符即可)

[root@wls12c ~]$ docker ps -l
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS                     PORTS               NAMES
2b65fe9b7678        cirros              "echo hello,cirros"   4 minutes ago       Exited (0) 4 minutes ago                       stupefied_wilson    
[root@wls12c ~]$ docker logs 2b65
hello,cirros

10、运行镜像,并进入容器

[root@app-01 ~]# docker run -i -t centos /bin/bash
[root@b27ef2fce5c9 /]# ls
anaconda-post.log  dev  home  lib64       media  opt   root  sbin  sys  usr
bin                etc  lib   lost+found  mnt    proc  run   srv   tmp  var

11. 提交容器

docker commit CONTAINERID [REPOSITORY[:TAG]]

 REPOSITORY参数可以是新的镜像名字,也可以是旧的镜像名;如果和旧的镜像名和TAG都相同,会覆盖掉旧的镜像。

[root@wls12c ~]$ docker ps -l
CONTAINER ID        IMAGE               COMMAND                CREATED              STATUS                      PORTS               NAMES  
591ae5cc3f4c        centos              "yum -y install lrzs   14 minutes ago       Exited (0) 14 minutes ago                       focused_morse       
 
[root@wls12c ~]$ docker commit 591 centos:lrzsz
ae1dac60b320cfc77744573edab145b80705551fbcfaf5aaef8a4e5debb3890a
[root@wls12c ~]$ docker images centos
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              lrzsz               ae1dac60b320        20 seconds ago      293.2 MB
centos              latest              d83a55af4e75        3 weeks ago         196.7 MB
[root@wls12c ~]$ docker run centos:lrzsz rz

11. 停止正在运行的容器

docker stop CONTAINERID

默认等待10秒钟再杀死指定容器。可以使用-t参数来设置等待时间

[root@wls12c ~]$ docker run centos sleep 20&
[1] 10160
[root@wls12c ~]$ docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
b69a612f14fc        centos              "sleep 20"          10 seconds ago      Up 9 seconds                            sick_mestorf        
[root@wls12c ~]$ docker stop b69a612f14fc
b69a612f14fc
[1]+  Done                    docker run centos sleep 20

12. 查看容器或镜像的详细信息

docker inspect CONTAINERID|IMAGE

[root@wls12c ~]$ docker inspect centos:lrzsz

13. 删除容器

 docker rm CONTAINERID

查看所有容器ID:docker ps -a -q     

删除所有的容器:docker rm $(docker ps -a -q)

[root@wls12c ~]$ docker rm $(docker ps -a -q)
b69a612f14fc
043d2f0a77f0
3b28f2609a26
64aabb9149f5
1212b3e2e0e1
e8c645df28d8
591ae5cc3f4c
5a4f19dc7c1b
8698516cb9c4
2b65fe9b7678
[root@wls12c ~]$ docker ps -a -q
[root@wls12c ~]$ 

14、删除镜像:

docker rmi IMAGE

一次性删除所有镜像: docker rmi $(docker images -q)

[root@wls12c ~]$ docker images centos
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              lrzsz               ae1dac60b320        2 hours ago         293.2 MB
centos              latest              d83a55af4e75        3 weeks ago         196.7 MB
[root@wls12c ~]$ docker rmi centos:lrzsz
Untagged: centos:lrzsz
Deleted: ae1dac60b320cfc77744573edab145b80705551fbcfaf5aaef8a4e5debb3890a
[root@wls12c ~]$ docker images centos
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              d83a55af4e75        3 weeks ago         196.7 MB

15. 查看docker的信息

[root@wls12c ~]$ docker info
Containers: 0
Images: 8
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 8
 Dirperm1 Supported: false
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 3.10.5-3.el6.x86_64
Operating System: <unknown>
CPUs: 2
Total Memory: 7.69 GiB
Name: wls12c
ID: G7L3:57DV:67IG:P4JP:C6TI:ZV5J:G73H:Q4PY:TSHQ:O5WN:AOWT:FKUC
WARNING: No swap limit support

16、启动一个停止的容器

docker start CONTAINERID

[root@wls12c ~]$ docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
74f1f1cc8c07        centos              "/bin/bash"         35 seconds ago      Exited (0) 11 seconds ago                       ecstatic_hodgkin    
[root@wls12c ~]$ docker start 74f1
74f1

17、attach到运行中的容器

docker attach CONTAINERID

[root@wls12c ~]$ docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
74f1f1cc8c07        centos              "/bin/bash"         About a minute ago   Up 22 seconds                           ecstatic_hodgkin    
[root@wls12c ~]$ docker attach 74f1

[root@74f1f1cc8c07 /]# ls
anaconda-post.log  dev  home  lib64       media  opt   root  sbin  sys  usr
bin                etc  lib   lost+found  mnt    proc  run   srv   tmp  var

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值