雲原生應用2:docker在線/離線安裝與基礎用法

本篇博客能讓你瞭解到docker的安裝與基礎語法

1.docker安裝與基礎用法

依賴環境:Linux kernel 3.10+
下面使用 Centos7.6演示:查看內核與系統版本:uname -a ,cat /etc/centos-release
在這裏插入圖片描述

2.安裝docker
  • 1.在阿里雲開源鏡像倉庫找到docker-ce庫,https://mirrors.aliyun.com/docker-ce/linux/centos/ ,找到 docker-ce.repo ,並右鍵複製鏈接地址,在centos上 cd /etc/yum.repos.d/ , wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  • 2.執行yum repolist,再安裝docker-ce,yum install docker-ce,中間步驟輸入 y 同意
    如果出現 container-selinux >= 2.9問題無法安裝,請先下載 container-selinux
    下載:wget http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-2.107-3.el7.noarch.rpm
    安裝:rpm -ivh container-selinux-2.107-3.el7.noarch.rpm
  • 3.設置docker鏡像加速器,dockerhub在國外,因此docker鏡像的下載很慢,需要設置加速地址,有很多第三方鏡像加速地址,如:阿里雲鏡像加速器、163、docker-cn等,設置加速器:1.mkdir -p /etc/docker , 2.vi /etc/docker/daemon.json , 3.輸入 {“registry-mirrors” : [“https://xxx.mirror.aliyuncs.com”,“https://registry.docker-cn.com”]} 裏面是數組,可以輸入多個
  • 4.啓動docker,systemctl start docker.service ,開機啓動,systemctl enable docker.service
  • 5.查看docker版本,docker version , docker info,可看到運行中的容器,docker 版本,以及剛剛設置的鏡像加速器地址,可看到機器上有一個已停止的容器
    在這裏插入圖片描述
3.docker基礎用法

幫助文檔可讓你快速瞭解它,輸入docker,或 docker image --help 查看幫助文檔
安裝一個nginx來快速瞭解docker的使用,以 nginx 爲例進行說明:

  • 1.查詢鏡像,有2種方式,一是使用 docker search nginx 命令查詢,二是在https://hub.docker.com/ 頁面上輸入nginx查詢
  • 2.下載鏡像:docker pull nginx:latest,或指定標籤版本 tag
  • 3.查看鏡像列表:docker image ls
  • 4.啓動容器:docker run -it -d --name nginx1 -p 8080:80 nginx:latest,運行一個有交互窗口的容器,名稱叫nginx1,且容器的80端口映射宿主機的8080端口,啓動後可通過 http://宿主機IP:8080 訪問nginx
  • 5.查看容器:docker ps ,docker ps -a(可查看已停止的容器)
  • 6.停止容器:docker stop nginx1
  • 7.刪除容器:docker rm nginx1,再通過docker ps -a 就看不到停止的容器了
  • 8.查看容器詳細信息:docker inspect nginx1
  • 9.查看容器中日誌信息:docker container logs nginx1

上面命令截圖
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
訪問剛剛運行的 nginx 容器
在這裏插入圖片描述
docker運行狀態圖:
在這裏插入圖片描述

阿里雲鏡像加速器,需要先註冊,然後在 容器鏡像服務–鏡像加速器中查看,如下圖:
在這裏插入圖片描述

下一篇:雲原生應用3:docker鏡像管理

配置一個 jenkins 容器:vi start_jenkins.sh,內容如下:

docker run -d --name jenkins -u root -it --restart=always
-p 7080:8080 -p 50000:50000
-p 8190:8190
-p 8191:8191
–privileged=true
-v /var/jenkins_home:/var/jenkins_home
-v /home:/home
-v /usr/bin/docker:/usr/bin/docker
-v /var/run/docker.sock:/var/run/docker.sock
-v /usr/lib64/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7
-v /etc/localtime:/etc/localtime -v /etc/timezone:/etc/timezone
jenkinsci/blueocean

-v /etc/localtime:/etc/localtime -v /etc/timezone:/etc/timezone 這行特別重要,表示容器的時間使用宿主機的時間,否則你應用內容的時間會少8個小時

docker也可以離線安裝
離線包下載地址tg:https://download.docker.com/linux/static/stable/x86_64/
離線包下載地址rpm:https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

上傳文件,解壓

[root@dev1 opt]# tar -xvf docker-18.06.1-ce.tgz
docker/
docker/docker-containerd
docker/docker-proxy
docker/docker
docker/docker-runc
docker/dockerd
docker/docker-containerd-ctr
docker/docker-containerd-shim
docker/docker-init

複製 [root@dev1 opt] cp docker/* /usr/bin/
設置開機自啓動,vim /etc/systemd/system/docker.service ,內容如下:

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target

添加文件權限並啓動docker:chmod +x /etc/systemd/system/docker.service
重載unit配置文件:systemctl daemon-reload
啓動Docker(2種方式): systemctl start docker / dockerd &
設置開機自啓:systemctl enable docker.service

測試
[root@centos75-1 ~]# docker -v

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章