第2次实践作业
(1) 实现一个自定义的web容器服务
获取 Nginx 镜像
$ docker pull nginx
查看是否已安装了 nginx
$ docker image
安装完成后,我们可以使用以下命令来运行 nginx 容器
$ docker run --name nginx-test -p 8080:80 -d nginx
可以通过浏览器可以直接访问 8080 端口的 nginx 服务
mkdir创建一个新目录
把nginx容器的默认配置default.conf复制进文件夹中,更改2433端口和root目录
vim 在该目录下新增文件index.html
创建Dockerfile文件
#Dockerfile
FROM nginx
#维护人的信息
MAINTAINER LXX031702433
COPY default.conf /etc/nginx/conf.d/
COPY index.html /home/hadoop/dockerFile/
#暴露端口
EXPOSE 4399
在该目录下构建镜像
$ docker build -t lxxnginx .
运行容器
$ docker run --name lnginx -d -p 2433:2433 lxxnginx
在浏览器访问2433端口
(2) 实现一个自定义的数据库容器服务
参考地址
1、首先创建Dckerfile
FROM mysql:5.7
#作者的信息
MAINTAINER LXX031702433
#设置免密登录
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
#将所需文件放到容器中
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、需要导入数据的mysql脚本命令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` bigint(20) NOT NULL,
`name` varchar(255) DEFAULT ""
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- 插入数据
INSERT INTO test (`id`, `name`)
VALUES
(031702433,'lxx');
4、mysql权限设置命令privileges.sql
use mysql;
select host, user from user;
-- 因为mysql版本是5.7,因此新建用户为如下命令:
create user docker identified by '123456';
-- 将docker_mysql数据库的权限授权给创建的docker用户,密码为123456:
grant all on docker_mysql.* to docker@'%' identified by '123456' with grant option;
-- 这一条命令一定要有:
flush privileges;
创建镜像
$ docker build -t lxxmysql .
运行容器
$ docker run --name lmysql -d -p 13306:3306 lxxmysql
执行exec命令进入容器
$ sudo docker exec -it b3501a0e1347 /bin/bash
使用docker用户登录数据库 ,切换至docker_mysql数据库
mysql -u docker -p
use docker_mysql
查看表中的数据
select * from test;
使用exit退出
查看容器的配置信息
docker inspect b3501a0e1347
[
{
"Id": "b3501a0e1347c7ca336a63348d97eb21e562d69a694aec2128be9baa9c563878",
"Created": "2020-04-22T14:12:38.566544627Z",
"Path": "docker-entrypoint.sh",
"Args": [
"sh",
"/mysql/setup.sh"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 6913,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-04-22T14:12:40.306831275Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:e7375dba6dcf311c5333e9bd6e21b37ffb6fa67cc374f840f73759518379af0b",
"ResolvConfPath": "/var/lib/docker/containers/b3501a0e1347c7ca336a63348d97eb21e562d69a694aec2128be9baa9c563878/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/b3501a0e1347c7ca336a63348d97eb21e562d69a694aec2128be9baa9c563878/hostname",
"HostsPath": "/var/lib/docker/containers/b3501a0e1347c7ca336a63348d97eb21e562d69a694aec2128be9baa9c563878/hosts",
"LogPath": "/var/lib/docker/containers/b3501a0e1347c7ca336a63348d97eb21e562d69a694aec2128be9baa9c563878/b3501a0e1347c7ca336a63348d97eb21e562d69a694aec2128be9baa9c563878-json.log",
"Name": "/lmysql",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"3306/tcp": [
{
"HostIp": "",
"HostPort": "13306"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/38f5b7569c867e333f93e7a34120d1c92387deb56374a4f4cfde9a48878e48b3-init/diff:/var/lib/docker/overlay2/46ad0205109ea2f2ee6fb3b0211a0b2788354b7f785e64d6a24dc17b543058cf/diff:/var/lib/docker/overlay2/a92ed51c0c129d2ba400b9c550b2d707b8e26b1aea411242da6b5ba1fdba1d7b/diff:/var/lib/docker/overlay2/273547ff1578d0a81be0e3a990b0b76261b79801132b153a4a96598ef1f01193/diff:/var/lib/docker/overlay2/f3d3a12427e9e40bcdba7712a0b2d7b19d92c13cb950898eb523b807a3163d71/diff:/var/lib/docker/overlay2/659bd4fa40675ef818f09427930013adcce7f478eb67298deccb6f8e3c492b41/diff:/var/lib/docker/overlay2/b60a9d26a34671f764bc9eb91d8793aaffe96b491a71b29a7d3d3aeba31f608a/diff:/var/lib/docker/overlay2/2bebaf44369affdda2c84ffd70b05ba2b36996c8e77b6dda5e809a697d85c5ad/diff:/var/lib/docker/overlay2/40577efac666a448a6f6ecc6ad95e16d2279caf0bf35eff2dfdc540f486b1506/diff:/var/lib/docker/overlay2/c40395db81b627fa39f920ac1fd8c0ec1002948394dd9f17d577f834b7a0cbf7/diff:/var/lib/docker/overlay2/3e399358683569a53b84d5aa810ed8190c62428420f96088b4fc71ec3c6e5cb3/diff:/var/lib/docker/overlay2/16ad574077f8e306c6fd499de821b37798a9c539ddb02281d35afd4b3111210f/diff:/var/lib/docker/overlay2/43e8e26fa5e4f94e195ffa655f519bc7f6716d6f0bfc0e3081b3daaba08fcf79/diff:/var/lib/docker/overlay2/490ed74d5b2950b286ef2f6d93441e3e955330226badb3866845e118acf0bd99/diff:/var/lib/docker/overlay2/2b4e79ef447992fd52adbd66baefce41828547ff11e75eb49fad45c09945df3c/diff",
"MergedDir": "/var/lib/docker/overlay2/38f5b7569c867e333f93e7a34120d1c92387deb56374a4f4cfde9a48878e48b3/merged",
"UpperDir": "/var/lib/docker/overlay2/38f5b7569c867e333f93e7a34120d1c92387deb56374a4f4cfde9a48878e48b3/diff",
"WorkDir": "/var/lib/docker/overlay2/38f5b7569c867e333f93e7a34120d1c92387deb56374a4f4cfde9a48878e48b3/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "cb07f2fe6757d62c5a9879ed0259276613e8bd9c125b191466d4e21176e253f1",
"Source": "/var/lib/docker/volumes/cb07f2fe6757d62c5a9879ed0259276613e8bd9c125b191466d4e21176e253f1/_data",
"Destination": "/var/lib/mysql",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "b3501a0e1347",
"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=yes"
],
"Cmd": [
"sh",
"/mysql/setup.sh"
],
"Image": "lxxmysql",
"Volumes": {
"/var/lib/mysql": {}
},
"WorkingDir": "",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "ffb9bb3991b3084b4312db04e43aecb4852048a71cc79d6d09cc5bfa6de71c71",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "13306"
}
],
"33060/tcp": null
},
"SandboxKey": "/var/run/docker/netns/ffb9bb3991b3",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "4ab1b6a9b49fffee3bde11b3f1a20a8ee9edddd2c33a5ecdd45ce83973755417",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "81958e1db4417df41858bfb3b381d305a01ad8c8f1bb946431ec0abffa163cc3",
"EndpointID": "4ab1b6a9b49fffee3bde11b3f1a20a8ee9edddd2c33a5ecdd45ce83973755417",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
(3) 遇到的问题
在自定义的web容器服务时一开始没有使用Dockerfile构建,百度的方式直接挂载容器,有点乱感觉不是很好但也记录一下
一开始先mkdir创建目录,用于存放Nginx的相关文件信息
$ mkdir nginx
$ cd nginx
$ mkdir -p {conf,www,logs}
其中:
- www: 目录将映射为 nginx 容器配置的虚拟目录。
- logs: 目录将映射为 nginx 容器的日志目录。
- conf: 目录里的配置文件将映射为 nginx 容器的配置文件。
cd 进入 /nginx/www 目录
拷贝容器内 nginx 默认配置文件到本地当前目录下的 conf 目录
$ docker cp 1fa929f0c7d0:/etc/nginx/conf.d/default.conf /home/hadoop/nginx/conf
部署命令
$ docker run --name nginx -d -p 4399:80 \
> -v /home/hadoop/nginx/www:/usr/share/nginx/html \
> -v /home/hadoop/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
> -v /home/hadoop/nginx/logs:/var/log/nginx \
> nginx:latest
命令说明:
- p 4399:80 : 将容器的 80 端口映射到主机的 4399 端口.
- 第一个-v :将自己创建的 www 目录挂载到容器的 /usr/share/nginx/html。
- 第二个-v :将自己创建的 nginx.conf 挂载到容器的 /etc/nginx/nginx.conf
- 第三个-v :将自己创建的 logs 挂载到容器的 /var/log/nginx。
在浏览器里输入4399端口号可以访问到index.html的内容