centos7 docker安裝和使用_入門教程

原文: centos7 docker安裝和使用_入門教程

說明:本文也是參考互聯網上的文章寫的,感謝相關做者的貢獻。html

操做系統

64位CentOS Linux release 7.2.1511 (Core)linux

配置好IP:192.168.1.160docker

修改yum源

目的是提高對docker的下載速度。json

1.備份你的原鏡像文件,以避免出錯後能夠恢復。ubuntu

[root@localhost ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

2.下載新的CentOS-Base.repo 到/etc/yum.repos.d/centos

[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

3.運行yum makecache生成緩存緩存

[root@localhost ~]# yum clean all [root@localhost ~]# yum makecache

安裝Docker

[root@localhost ~]# yum -y install docker-io

要稍等幾分鐘才能安裝好。網速快的話幾十秒吧。bash

啓動Docker

[root@localhost ~]# systemctl start docker

發現會報錯:app

Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.dom

按照提示執行:

[root@localhost ~]# systemctl status docker.service

會有提示信息以下:

● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since 三 2018-05-02 23:34:46 CST; 52s ago Docs: http://docs.docker.com Process: 14416 ExecStart=/usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --seccomp-profile=/etc/docker/seccomp.json $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY $REGISTRIES (code=exited, status=1/FAILURE) Main PID: 14416 (code=exited, status=1/FAILURE) 5月 02 23:34:45 localhost.localdomain systemd[1]: Starting Docker Application Container Engine... 5月 02 23:34:45 localhost.localdomain dockerd-current[14416]: time="2018-05-02T23:34:45.527821208+08:00" level=warning msg="could not change group /var/run/...t found" 5月 02 23:34:45 localhost.localdomain dockerd-current[14416]: time="2018-05-02T23:34:45.532650572+08:00" level=info msg="libcontainerd: new containerd proce...: 14421" 5月 02 23:34:46 localhost.localdomain dockerd-current[14416]: time="2018-05-02T23:34:46.539484373+08:00" level=warning msg="overlay2: the backing xfs filesystem is ... 5月 02 23:34:46 localhost.localdomain dockerd-current[14416]: Error starting daemon: SELinux is not supported with the overlay2 graph driver on this kernel....d=false) 5月 02 23:34:46 localhost.localdomain systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE 5月 02 23:34:46 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine. 5月 02 23:34:46 localhost.localdomain systemd[1]: Unit docker.service entered failed state. 5月 02 23:34:46 localhost.localdomain systemd[1]: docker.service failed. Hint: Some lines were ellipsized, use -l to show in full.

紅色部分告訴咱們此linux的內核中的SELinux不支持 overlay2 graph driver,解決方法有兩個,要麼啓動一個新內核,要麼就在docker裏禁用selinux,設置--selinux-enabled=false。咱們採用第二種方式。

[root@localhost ~]# vi /etc/sysconfig/docker

而後將--selinux-enabled設置成false,保存並退出。

# /etc/sysconfig/docker # Modify these options if you want to change the way the docker daemon runs OPTIONS='--selinux-enabled=false --log-driver=journald --signature-verification=false'
if [ -z "${DOCKER_CERT_PATH}" ]; then DOCKER_CERT_PATH=/etc/docker fi # ......省略N行

從新啓動

[root@localhost ~]# systemctl start docker

查看版本號

[root@localhost ~]# docker -v

Docker version 1.13.1, build 774336d/1.13.1

至此docker啓動成功

修改Docker鏡像加速器

由於國外的docker鏡像訪問太慢,因此咱們須要修改爲阿里雲的docker鏡像。這樣從國內鏡像拉取速度會快點。

1.打開阿里雲docker倉庫地址https://dev.aliyun.com/search.html

2.淘寶帳號便可登陸,登陸後點擊本身的管理中心。

3.點擊管理中心左側菜單欄的「鏡像加速器」,右邊面板會有你的加速地址,面板下面有詳細設置步驟。以下圖:

[root@localhost ~]# vi /etc/docker/daemon.json

將下面整行字符拷貝進去,保存並退出。

"registry-mirrors": ["https://njrds9qc.mirror.aliyuncs.com"]

 刷新daemon

[root@localhost ~]# systemctl daemon-reload

重啓docker

[root@localhost ~]# systemctl restart docker

拉取鏡像

以hello-world爲例

[root@localhost tmp]# docker pull hello-world Using default tag: latest Trying to pull repository docker.io/library/hello-world ... latest: Pulling from docker.io/library/hello-world 9bb5a5d4561a: Pull complete Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77 Status: Downloaded newer image for docker.io/hello-world:latest

表示成功拉取

運行鏡像

[root@localhost tmp]# 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. (amd64) 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://hub.docker.com/
 For more examples and ideas, visit: https://docs.docker.com/engine/userguide/

成功運行。

簡單操做命令

鏡像

docker pull 鏡像名稱[:版本]

拉取鏡像,若是版本爲空,則拉取最新的版本

 

docker run 鏡像名稱或ID

運行鏡像,建立容器

 

docker run -it 鏡像名稱或ID

運行鏡像,建立容器,並進入該容器。

-i表示以交互模式運行容器,一般與 -t 同時使用;

-t表示爲容器從新分配一個僞輸入終端,一般與 -i 同時使用;

另外還有不少參數,可參考http://www.runoob.com/docker/docker-run-command.html

 

docker rmi 鏡像名稱或ID

根據名稱或ID刪除鏡像

容器

docker ps

列出容器

-a顯示全部的容器,包括未運行的

-q 靜默模式,只顯示容器編號,一般和-a一塊兒使用,docker ps -aq

 

docker rm 容器名稱或ID

根據名稱或者ID刪除容器,若是帶上參數-f,表示強制刪除正在運行的容器

 

docker rm $(docker ps -aq)

表示刪除全部容器

 

docker start 容器名稱或ID

根據名稱或者ID啓動容器

 

docker stop 容器名稱或ID

根據名稱或者ID中止容器

 

docker attach id

進入某個容器(使用exit退出後容器也跟着中止運行)

 

docker exec -ti id

啓動一個僞終端以交互式的方式進入某個容器(使用exit退出後容器不中止運行)

相關文章
相關標籤/搜索