Docker 要求 CentOS 系統的內核版本高於 3.10 ,查看本頁面的前提條件來驗證你的CentOS 版本是否支持 Docker 。linux
經過 uname -r 命令查看你當前的內核版本docker
[root@bigdata ~]# uname -r
Docker 軟件包和依賴包已經包含在默認的 CentOS-Extras 軟件源裏,安裝命令以下:vim
[root@bigdata ~]# yum -y install docker
[root@bigdata ~]# docker version
方式一:centos
[root@bigdata ~]# service docker start Redirecting to /bin/systemctl start docker.service [root@bigdata ~]#
方式二:安全
[root@bigdata ~]# systemctl start docker.service
[root@bigdata ~]# ps aux | grep docker
默認狀況下,docker 命令會使用 Unix socket 與 Docker 引擎通信。而只有 root 用戶和 docker 組的用戶才能夠訪問 Docker 引擎的 Unix socket。出於安全考慮,通常 Linux 系統上不會直接使用 root 用戶。所以,更好地作法是將須要使用 docker 的用戶加入 docker 用戶組。bash
[root@bigdata ~]# groupadd docker [root@bigdata ~]# useradd -g docker docker
[docker@bigdata ~]$ docker run hello-world
[docker@bigdata ~]$ docker run hello-world Unable to find image 'hello-world:latest' locally Trying to pull repository docker.io/library/hello-world ... latest: Pulling from docker.io/library/hello-world 9bb5a5d4561a: Pulling fs layer /usr/bin/docker-current: error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net/registry-v2/docker/registry/v2/blobs/sha256/e3/e38bc07ac18ee64e6d59cf2eafcdddf9cec2364dfe129fe0af75f1b0194e0c96/data?Expires=1525823399&Signature=SjqbSNVW5X~uDhy9jXvuLqv22jC3auyGRx4JCRE1ceXkdh0Qpsc21VmhIXwAO6XcxwyJ1gGNVQhnJWYozOWXjysL8taJFBCxKNqAD9Cy~TCt-iMi06z9dHX6-WxxIU3WJ4LbCT7RxsWIKArTVKmPvyQdD4Djkgr~rWzoL6eyTfg_&Key-Pair-Id=APKAJECH5M7VWIS5YZ6Q: net/http: TLS handshake timeout. See '/usr/bin/docker-current run --help'. [docker@bigdata ~]$
如上圖報錯/usr/bin/docker-current: error pulling image configuration。。。app
出現這個問題,通常的緣由是沒法鏈接到 docker hub經過(使用root用戶執行如下命令):ssh
[root@bigdata ~]# cat /etc/sysconfig/docker
在文件中添加如下內容:socket
--registry-mirror=http://f2d6cb40.m.daocloud.io
重啓dockeroop
[root@bigdata ~]# service docker restart
再次運行helloworld(docker用戶)
[docker@bigdata ~]$ docker run hello-world
因爲本地沒有hello-world這個鏡像,因此會下載一個hello-world的鏡像,並在容器內運行。
從 Docker 鏡像倉庫獲取鏡像的命令是 docker pull。其命令格式爲:
docker pull [選項] [Docker Registry 地址[:端口號]/]倉庫名[:標籤]
能夠直接使用docker pull centos:7命令安裝鏡像
[docker@bigdata ~]$ docker pull centos:7
[docker@bigdata ~]$ docker image ls
一個是centos鏡像,另外一個是咱們以前使用docker run hello-world命令下載的鏡像。
鏡像(Image)和容器(Container)的關係,就像是面向對象程序設計中的 類 和 實例 同樣,鏡像是靜態的定義,容器是鏡像運行時的實體。容器能夠被建立、啓動、中止、刪除、暫停等。
有了鏡像後,咱們就可以以這個鏡像爲基礎啓動並運行一個容器。
[docker@bigdata ~]$ docker run -it --rm centos bash
docker run 就是運行容器的命令,說明一下上面用到的參數。
- -it:這是兩個參數,一個是 -i:交互式操做,一個是 -t 終端。咱們這裏打算進入 bash 執行一些命令並查看返回結果,因 此咱們須要交互式終端。
- --rm:這個參數是說容器退出後隨之將其刪除。默認狀況下,爲了排障需求,退出的容器並不會當即刪除,除非手動 docker rm。咱們這裏只是隨便執行個命令,看看結果,不須要排障和保留結果,所以使用 --rm 能夠避免浪費空間。
- centos :這是指用centos 鏡像爲基礎來啓動容器。
- bash:放在鏡像名後的是命令,這裏咱們但願有個交互式 Shell,所以用的是 bash。
[root@cb55b5f51685 /]# cat /etc/os-release
進入容器後,咱們能夠在 Shell 下操做,執行任何所需的命令。這裏,咱們執行了 cat /etc/os-release,這是 Linux 經常使用的查看當前系統版本的命令,從返回的結果能夠看到容器內是 CentOS Linux 系統。
最後咱們能夠經過 exit 退出了這個容器。
[docker@bigdata ~]$ docker system df
[docker@bigdata ~]$ docker run -it -v /home/docker/build:/root/build --privileged -h hadoop1 --name hadoop1 centos /bin/bash /usr/bin/docker-current: Error response from daemon: Conflict. The container name "/hadoop1" is already in use by container a094bdef9e1cac62a17022e568fe9b1eb021e13adf8ed2624a71be5a2e42c618. You have to remove (or rename) that container to be able to reuse that name.. See '/usr/bin/docker-current run --help'. [docker@bigdata ~]$
docker ps
: 查看當前運行的容器docker ps -a
:查看全部容器,包括中止的。
標題含義:
- CONTAINER ID:容器的惟一表示ID。
- IMAGE:建立容器時使用的鏡像。
- COMMAND:容器最後運行的命令。
- CREATED:建立容器的時間。
- STATUS:容器狀態。
- PORTS:對外開放的端口。
- NAMES:容器名。能夠和容器ID同樣惟一標識容器,同一臺宿主機上不容許有同名容器存在,不然會衝突。
使用命令中止並刪除這個容器就能夠
[docker@bigdata ~]$ docker run -it -v /home/docker/build:/root/build --privileged -h hadoop1 --name hadoop1 centos /bin/bash
以centos鏡像啓動一個容器,容器名是hadoop1,主機名是hadoop1,而且將基於容器的centos系統的/root/build目錄與本地/home/docker/build共享。
參數解釋:
- -v 表示基於容器的centos系統的/root/build目錄與本地/home/hadoop/build共享;這能夠很方便將本地文件上傳到Docker內部的centos系統;
- -h 指定主機名爲hadoop1
- –-name 指定容器名
- /bin/bash 使用bash命令
[root@hadoop1 /]# yum install vim
[root@hadoop1 /]# yum -y update
[root@hadoop1 /]# yum -y install openssh-server
[root@hadoop1 /]# yum -y install openssh-clients
編輯sshd的配置文件/etc/ssh/sshd_config,將其中的UsePAM yes改成UsePAM no
[root@hadoop1 /]# vi /etc/ssh/sshd_config
[root@hadoop1 /]# /usr/sbin/sshd -D
報錯如圖,解決方案爲:建立公私密鑰,輸入命令後,直接按兩次enter鍵確認就好了
[root@hadoop1 /]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
[root@hadoop1 /]# ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
[root@hadoop1 /]# ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key
再次啓動SSH服務
[root@hadoop1 /]# yum -y install lsof
[root@hadoop1 /]# lsof -i:22
[root@hadoop1 /]# passwd
測試
[root@hadoop1 /]# ssh localhost
上圖中能夠看到已經登陸到本機了,也就說容器中的主機擁有了ssh遠程登陸其它主機的能力,固然你也能夠登陸其餘主機。要退出的話,輸入命令exit便可。
[root@hadoop1 ~]# vi /etc/hosts
獲得容器中的主機的ip地址172.17.0.2(可能和你獲得的不同)
而後在宿主機中開啓一個新的終端輸入命令
[docker@bigdata ~]$ ssh root@172.17.0.2
[root@hadoop1 ~]# ssh-keygen -t rsa
[root@hadoop1 ~]# cd .ssh/ [root@hadoop1 .ssh]# cat id_rsa.pub >> authorized_keys
輸入完後,這時再輸入命令
[root@hadoop1 .ssh]# ssh localhost
將JDK上傳到Linux系統,,而後將其移動到/home/docker/build文件夾下面,注意:這裏須要使用root用戶
[root@bigdata docker]# mv jdk-8u73-linux-x64.tar.gz build/
進入容器裏面的/root/build文件夾下面進行查看
[root@hadoop1 /]# cd /root/build/ [root@hadoop1 build]# ls
在容器/root下面建一個apps文件夾
[root@hadoop1 ~]# mkdir apps
解壓JDK的安裝包到apps文件夾下面
[root@hadoop1 build]# tar -zxvf jdk-8u73-linux-x64.tar.gz -C /root/apps/
修改環境變量
[root@hadoop1 ~]# vi .bashrc
#JAVA export JAVA_HOME=/root/apps/jdk1.8.0_73 export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOME/bin
保存使其當即生效
[root@hadoop1 ~]# source .bashrc
基於已有的docker容器,作一新的dokcer image.
$ docker commit <container_id> <image_name>
另開一個窗口
舉例: