1、使用mysql:5.6和 owncloud 镜像,构建一个个人网盘。
①拉取mysql:5.6和 owncloud 镜像[root@node1 harbor]# docker pull mysql:5.6
[root@node1 harbor]# docker pull owncloud
[root@node1 harbor]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZEmysql 5.6 dd3b2a5dcb48 20 months ago 303MB
owncloud latest 327bd201c5fb 4 years ago 618MB②进入容器
[root@node1 harbor]# docker run -itd --name mysql --env MYSQL_ROOT_PASSWORD=123456 mysql:5.6
160c93a712eef40f89ff6cd69db2ce00df659240c0e63577d5718355ecd1de98
[root@node1 harbor]# docker run -itd --name web1 --link mysql:mysql -p 80:80 owncloud:latest
cede2f8bb9c1b8d99581a15322de7a594edceb3901e214a60c758fffc719f075[root@node1 harbor]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nam
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 857/sshd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 31503/docker-proxy
tcp6 0 0 :::22 :::* LISTEN 857/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1058/master
tcp6 0 0 :::3306 :::* LISTEN 1199/mysqld
tcp6 0 0 :::80 :::* LISTEN 31511/docker-pr[root@node1 harbor]# kill 31503
删除创建失败的 web1容器
重新创建web1
docker run -itd --name web1 --link mysql:mysql -p 80:80 owncloud:latest
③ 浏览器访问
④ 输入用户名(admin)和密码(123456)登录成功
2、安装搭建私有仓库 Harbor
(1)安装docker编排工具docker compose
[root@node1 ~]# yum -y install epel-release #安装pip需要先安装epel-release包[root@node1 ~]# yum install -y python-pip #安装pip
[root@node1 ~]# pip install --upgrade pip #升级pip
[root@node1 ~]# pip install docker-compose #安装docker-compose
[root@node1 ~]# docker-compose -v
/usr/local/lib/python3.6/site-packages/paramiko/transport.py:32: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography. The next release of cryptography will remove support for Python 3.6.
from cryptography.hazmat.backends import default_backend
docker-compose version 1.29.2, build unknown若升级报错:
原因:
yum install python-pip时,CentOS7默认的python版本是2.7。它不支持更新到最新版本。
解决:
[root@node1 bin]# yum install -y python3-pip #安装python3的pip
[root@node1 bin]# pip3 install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple #升级pip(需要使用pip3指令),-i 添加镜像源地址,提高下载速度
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
[root@node1 bin]# pip3 install docker-compose -i https://pypi.tuna.tsinghua.edu.cn/simple(2)下载Harbor 安装包
添加一块新硬盘格式化后挂载在/harbordata:①添加sdb1磁盘
首先把虚拟机系统关机,在弹出的界面中单击添加按钮,新增一块硬件设备,如下图所示:
在虚拟机中模拟添加了硬盘设备后就应该能看到抽象成的硬盘设备文件了。按照udev 服务命名规则,第二个被识别的SCSI 设备应该会被保存为/dev/sdb,这个就是硬盘设备文件了。但在开始使用该硬盘之前还需要进行分区操作,例如从中取出一个10GB 的分区设备以供后面的操作使用。
用fdisk 命令来尝试管理/dev/sdb 硬盘设备:
[root@node1 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x67bd3d87.Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is setCommand (m for help): w
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.[root@node1 ~]# file /dev/sdb1
/dev/sdb1: block special
[root@node1 ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@node1 ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.[root@node1 ~]# file /dev/sdb1
/dev/sdb1: block special
②格式化和挂载[root@node1 ~]# mkfs.ext4 /dev/sdb1
[root@node1 ~]# mkdir /harbordata
[root@node1 ~]# mount /dev/sdb1 /harbordata/
[root@node1 ~]# df -h
(3)修改配置文件
[root@node1 ~]# /harbordata/harbor[root@node1 harbor]# cp harbor.yml.tmpl harbor.yml
[root@node1 harbor]# vim harbor.yml
修改hostname值和注释https为443的信息,如下(4)添加主机映射
[root@node1 harbor]# tail -1 /etc/hosts
192.168.19.133 node1安装启动harbor
[root@node1 harbor]# ./install.sh安装后验证
①打开了一些端口[root@node1 harbor]# ss -lntup | grep docker
tcp LISTEN 0 128 127.0.0.1:1514 *:* users:(("docker-proxy",pid=26691,fd=4))
tcp LISTEN 0 128 *:80 *:* users:(("docker-proxy",pid=27275,fd=4))
tcp LISTEN 0 128 [::]:80 [::]:* users:(("docker-proxy",pid=27279,fd=4))
②harbor实际就是启动了一些docker服务[root@node1 harbor]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be75291d7f54 goharbor/nginx-photon:v2.8.4 "nginx -g 'daemon of…" About a minute ago Up About a minute (healthy) 0.0.0.0:80->8080/tcp, :::80->8080/tcp nginx
8391933a9bd0 goharbor/harbor-jobservice:v2.8.4 "/harbor/entrypoint.…" About a minute ago Up 58 seconds (healthy) harbor-jobservice
d4be8400acb4 goharbor/harbor-core:v2.8.4 "/harbor/entrypoint.…" About a minute ago Up About a minute (healthy) harbor-core
3、编写Dockerfile制作Web应用系统nginx镜像,生成镜像nginx:v1.1,并推送其到私有仓库。
具体要求如下:(1)基于centos基础镜像;
(2)指定作者信息;
(3)安装nginx服务,将提供的dest目录(提供默认主页index.html)传到镜像内,并将dest目录内的前端文件复制到nginx的工作目录;
(4)暴露80端口;
(5)设置服务自启动。
(6)验证镜像。
①创建项目目录
[root@node1 ~]# mkdir nginx
[root@node1 ~]# cd nginx②创建html文件
[root@node1 ~]# cd nginx/
[root@node1 nginx]# cat index.html
<html>
<head>
<title> node1 docker nginx demo </title>
</head>
<body>
<h1> This is Nginx - node1 </h1>
</body>
</html>
③创建Dockerfile
Dockerfile包含了Docker的指令。在nginx目录下创建Dockerfile,文件名必须是Dockerfile。[root@node1 nginx]# cat Dockerfile
FROM centos:7
MAINTAINER "mysql <mysql@163.com>"
RUN yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel wget\
&& useradd -r -s /sbin/nologin nginx \
&& yum clean allRUN wget http://nginx.org/download/nginx-1.16.1.tar.gz && tar -xf nginx-1.16.1.tar.gz -C /usr/local/src/
RUN cd /usr/local/src/nginx-1.16.1 \
&& ./configure --prefix=/apps/nginx \
&& make \
&& make install \
&& rm -rf /usr/local/src/nginx*
#COPY nginx.conf /apps/nginx/conf/nginx.confWORKDIR /usr/local/src/nginx
COPY index.html /apps/nginx/html/
RUN ln -s /apps/nginx/sbin/nginx /usr/sbin/nginx
EXPOSE 80
CMD ["/usr/sbin/nginx","-g","daemon off;"]了解Dockerfile指令 : http://t.csdn.cn/hJf7A
④构建Docker 镜像
[root@node1 nginx]# docker build -t nginx:v1.1 .……
Successfully built bc151e88c131
Successfully tagged nginx:v1.1⑤登陆到镜像仓库
[root@node1 nginx]# docker login -u admin -p Harbor12345 192.168.19.133:8081⑥打标记并将nginx:v1.1镜像推送到远程仓库
[root@node1 nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v1.1 bc151e88c131 About an hour ago 347MB[root@node1 nginx]# docker tag nginx:v1.1 node1:8081/test/nginx:v1.1
[root@node1 nginx]# docker push node1:8081/test/nginx:v1.1⑦运行镜像
[root@node1 nginx]# docker run -p 80:80 -d nginx:v1.1
068f91f7655046047cf231144e1500bf44ee75eeb3e2aaad367eabce771e9ce0
[root@node1 nginx]# curl 192.168.19.133
<html>
<head>
<title> node1 docker nginx demo </title>
</head>
<body>
<h1> This is Nginx - node1 </h1>
</body>
</html>
4、Dockerfile快速搭建自己专属的LAMP环境,生成镜像lamp:v1.1,并推送到私有仓库。
具体要求如下:(1)基于centos:6基础镜像;
(2)指定作者信息;
(3)安装httpd、mysql、mysql-server、php、php-mysql、php-gd;
(4)暴露80和3306端口;
(5)设置服务自启动。
(6)验证镜像。
①创建项目目录
[root@node1 ~]# mkdir lamp
[root@node1 ~]# cd lamp②创建Dockerfile
[root@node1 lamp]# cat Dockerfile
FROM centos:7
MAINTAINER "mysql <mysql@163.com>"ENV WEB_DOC_ROOT="/data/web/html"
RUN yum install -y httpd mariadb-server php php-msysql php-gd
EXPOSE 80/tcp
EXPOSE 3306/tcp
CMD /bin/httpd -f -h ${WEB_DOC_ROOT}③构建 Docker 镜像
[root@node1 lamp]# docker build -t lamp:v1.1 .……
Successfully built 314dca356adf
Successfully tagged lamp:v1.1[root@node1 lamp]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
lamp v1.1 314dca356adf 2 minutes ago 633MB④登陆到镜像仓库
[root@node1 lamp]# docker login -u admin -p Harbor12345 192.168.19.133:8081⑤打标记并将lamp:v1.1镜像推送到远程仓库
[root@node1 lamp]# docker tag lamp:v1.1 node1:8081/test/lamp:v1.1
[root@node1 lamp]# docker push node1:8081/test/lamp:v1.1
The push refers to repository [node1:8081/test/lamp]
70e564259e24: Pushed
174f56854903: Pushed
v1.1: digest: sha256:ea26091cc0b09fa199915de8fd50f2cdc32705bf688891f3825793b78bee5bf1 size: 742