首页 Docker教程docker实战-制作nginx镜像

docker-制作镜像

docker实战-dockerfile制作镜像

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

Dockerfile 制作手动制作编译版本nginx 镜像

在centos 基础镜像的容器之上手动编译安装nginx,然后再将此容器提交为镜像

下载镜像并初始化系统
[root@ubuntu1804 ~]#docker pull centos:centos7.7.1908
[root@ubuntu1804 ~]#docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              centos7.7.1908      08d05d1d5859        2 months ago        204MB
[root@ubuntu1804 ~]#docker run -it centos:centos7.7.1908 /bin/bash

#生成yum源配置
[root@86a48908bb97 /]# yum -y install wget
[root@64944257fa88 /]# rm -rf /etc/yum.repos.d/*
[root@64944257fa88 /]# wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/Centos-7.repo http://mirrors.aliyun.com/repo/epel-7.repo 
编译安装nginx
[root@64944257fa88 /]# useradd -r -s /sbin/nologin nginx
#安装基础包
[root@64944257fa88 /]# yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel

[root@64944257fa88 /]# cd /usr/local/src
[root@64944257fa88 src]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
[root@64944257fa88 src]# tar xf nginx-1.16.1.tar.gz 
[root@64944257fa88 src]# cd nginx-1.16.1
[root@64944257fa88 nginx-1.16.1]# ./configure  --prefix=/apps/nginx
[root@64944257fa88 nginx-1.16.1]# make && make install
关闭nginx后台运行
[root@64944257fa88 nginx-1.16.1]# cd /apps/nginx/    
[root@64944257fa88 nginx]# ls
conf  html  logs  sbin
[root@64944257fa88 nginx]# vi conf/nginx.conf
user nginx;
daemon off;
[root@64944257fa88 nginx]# ln -s /apps/nginx/sbin/nginx  /usr/sbin/
[root@64944257fa88 nginx]# ll /usr/sbin/nginx 
lrwxrwxrwx 1 root root 22 Jan 28 05:29 /usr/sbin/nginx -> /apps/nginx/sbin/nginx
准备相关数据自定义web界面
[root@64944257fa88 nginx]# echo "Nginx Test Page in Docker" > /apps/nginx/html/index.html
提交为镜像
#不要退出容器,在另一个终端窗口执行以下命令
[root@ubuntu1804 ~]#docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              centos7.7.1908      08d05d1d5859        2 months ago        204MB
[root@ubuntu1804 ~]#docker ps 
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS               NAMES
64944257fa88        centos:centos7.7.1908   "/bin/bash"         18 seconds ago      Up 17 seconds                           stupefied_albattani
[root@ubuntu1804 ~]#docker commit -m "nginx1.6.1" 64944257fa88  centos7-nginx:1.6.1
sha256:d86d957bb00f35fe09ae38230e1e2d12916f4406e997146c68e34dae7526c079
[root@ubuntu1804 ~]#docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos7-nginx       1.6.1               d86d957bb00f        2 minutes ago       486MB
centos              centos7.7.1908      08d05d1d5859        2 months ago       204MB
从自己的镜像启动容器
[root@ubuntu1804 ~]#docker run -d -p 80:80 centos7-nginx:1.6.1 nginx
ae90b1abf374138a21f7ed104d14c88f1af23c0b2027c3fe099722fd7fbad3a4
[root@ubuntu1804 ~]#docker ps 
CONTAINER ID        IMAGE                 COMMAND             CREATED              STATUS              PORTS                NAMES
ae90b1abf374        centos7-nginx:1.6.1   "nginx"             About a minute ago   Up About a minute   0.0.0.0:80->80/tcp   naughty_thompson

备注:最后面的nginx是运行的命令,即镜像里面要运行一个nginx命令,所以前面软连接到/usr/sbin/nginx,目的就是为了让系统不需要指定路径就可以执行此命令

访问测试
[root@ubuntu1804 ~]#curl 127.0.0.1
Nginx Test Page in Docker
查看Nginx访问日志和进程
[root@ubuntu1804 ~]#docker exec -it ae90b1abf374 bash
[root@ae90b1abf374 /]# cat /apps/nginx/logs/access.log 
172.17.0.1 - - [28/Jan/2020:05:40:51 +0000] "GET / HTTP/1.1" 200 26 "-" "curl/7.58.0"

[root@ae90b1abf374 /]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.2  20572  2468 ?        Ss   05:40   0:00 nginx: master process nginx
nginx         6  0.0  0.3  21024  3104 ?        S    05:40   0:00 nginx: worker process
root          7  0.3  0.2  11840  2928 pts/0    Ss   05:45   0:00 bash
root         21  0.0  0.3  51764  3344 pts/0    R+   05:46   0:00 ps aux

本文链接:http://www.yunweipai.com/34820.html

docker-制作镜像

docker实战-dockerfile制作镜像

网友评论comments

发表评论

邮箱地址不会被公开。

暂无评论

Copyright © 2012-2021 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部