docker虚拟化安装配置与常用命令

一、Docker安装配置

docker只有在centos6.5以上机器才能使用yum直接安装,如果其他版本需要安装centos扩展源epel。

docker官方文档说要求Linux kernel至少3.8以上,一般为centos6.5或者Ubuntu系统,

在Centos6.x系列安装docker软件,首先要关闭selinux,然后需要安装相应的epel源,如下:

sed -i '/SELINUX/s/enforcing/disabled/g' /etc/selinux/config

wget http://ftp.riken.jp/Linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm 

rpm -ivh  epel-release-6-8.noarch.rpm

Last login: Fri May 19 21:41:49 2017
[root@test ~]# sed -i '/SELINUX/s/enforcing/disabled/g' /etc/selinux/config
[root@test ~]# wget http://ftp.riken.jp/Linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm 
--2017-05-19 21:42:36--  http://ftp.riken.jp/Linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
正在解析主机 ftp.riken.jp... 134.160.38.1
正在连接 ftp.riken.jp|134.160.38.1|:80... 已连接。
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
[root@test ~]# rpm -ivh  epel-release-6-8.noarch.rpm
warning: epel-release-6-8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]
[root@test ~]# yum install  lxc  libcgroup  device-mapper-event-libs
Loaded plugins: fastestmirror
Determining fastest mirrors
epel/metalink                                                                                                                | 6.4 kB     00:00     
 * base: mirrors.btte.net
 * epel: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirror.bit.edu.cn
base                                                                                                                         | 3.7 kB     00:00   

然后安装docker:

[root@test ~]# yum install docker-io
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * epel: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirror.bit.edu.cn
Setting up Install Process
Resolving Dependencies
..........................................................................................................................
Complete!
[root@test ~]# yum install  device-mapper*  -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * epel: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirror.bit.edu.cn
Setting up Install Process

 

安装完后:

启动docker进程:/etc/init.d/docker start

查看docker进程:ps -ef |grep docker

Linux中service命令和/etc/init.d/的关系
service xxx启动 /etc/init.d/ 目录下的xxx脚本
如一个脚本名为 mysvc保存在/etc/init.d/下可以用 service mysvc 运行mysvc脚本
[root@test ~]# service docker start
Starting cgconfig service:                                 [确定]
Starting docker:	                                   [确定]
[root@test ~]# ps -ef |grep docker
root      1323     1  1 21:53 pts/0    00:00:00 /usr/bin/docker -d
root      1440  1174  0 21:54 pts/0    00:00:00 grep docker
[root@test ~]# 

Ubuntu16安装dokcer:

网站 https://get.docker.com 提供了curl-able的安装脚本install.sh,我们可以通过curl的方式进行安装docker。

安装curl:
root@meng-virtual-machine:~# apt-get update
root@meng-virtual-machine:~# sudo apt-get install curl
安装docker:
root@meng-virtual-machine:~# curl -k -sSl https://get.docker.com | sudo sh

查看docker版本:
root@meng-virtual-machine:~# docker version
Client:
 Version:      17.05.0-ce
 API version:  1.29
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 22:10:54 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.05.0-ce
 API version:  1.29 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   89658be
 Built:        Thu May  4 22:10:54 2017
 OS/Arch:      linux/amd64
 Experimental: false
验证出现以下信息安装成功。
root@meng-virtual-machine:~# docker run hello-world

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.
 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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/
卸载docker:
apt-get remove docker-engine

  

Docker简单使用:

要使用docker虚拟化,首先要去下载一个镜像,然后使用docker命令启动。

去公共仓库下载centos镜像,下载速度取决于网速。

[root@test ~]# docker pull centos
latest: Pulling from centos
5932f74ff0cd: Pull complete 
4a7b890637c2: Pull complete 
4beff0251382: Pull complete 
Digest: sha256:cb2a2bffb199880da9c69e7f647c01c720c6f95b186a86cfbd3ef168b8032074
Status: Downloaded newer image for centos:latest
[root@test ~]# 

  

二、docker常用命令

docker  version #查看版本

docker  search centos#搜索可用docker镜像

docker  images 查看当前docker所有镜像

docker  pull  centos #下载镜像

cat   centos.tar | docker import  -  centos6  #Docker导入镜像

docker  export  id  > cenos6.tar  #Docker导出镜像

docker  run   centos echo "hello word"#在docker容器中运行hello world!

docker  run  centos yum install ntpdate#在容器中安装ntpdate的程序

docker  ps -l 命令获得最后一个容器的id,docker   ps  -a查看所有的容器。

运行docker commit 提交刚修改的容器,例如:

docker  commit  2313132  centos:v1

docker run -i -t centos /bin/bash 在容器里启动一个/bin/bash shell环境,可以登录进入操作,其中-t 表示打开一个终端的意思,-i表示可以交互输入。

docker  run  -d  centos:v1  /bin/bash  ,-d表示在后台启动,以daemon方式启动。 

docker stop  id 关闭容器

docker start  id 启动某个容器

docker stop $(docker ps -a -q) 停止所有容器

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

docker  rm  id 删除容器,docker  rmi  images删除镜像

docker  run  -d  -p  80:80  -p 8022:22   centos:v2,解析:-p指定容器启动后docker上运行的端口映射及容器里运行的端口,80:80,第一个80表示docker系统上的80,第二个80表示docker虚拟机里面的端口。用户默认访问本机80端口,自动映射到容器里面的80端口。

docker  exec   -it  id  /bin/bash

 

 

原文地址:https://www.cnblogs.com/menglingqian/p/6880633.html