系统实践第二次作业

使用Dockerfile文件实现自定义镜像

1.题目要求

推荐apache或nginx,要求标明镜像作者信息,安装必要的工具以方便维护;设定你自己的web存放目录,安全起见,请将默认的监听端口80更改为你自定义的端口,并且声明暴露的端口,容器启动时,能直接进入web代码的存放目录。

2.新建一个目录下面新建三个vi,分别存放三个文件

3.default.conf

这是nginx的配置文件

charset utf-8;
server {
    listen       2435;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/myweb;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/myweb;
    }
}

1)listen是监听端口,默认是80,这里修改为我学号后四位2435。
2)root指定了我们网页资源文件存放的目录,默认为/usr/share/nginx/html,这里指定为/usr/share/nginx/myweb
这里出了点问题,主要是启动容器后会报错
百度解决

4.Dockerfile文件

FROM nginx
MAINTAINER zhanghao031702435
RUN apt-get update && apt-get install -y nginx vim net-tools
COPY default.conf /etc/nginx/conf.d/
COPY index.html /home/zhanghao/docker1/
WORKDIR /home/zhanghao/docker1/
EXPOSE 2435

5.index.html文件

<!DOCTYPE html>
<html>
    <head>
        <title>031702435Nginx</title>
    </head>
    <body>
        <h1>张昊</h1>
        <h1>031702435</h1>
        <h1>Welcome Nginx</h1>
    </body>
</html>

6.构建镜像

sudo docker build -t zhnginx:1.0 .
james@allan:-ThinkPad-T470p:~/WorkPath$ docker build -t zhnginx:1.0
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 zhnginx:1.0

7.运行

docker run -dit -p 2435:2435 zhnginx:1.0
docker ps -a
curl 127.0.0.1:2435

实现一个自定义的数据库容器服务

1.题目要求

可选择Mysql,Mariadb等,要求标明镜像作者信息,为了方便维护,需要能够查看容器内的配置信息,包括但不限于网络、应用配置文件等。在环境变量中设置好数据库的root密码且不允许空密码登录,创建一个测试数据库,指定用户名和密码

2.Dockerfile

#设置基础镜像信息
FROM mysql:5.7

#镜像作者信息
MAINTAINER zhanghao031702435

#设置免密登录
ENV MYSQL_ALLOW_EMPTY_PASSWORD no
ENV MYSQL_ROOT_PASSWORD=123456

#将所需文件放到容器中
COPY setup.sh /home/zh/docker2/setup.sh
COPY schema.sql /home/zh/docker2/schema.sql
COPY privileges.sql /home/zh/docker2/privileges.sql

#设置容器启动时执行的命令
CMD ["sh", "/mysql/setup.sh"]

3.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 < /home/zh/docker2/schema.sql
echo '3.导入数据完毕....'

sleep 3
echo `service mysql status`

#重新设置mysql密码
echo '4.开始修改密码....'
mysql < /home/zh/docker2/privileges.sql
echo '5.修改密码完毕....'

#sleep 3
echo `service mysql status`
echo 'mysql容器启动完毕,且数据导入成功'

tail -f /dev/null

4.schema.sql文件

-- 创建数据库
create database `docker_mysql` default character set utf8 collate utf8_general_ci;

use docker_mysql;

-- 建表
DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (
  `id` bigint(20) NOT NULL,
  `email` varchar(255) DEFAULT NULL,
  `username` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- 插入数据
INSERT INTO `user` (`id`,  `email`,  `username`)
VALUES
	(0,'john.doe@example.com','user');

5.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;

6.build image

docker build -t zh_mysql
james@allan:-ThinkPad-T470p:~/WorkPath$ docker build -t zh_mysql .
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 zh_mysql:latest

7.运行

docker run -d -p 13306:3306 zh_mysql
docker ps -a

命令行进入容器

mysql -u kingdom -p
use docker_mysql;
show tables;
select * from test;


posted @ 2020-04-25 00:55  Zoe丨  阅读(218)  评论(0编辑  收藏  举报