docker快速搭建實驗

1.安裝與配置 Docker
安裝 Docker
首先安裝 Docker 必要依賴包
[root@VM_1_64_centos ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@VM_1_64_centos ~]#
[root@VM_1_64_centos ~]#
[root@VM_1_64_centos ~]#
[root@VM_1_64_centos ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
Updated:
device-mapper-persistent-data.x86_64 0:0.8.5-1.el7
lvm2.x86_64 7:2.02.185-2.el7_7.2
yum-utils.noarch 0:1.1.31-52.el7

Dependency Updated:
device-mapper.x86_64 7:1.02.158-2.el7_7.2
device-mapper-event.x86_64 7:1.02.158-2.el7_7.2
device-mapper-event-libs.x86_64 7:1.02.158-2.el7_7.2
device-mapper-libs.x86_64 7:1.02.158-2.el7_7.2
lvm2-libs.x86_64 7:2.02.185-2.el7_7.2
python-urlgrabber.noarch 0:3.10-9.el7
rpm.x86_64 0:4.11.3-40.el7
rpm-build-libs.x86_64 0:4.11.3-40.el7
rpm-libs.x86_64 0:4.11.3-40.el7
rpm-python.x86_64 0:4.11.3-40.el7
yum.noarch 0:3.4.3-163.el7.centos

Complete!
[root@VM_1_64_centos ~]#

由於自帶 yum 沒有 Docker-CE 所以我們需要先增加 docker repo:
[root@VM_1_64_centos ~]# yum search all | grep docker
[root@VM_1_64_centos ~]#

[root@VM_1_64_centos ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@VM_1_64_centos ~]#

用 yum 安裝 Docker:
[root@VM_1_64_centos ~]# yum install -y docker-ce
Installed:
docker-ce.x86_64 3:19.03.4-3.el7

Dependency Installed:
audit-libs-python.x86_64 0:2.8.5-4.el7
checkpolicy.x86_64 0:2.5-8.el7
container-selinux.noarch 2:2.107-3.el7
containerd.io.x86_64 0:1.2.10-3.2.el7
docker-ce-cli.x86_64 1:19.03.4-3.el7
libcgroup.x86_64 0:0.41-21.el7
libseccomp.x86_64 0:2.3.1-3.el7
libsemanage-python.x86_64 0:2.5-14.el7
policycoreutils-python.x86_64 0:2.5-33.el7
python-IPy.noarch 0:0.75-6.el7
setools-libs.x86_64 0:3.3.8-4.el7

Dependency Updated:
audit.x86_64 0:2.8.5-4.el7
audit-libs.x86_64 0:2.8.5-4.el7
libselinux.i686 0:2.5-14.1.el7
libselinux.x86_64 0:2.5-14.1.el7
libselinux-python.x86_64 0:2.5-14.1.el7
libselinux-utils.x86_64 0:2.5-14.1.el7
libsemanage.x86_64 0:2.5-14.el7
libsepol.i686 0:2.5-10.el7
libsepol.x86_64 0:2.5-10.el7
policycoreutils.x86_64 0:2.5-33.el7
selinux-policy.noarch 0:3.13.1-252.el7.1
selinux-policy-targeted.noarch 0:3.13.1-252.el7.1

Complete!

安裝成功後查看版本
[root@VM_1_64_centos ~]# docker -v
Docker version 19.03.4, build 9013bf583a
[root@VM_1_64_centos ~]#

設置開機自啓動
[root@VM_1_64_centos ~]# systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@VM_1_64_centos ~]#

配置 Docker
因爲國內訪問 Docker Hub 較慢, 可以使用騰訊雲提供的國內鏡像源, 加速訪問 Docker Hub
依次執行以下命令

[root@VM_1_64_centos ~]# echo "OPTIONS='--registry-mirror=https://mirror.ccs.tencentyun.com'" >> /etc/sysconfig/docker
[root@VM_1_64_centos ~]# cat /etc/sysconfig/docker
OPTIONS='--registry-mirror=https://mirror.ccs.tencentyun.com'
[root@VM_1_64_centos ~]#

重啟修改過的daemon
[root@VM_1_64_centos ~]# systemctl daemon-reload
[root@VM_1_64_centos ~]# systemctl restart docker
[root@VM_1_64_centos ~]#
[root@VM_1_64_centos ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2019-11-12 18:32:11 CST; 7s ago
Docs: https://docs.docker.com
Main PID: 9286 (dockerd)
Memory: 41.1M
CGroup: /system.slice/docker.service
└─9286 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/cont...

Nov 12 18:32:10 VM_1_64_centos dockerd[9286]: time="2019-11-12T18:32:10.2274...c
Nov 12 18:32:10 VM_1_64_centos dockerd[9286]: time="2019-11-12T18:32:10.2274...c
Nov 12 18:32:10 VM_1_64_centos dockerd[9286]: time="2019-11-12T18:32:10.4271..."
Nov 12 18:32:10 VM_1_64_centos dockerd[9286]: time="2019-11-12T18:32:10.8039..."
Nov 12 18:32:10 VM_1_64_centos dockerd[9286]: time="2019-11-12T18:32:10.9335..."
Nov 12 18:32:10 VM_1_64_centos dockerd[9286]: time="2019-11-12T18:32:10.9419...2
Nov 12 18:32:10 VM_1_64_centos dockerd[9286]: time="2019-11-12T18:32:10.9421...4
Nov 12 18:32:10 VM_1_64_centos dockerd[9286]: time="2019-11-12T18:32:10.9422..."
Nov 12 18:32:11 VM_1_64_centos systemd[1]: Started Docker Application Contai....
Nov 12 18:32:11 VM_1_64_centos dockerd[9286]: time="2019-11-12T18:32:11.1588..."
Hint: Some lines were ellipsized, use -l to show in full.
[root@VM_1_64_centos ~]#

2.Docker 的簡單操作
下載鏡像
下載一個官方的 CentOS 鏡像到本地
[root@VM_1_64_centos ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
729ec3a6ada3: Pull complete
Digest: sha256:f94c1d992c193b3dc09e297ffd54d8a4f1dc946c37cbeceb26d35ce1647f88d9
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

下載好的鏡像就會出現在鏡像列表裏

[root@VM_1_64_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0f3e07c0138f 5 weeks ago 220MB

運行容器
這時我們可以在剛纔下載的 CentOS 鏡像生成的容器內操作了。
生成一個 centos 鏡像爲模板的容器並使用 bash shell
[root@VM_1_64_centos ~]# docker run -it centos /bin/bash
[root@649840f41e77 /]#

這個時候可以看到命令行的前端已經變成了 [root@(一串 hash Id)] 的形式, 這說明我們已經成功進入了 CentOS 容器。
在容器內執行任意命令, 不會影響到宿主機, 如下

[root@649840f41e77 /]# mkdir -p /data/simple_docker
[root@649840f41e77 /]#

可以看到 /data 目錄下已經創建成功了 simple_docker 文件夾
[root@649840f41e77 /]# ls -l /data
total 4
drwxr-xr-x 2 root root 4096 Nov 12 10:34 simple_docker
[root@649840f41e77 /]#

退出容器
[root@649840f41e77 /]# exit
exit
[root@VM_1_64_centos ~]#

查看宿主機的 /data 目錄, 並沒有 simple_docker 文件夾, 說明容器內的操作不會影響到宿主機
[root@VM_1_64_centos ~]# ll /data
total 0
[root@VM_1_64_centos ~]#

保存容器
查看所有的容器信息, 能獲取容器的id

[root@VM_1_64_centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
649840f41e77 centos "/bin/bash" 2 minutes ago Exited (0) 58 seconds ago vigorous_banach
[root@VM_1_64_centos ~]#

然後執行如下命令
,保存鏡像:

[root@VM_1_64_centos ~]# docker commit -m="test container 20191112" 649840f41e77
sha256:92bf635d09941b79558c62c1940ed4a707091d83d26595624cf8bb62b69e28e3
[root@VM_1_64_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 92bf635d0994 3 seconds ago 220MB
centos latest 0f3e07c0138f 5 weeks ago 220MB
[root@VM_1_64_centos ~]#

給剛剛創建的鏡像一個tag
[root@VM_1_64_centos ~]# docker tag 92bf635d0994 centos02
[root@VM_1_64_centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos02 latest 92bf635d0994 48 seconds ago 220MB
centos latest 0f3e07c0138f 5 weeks ago 220MB
[root@VM_1_64_centos ~]#

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