docker快速搭建实验
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ~]#
以上是关于docker快速搭建实验的主要内容,如果未能解决你的问题,请参考以下文章