Docker學習一 Docker社區版安裝

Docker社區版安裝–CENTOS 7

前言

Docker至今爲止已經非常火了,不管是運維,還是後端開發都用得到。首先安裝一版試試吧。

參考菜鳥教程進行安裝,並解決一些可能遇到的問題。

環境

  • 操作系統:CentOS 7.6
  • 操作用戶:root
  • 安裝時間:2020年5月21日

卸載舊版本

sudo yum remove docker \
         docker-client \
         docker-client-latest \
         docker-common \
         docker-latest \
         docker-latest-logrotate \
         docker-logrotate \
         docker-engine

安裝 Docker Engine-Community

使用 Docker 倉庫進行安裝

在新主機上首次安裝 Docker Engine-Community 之前,需要設置 Docker 倉庫。之後,您可以從倉庫安裝和更新 Docker。

設置倉庫

安裝所需的軟件包。yum-utils 提供了 yum-config-manager ,並且 device mapper 存儲驅動程序需要 device-mapper-persistent-data 和 lvm2。

sudo yum install  -y yum-utils \
 device-mapper-persistent-data \
 lvm2

使用以下命令來設置穩定的倉庫。

sudo yum-config-manager \
  --add-repo \
  https://download.docker.com/linux/centos/docker-ce.repo

安裝 Docker Engine-Community

安裝最新版本的 Docker Engine-Community 和 containerd:

sudo yum install docker-ce docker-ce-cli containerd.io

安裝結果

[root@gxp5 default]# yum list installed |grep docker
containerd.io.x86_64                    1.2.13-3.2.el7                 @docker-ce-stable
docker-ce.x86_64                        3:19.03.9-3.el7                @docker-ce-stable
docker-ce-cli.x86_64                    1:19.03.9-3.el7                @docker-ce-stable

啓動 Docker。

sudo systemctl start docker

通過運行 hello-world 映像來驗證是否正確安裝了 Docker Engine-Community 。

sudo docker run hello-world

WARNING: IPv4 forwarding is disabled. Networking will not work.
Hello from Docker!
This message shows that your installation appears to be working correctly.
...

安裝遇到的問題

下載hello-world失敗

[root@gxp5 docker]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
...

本地沒有hello-world,且長時間下載不下來,報超時。通過添加穩定的國內docker源解決。

  1. 在 /etc/docker/daemon.json 中寫入如下內容(如果文件不存在請新建該文件):
{  
    "registry-mirrors": [    
        "https://dockerhub.azk8s.cn",    
        "https://reg-mirror.qiniu.com",    
        "https://registry.docker-cn.com"  
    ] 
}
  1. 重啓docker
sduo systemctl restart docker

配置文件/etc/sysconfig/docker不存在

  1. 修改service文件
vim  /lib/systemd/system/docker.service

在[Service]中添加一行EnvironmentFile=/etc/sysconfig/docker

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
  1. 新增配置文件 vim /etc/sysconfig/docker
# /etc/sysconfig/docker

# Modify these options if you want to change the way the docker daemon runs
OPTIONS='-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock'
DOCKER_CERT_PATH=/etc/docker

# If you want to add your own registry to be used for docker search and docker
# pull use the ADD_REGISTRY option to list a set of registries, each prepended
# with --add-registry flag. The first registry added will be the first registry
# searched.
# ADD_REGISTRY='--add-registry registry.access.redhat.com'

# If you want to block registries from being used, uncomment the BLOCK_REGISTRY
# option and give it a set of registries, each prepended with --block-registry
# flag. For example adding docker.io will stop users from downloading images
# from docker.io
# BLOCK_REGISTRY='--block-registry'

# If you have a registry secured with https but do not have proper certs
# distributed, you can tell docker to not look for full authorization by
# adding the registry to the INSECURE_REGISTRY line and uncommenting it.
 INSECURE_REGISTRY='--insecure-registry dl.dockerpool.com:5000'

# On an SELinux system, if you remove the --selinux-enabled option, you
# also need to turn on the docker_transition_unconfined boolean.
# setsebool -P docker_transition_unconfined 1

# Location used for temporary files, such as those created by
# docker load and build operations. Default is /var/lib/docker/tmp
# Can be overriden by setting the following environment variable.
# DOCKER_TMPDIR=/var/tmp

# Controls the /etc/cron.daily/docker-logrotate cron job status.
# To disable, uncomment the line below.
# LOGROTATE=false
  1. 重啓docker
systemctl daemon-reload
systemctl restart docker
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章