第2次实践作业
使用Dockerfile文件实现自定义镜像
一、实现一个自定义的web容器服务
0.题目要求
推荐apache或nginx,要求标明镜像作者信息,安装必要的工具以方便维护;设定你自己的web存放目录,安全起见,请将默认的监听端口80更改为你自定义的端口,并且声明暴露的端口,容器启动时,能直接进入web代码的存放目录。
我采用的是CentOS7+nginx
1.default.conf
这是nginx的配置文件
charset utf-8;
server {
listen 2426;
server_name localhost;
location / {
root /usr/share/nginx/mydir;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/mydir;
}
}
1)listen是监听端口,默认是80,这里修改为我学号后四位2426。
2)root指定了我们网页资源文件存放的目录,默认为/usr/share/nginx/html,这里我们将他指定为我自己目录/usr/share/nginx/mydir,这样启动时候就会默认到我的目录下打开index.html
2.nginx.repo
用于yum配置nginx官方源来安装nginx
可以在nginx官网的documents中(http://nginx.org/en/linux_packages.html#RHEL-CentOS)查看到这个配置方法
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
3.index.html
将要展示网页
内容随意,只要命名为index.html即可
<!DOCTYPE html>
<html>
<head>
<title>031702426Nginx</title>
</head>
<body>
<h1>朱庆章</h1>
<h1>031702426</h1>
<h1>Nginx</h1>
</body>
</html>
4.Dockerfile
#Dockerfile
FROM centos:centos7
#MAINTAINER 维护者信息
MAINTAINER ZhuQingzhang031702426
#Dockerfile是docker的配置文件
#nginx.repo容器安装nginx的yum镜像
ADD default.conf /etc/nginx/conf.d/
ADD nginx.repo /etc/yum.repos.d/
ADD index.html /usr/share/nginx/mydir/
#RUN执行以下命令
RUN yum install -y nginx
#EXPOSE 暴露端口
EXPOSE 2426
#CMD 运行以下命令,daemon off后台运行,否则启动完就自动关闭
CMD ["/usr/sbin/nginx", "-g","daemon off;"]
5.build image
将上面所有四个文件存放到我主目录下的WorkPath文件夹中
(default.conf、nginx.repo、index.html、Dockerfile)
cd WorkPath/
进入工作目录
docker build -t nginx_zqz .
注意最后有个点,代表使用当前路径的 Dockerfile 进行构建
这样就开始 build 了
输出
kingdom@kingdom-ThinkPad-T470p:~/WorkPath$ docker build -t nginx_zqz .
Sending build context to Docker daemon 53.25kB
Step 1/8 : FROM centos:centos7
---> 5e35e350aded
Step 2/8 : MAINTAINER ZhuQingzhang031702426
---> Running in 87af89b7374e
Removing intermediate container 87af89b7374e
---> 5766b04652bc
Step 3/8 : ADD default.conf /etc/nginx/conf.d/
---> 8bff7cc65be0
Step 4/8 : ADD nginx.repo /etc/yum.repos.d/
---> 5ada3f731c58
Step 5/8 : ADD index.html /usr/share/nginx/mydir/
---> 0cbc75582c8f
Step 6/8 : RUN yum install -y nginx
---> Running in 996d4111f15d
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.cn99.com
* extras: mirrors.cn99.com
* updates: mirrors.cn99.com
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.16.1-1.el7.ngx will be installed
--> Processing Dependency: openssl >= 1.0.2 for package: 1:nginx-1.16.1-1.el7.ngx.x86_64
--> Running transaction check
---> Package openssl.x86_64 1:1.0.2k-19.el7 will be installed
--> Processing Dependency: make for package: 1:openssl-1.0.2k-19.el7.x86_64
--> Running transaction check
---> Package make.x86_64 1:3.82-24.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
nginx x86_64 1:1.16.1-1.el7.ngx nginx 766 k
Installing for dependencies:
make x86_64 1:3.82-24.el7 base 421 k
openssl x86_64 1:1.0.2k-19.el7 base 493 k
Transaction Summary
================================================================================
Install 1 Package (+2 Dependent packages)
Total download size: 1.6 M
Installed size: 4.6 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/openssl-1.0.2k-19.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for openssl-1.0.2k-19.el7.x86_64.rpm is not installed
--------------------------------------------------------------------------------
Total 12 kB/s | 1.6 MB 02:23
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-7.1908.0.el7.centos.x86_64 (@CentOS)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:make-3.82-24.el7.x86_64 1/3
Installing : 1:openssl-1.0.2k-19.el7.x86_64 2/3
Installing : 1:nginx-1.16.1-1.el7.ngx.x86_64 3/3
warning: /etc/nginx/conf.d/default.conf created as /etc/nginx/conf.d/default.conf.rpmnew
----------------------------------------------------------------------
Thanks for using nginx!
Please find the official documentation for nginx here:
* http://nginx.org/en/docs/
Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* http://nginx.org/en/support.html
Commercial subscriptions for nginx are available on:
* http://nginx.com/products/
----------------------------------------------------------------------
Verifying : 1:make-3.82-24.el7.x86_64 1/3
Verifying : 1:nginx-1.16.1-1.el7.ngx.x86_64 2/3
Verifying : 1:openssl-1.0.2k-19.el7.x86_64 3/3
Installed:
nginx.x86_64 1:1.16.1-1.el7.ngx
Dependency Installed:
make.x86_64 1:3.82-24.el7 openssl.x86_64 1:1.0.2k-19.el7
Complete!
Removing intermediate container 996d4111f15d
---> 6b1632e7aeba
Step 7/8 : EXPOSE 2426
---> Running in 3be6a08ed493
Removing intermediate container 3be6a08ed493
---> 1f9d81cd0e6e
Step 8/8 : CMD ["/usr/sbin/nginx", "-g","daemon off;"]
---> Running in 3f5e0855ca24
Removing intermediate container 3f5e0855ca24
---> 25bb06e256ce
Successfully built 25bb06e256ce
Successfully tagged nginx_zqz:latest
docker images
列出所有镜像
docker inspect <imageID>
查看镜像信息
kingdom@kingdom-ThinkPad-T470p:~$ docker inspect 25bb06e256ce
[
{
"Id": "sha256:25bb06e256ce41f8ea29d6979f7cb0a77a6651c0128b7dc55fed444105c21f5c",
"RepoTags": [
"nginx_zqz:latest"
],
"RepoDigests": [],
"Parent": "sha256:1f9d81cd0e6e135c6e9bc592a9455c44e2c3d5787e3f1fb8753df71ed8ed200b",
"Comment": "",
"Created": "2020-04-21T14:02:12.791732249Z",
"Container": "3f5e0855ca24594150871773b101d6e1b31116efad2968728cc83995955c3ae2",
"ContainerConfig": {
"Hostname": "3f5e0855ca24",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"2426/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"/usr/sbin/nginx\" \"-g\" \"daemon off;\"]"
],
"Image": "sha256:1f9d81cd0e6e135c6e9bc592a9455c44e2c3d5787e3f1fb8753df71ed8ed200b",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20191001",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"DockerVersion": "19.03.8",
"Author": "ZhuQingzhang031702426",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"2426/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/usr/sbin/nginx",
"-g",
"daemon off;"
],
"Image": "sha256:1f9d81cd0e6e135c6e9bc592a9455c44e2c3d5787e3f1fb8753df71ed8ed200b",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20191001",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 319305141,
"VirtualSize": 319305141,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/cc7e05dd927065a00cc953154c1226f801cde6a5e08d56ce9132042883694ce3/diff:/var/lib/docker/overlay2/cef7bfb3c12e5533a01509f27c7b15bdc3520752b7b625b69bd296f04da20530/diff:/var/lib/docker/overlay2/9fd2b0f6af4509a075aeaa69b92d16da48e2c36da982decc28ec019f92eead17/diff:/var/lib/docker/overlay2/55b0d888a85d75105b00260257c725e2052b3f5b570addb1f68b31a942490284/diff",
"MergedDir": "/var/lib/docker/overlay2/881f957306c77455c7fe189233e3850b8c85d85be774c01bc5e0c385385497d0/merged",
"UpperDir": "/var/lib/docker/overlay2/881f957306c77455c7fe189233e3850b8c85d85be774c01bc5e0c385385497d0/diff",
"WorkDir": "/var/lib/docker/overlay2/881f957306c77455c7fe189233e3850b8c85d85be774c01bc5e0c385385497d0/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:77b174a6a187b610e4699546bd973a8d1e77663796e3724318a2a4b24cb07ea0",
"sha256:378462c8ac8885954d474c85ce409adc15e343fed3aa29d764d13a9e4dc4d96b",
"sha256:8b144d3dfc59424617b6a3383b8abdc6f0602dc2501a5cf84b8ae5819533a832",
"sha256:a1bff4f1c61d5f8d09385e56e6df4d63f87aebbd05c363ffc042a26a93a9fe13",
"sha256:60d044f12d742558f5104ccda2cf7df55f3bda3adb18e22ba36393540fde6080"
]
},
"Metadata": {
"LastTagTime": "2020-04-21T22:02:13.235414357+08:00"
}
}
]
6.运行和查看
docker run -dit -p 2426:2426 nginx_zqz
启动容器
docker ps -a
查看容器运行情况
curl 127.0.0.1:2426
使用terminal访问我主页
然后使用浏览器访问我的主页
7.遇到的问题
一开始,网页访问127.0.0.1:2426
时,中文内容显示乱码。
然后我在default.conf
中
加入一行
charset utf-8;
就可以正确编码了
然后进入到/usr/sbin
目录下
./nginx -s reload
重启nginx生效
这题主要难度不在Dockerfile,如果熟悉Nginx的话应该做的很快
二、实现一个自定义的数据库容器服务
0.题目要求
可选择Mysql,Mariadb等,要求标明镜像作者信息,为了方便维护,需要能够查看容器内的配置信息,包括但不限于网络、应用配置文件等。在环境变量中设置好数据库的root密码且不允许空密码登录,创建一个测试数据库,指定用户名和密码
参考链接
将以下四个文件放到主目录WorkPath文件夹
1.Dockerfile
FROM mysql:5.7
#维护者信息
MAINTAINER ZhuQingzhang031702426
#不允许免密登录
ENV MYSQL_ALLOW_EMPTY_PASSWORD no
#将所需文件放到容器中
COPY setup.sh /mysql/setup.sh
COPY schema.sql /mysql/schema.sql
COPY privileges.sql /mysql/privileges.sql
#设置容器启动时执行的命令
CMD ["sh", "/mysql/setup.sh"]
2.setup.sh
#!/bin/bash
set -e
#查看mysql服务的状态,方便调试,这条语句可以删除
echo `service mysql status`
echo '1.启动mysql....'
#启动mysql
service mysql start
sleep 3
echo `service mysql status`
echo '2.开始导入数据....'
#导入数据
mysql < /mysql/schema.sql
echo '3.导入数据完毕....'
sleep 3
echo `service mysql status`
#重新设置mysql密码
echo '4.开始修改密码....'
mysql < /mysql/privileges.sql
echo '5.修改密码完毕....'
#sleep 3
echo `service mysql status`
echo `mysql容器启动完毕,且数据导入成功`
tail -f /dev/null
3.schema.sql
-- 创建数据库
create database `docker_mysql` default character set utf8 collate utf8_general_ci;
use docker_mysql;
-- 建表
DROP TABLE IF EXISTS test;
CREATE TABLE test (
`id` varchar(10) NOT NULL,
`score` int
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- 插入数据
INSERT INTO test (`id`, `score`)
VALUES
('031702426',100);
4.privileges.sql
use mysql;
select host, user from user;
-- 因为mysql版本是5.7,因此新建用户为如下命令:
create user kingdom identified by '123456';
-- 将docker_mysql数据库的权限授权给创建的用户kingdom,密码为123456:
grant all on docker_mysql.* to kingdom@'%' identified by '123456' with grant option;
-- 这一条命令一定要有:
flush privileges;
5.build image
先cd到WorkPath
docker build -t mysql_zqz .
开始build
kingdom@kingdom-ThinkPad-T470p:~/WorkPath$ docker build -t mysql_zqz .
Sending build context to Docker daemon 53.76kB
Step 1/7 : FROM mysql:5.7
---> 273c7fcf9499
Step 2/7 : MAINTAINER ZhuQingzhang031702426
---> Running in 503def9323bf
Removing intermediate container 503def9323bf
---> 0746a5e33948
Step 3/7 : ENV MYSQL_ALLOW_EMPTY_PASSWORD no
---> Running in d5c793ed718e
Removing intermediate container d5c793ed718e
---> 4abbbc23c736
Step 4/7 : COPY setup.sh /mysql/setup.sh
---> 60ccc0169b0a
Step 5/7 : COPY schema.sql /mysql/schema.sql
---> 10d8c406fbf9
Step 6/7 : COPY privileges.sql /mysql/privileges.sql
---> 5cab43a4b03a
Step 7/7 : CMD ["sh", "/mysql/setup.sh"]
---> Running in 971eb396254f
Removing intermediate container 971eb396254f
---> 5e8f407f310f
Successfully built 5e8f407f310f
Successfully tagged mysql_zqz:latest
docker images
查看所有镜像
docker inspect 5e8f407f310f
查看镜像信息
kingdom@kingdom-ThinkPad-T470p:~$ docker inspect 5e8f407f310f
[
{
"Id": "sha256:5e8f407f310fb4f1e0a2e7f8c3f9e5cf3b8077273d8dea728efc93f982e04731",
"RepoTags": [
"mysql_zqz:latest"
],
"RepoDigests": [],
"Parent": "sha256:5cab43a4b03ab4065c6635fa255b6ffe7db07278a5560c4e9444784b270350d1",
"Comment": "",
"Created": "2020-04-21T17:14:26.237787023Z",
"Container": "971eb396254f7aa3586b00340a5c9cd2a151c6f9fbc1694fa7baecffc8404b15",
"ContainerConfig": {
"Hostname": "971eb396254f",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"3306/tcp": {},
"33060/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.12",
"MYSQL_MAJOR=5.7",
"MYSQL_VERSION=5.7.29-1debian10",
"MYSQL_ALLOW_EMPTY_PASSWORD=no"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"sh\" \"/mysql/setup.sh\"]"
],
"Image": "sha256:5cab43a4b03ab4065c6635fa255b6ffe7db07278a5560c4e9444784b270350d1",
"Volumes": {
"/var/lib/mysql": {}
},
"WorkingDir": "",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "19.03.8",
"Author": "ZhuQingzhang031702426",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"3306/tcp": {},
"33060/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.12",
"MYSQL_MAJOR=5.7",
"MYSQL_VERSION=5.7.29-1debian10",
"MYSQL_ALLOW_EMPTY_PASSWORD=no"
],
"Cmd": [
"sh",
"/mysql/setup.sh"
],
"Image": "sha256:5cab43a4b03ab4065c6635fa255b6ffe7db07278a5560c4e9444784b270350d1",
"Volumes": {
"/var/lib/mysql": {}
},
"WorkingDir": "",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": null
},
"Architecture": "amd64",
"Os": "linux",
"Size": 455220307,
"VirtualSize": 455220307,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/547e0efb1fca26bfdb026374a66facba2d26109cbe8befe605cf6c8a459b6a2f/diff:/var/lib/docker/overlay2/41d648303a5782a2ecf5f7c1a7aadcb4f14a0daa0d63d62c0f2586bf3f58b633/diff:/var/lib/docker/overlay2/0cfcd1236b40e5ef0e1c908838e6239fdccb0bda1d506d3e605fe6305df524b7/diff:/var/lib/docker/overlay2/5103380a07ed13bcf1ba639d5e9a09061f284fb795760c9a317d8e4150078ad2/diff:/var/lib/docker/overlay2/b4f116ca7160c3e9ce0a8f9b4b4d8136fff6b361c3f9721cb576c917c9671686/diff:/var/lib/docker/overlay2/7b6fd50f4ffa0b209b1c338b59d942c1d92f47049eafa81b43b92f0f285fdb5b/diff:/var/lib/docker/overlay2/1a4b189c53e9880cf423de2ef591923e52b986ae96c304868922093a1f609041/diff:/var/lib/docker/overlay2/eb565ef0da593105dcd16c90d9fef5dc024d9a9fe3758cb1b4a2596cfb11ea38/diff:/var/lib/docker/overlay2/5e9e495101f4ce18937a1a2380e40f4509df25eb09a8561137eec7819b5064a1/diff:/var/lib/docker/overlay2/e6489925ed4a25e7f6eaa6b257f3307090b27996799f3a5bc48b9fb79eb2b7c8/diff:/var/lib/docker/overlay2/f17720baba9d67043a374ceeb2cbe8eb5175e866679fb024bc41652b23ad01ec/diff:/var/lib/docker/overlay2/8d256931016d75d9db3b81747e09bb6dc9eca7662444eafeec83528d24a9cff5/diff:/var/lib/docker/overlay2/f92c06b029cb6a5387acdcff40b5756b8e9b58192529931fafe29d3bcec9a2b2/diff",
"MergedDir": "/var/lib/docker/overlay2/6bead5e5b7b15ac32310bb6c27328aba23387ef2ce46fbfc7e36bb2a722b117e/merged",
"UpperDir": "/var/lib/docker/overlay2/6bead5e5b7b15ac32310bb6c27328aba23387ef2ce46fbfc7e36bb2a722b117e/diff",
"WorkDir": "/var/lib/docker/overlay2/6bead5e5b7b15ac32310bb6c27328aba23387ef2ce46fbfc7e36bb2a722b117e/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:b60e5c3bcef2f42ec42648b3acf7baf6de1fa780ca16d9180f3b4a3f266fe7bc",
"sha256:746ef614d66131a14736cc3e9b10a4c33bfe246bee826802af3d1c55f4787faf",
"sha256:478bf6a73d060e527285d1cb3428a61b068ac4e23d3b44aae944ccf6a5438ff5",
"sha256:246ad53299e099d265c24fe63a0c8564838a58f1408e1bf0915aa969704f005f",
"sha256:f2b1703888edba3399b82f9ebdd1c98e729f05b6cb5fd8c5d8f85af0b73589ad",
"sha256:c73d9f519696cf503017bd93185b0df9e4c97539d9bffb7e861e1f419e074032",
"sha256:c4a52d4531b78de76b169a5312c023fc6bb856b1603eeb2748ac3e8622999273",
"sha256:449fcf310ea0173e3d42917ee1a81721b5c939bb028a4d16d7ec80565ae40865",
"sha256:af65e8376fa3402efd8626ae663b3c31f0cabdc918928c0f59e73062773435a4",
"sha256:6ddaaf3458aefd07ed5a1bd4874d2485cfc953c1bdd302f2169dfd8397cbddfd",
"sha256:4c2e7024d97a1f540997293e89527b2d21f992b309a4532b2394553e88c36078",
"sha256:30852b40a7342fa2056e44b5872c7e71ccaf6d4253e50a4c5939a79ea278a04d",
"sha256:e5d38b51e926acbaa0661ff62668658706d9f5cc7df27a07bcb5274f37c68dd3",
"sha256:51e099d1c670c8d334b8a98f7f1047346bf503a258d1c0953c5762b561ac47fd"
]
},
"Metadata": {
"LastTagTime": "2020-04-22T01:14:26.706513125+08:00"
}
}
]
6.运行和查看
docker run -d -p 13306:3306 mysql_zqz
运行容器,映射13306端口
docker ps -a
查看所有容器运行情况
docker exec -it c30e8b185132 /bin/bash
命令行进入容器
mysql -u kingdom -p
使用kingdom账户登录mysql,密码是123456
use docker_mysql;
连接到docker_mysql这个数据库
show tables;
显示这个数据库中所有的表格
select * from test;
选择test表的所有内容
最后使用exit
来退出