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


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

操作系統

64位CentOS Linux release 7.2.1511 (Core)

配置好IP:192.168.1.160

修改yum源

目的是提升對docker的下載速度。

1.備份你的原鏡像文件,以免出錯后可以恢復。

[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/

[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

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

啟動Docker

[root@localhost ~]# systemctl start docker

發現會報錯:

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

按照提示執行:

[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退出后容器不停止運行)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM