极客时间运维进阶训练营第二周作业
作者:chenmin
- 2022-10-30 新疆
本文字数:1576 字
阅读完需:约 5 分钟
1.基于 dockerfile,实现分层构建的 nginx 业务镜像
#1.创建工作目录
chen@chen:~$ mkdir nginx-centos
chen@chen:~$ cd nginx-centos
#2.准备文件
chen@chen:~/nginx-centos$ ls -l
-rw-rw-r-- 1 chen chen 1229 Oct 30 10:55 CentOS-Base.repo
-rw-r--r-- 1 chen chen 38751 Oct 30 09:35 frontend.tar.gz
-rw-r--r-- 1 chen chen 1064925 Oct 30 09:33 nginx-1.21.1.tar.gz
-rw-r--r-- 1 chen chen 2812 Oct 30 09:34 nginx.conf
#3.编辑Dockerfile
chen@chen:~/nginx-centos$ vi Dockerfile
FROM centos:centos7.9.2009
MAINTAINER "chen 370460269@qq.com"
RUN yum install -y gcc make zlib zlib-devel pcre pcre-devel expect openssl-devel
ADD nginx-1.21.1.tar.gz /usr/local/src/
RUN cd /usr/local/src/nginx-1.21.1 && ./configure --prefix=/apps/nginx && make && make install && ln -sv /apps/nginx/sbin/nginx /usr/bin
RUN groupadd -g 2088 nginx && useradd -g nginx -s /usr/sbin/nologin -u 2088 nginx && chown -R nginx.nginx /apps/nginx
ADD nginx.conf /apps/nginx/conf/
ADD frontend.tar.gz /apps/nginx/html/
EXPOSE 80 443
#ENTRYPOINT ["nginx"]
CMD ["/apps/nginx/sbin/nginx","-g","daemon off;"]
#4.运行容器镜像
chen@chen:~/nginx-centos$ sudo docker run -d -p 80:80 nginx-centos:v1
1c8297128bf15b3a57475276d7ad438fbd96bc4b97cd007808fed97ce3ea241f
chen@chen:~/nginx-centos$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c8297128bf1 nginx-centos:v1 "/apps/nginx/sbin/ng…" 6 seconds ago Up 5 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp stupefied_thompson
复制代码
2.基于 docker 实现对容器的 CPU 和内存的资源限制
#1.限制容器CPU使用
chen@chen:~/nginx-centos$ sudo docker run -d --rm --name cputest --cpus 1 nginx
5cc912243d0af79786fec5eb76bc32867bd7d6152e8c643969108962d2acff3f
chen@chen:~/nginx-centos$ sudo docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
5cc912243d0a cputest 0.00% 3.246MiB / 3.84GiB 0.08% 656B / 0B 0B / 16.4kB 3
1c8297128bf1 stupefied_thompson 0.00% 1.863MiB / 3.84GiB 0.05% 3.52kB / 946B 0B / 0B 2
#2.限制容器内存使用
chen@chen:~/nginx-centos$ sudo docker run -d --rm -m 256m --name memtest nginx
WARNING: Your kernel does not support swap limit capabilities or the cgroup is not mounted. Memory limited without swap.
d5d6e0b0fffa453d4e84d2660147019ce5eb7c5253f4f15d32b28f9cae11949b
chen@chen:~/nginx-centos$ sudo docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
d5d6e0b0fffa memtest 0.00% 3.191MiB / 256MiB 1.25% 586B / 0B 0B / 16.4kB 3
1c8297128bf1 stupefied_thompson 0.00% 1.863MiB / 3.84GiB 0.05% 3.45kB / 946B 0B / 0B 2
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
复制代码
3.部署 http 协议的 harbor 镜像仓库
abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd
abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd
abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd
划线
评论
复制
发布于: 刚刚阅读数: 3
chenmin
关注
还未添加个人签名 2018-02-26 加入
还未添加个人简介
评论