Docker-创建源码编译nginx镜像

Docker-创建源码编译nginx镜像

1、从官网网站拉取一个centos作为基础镜像

[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
busybox      latest    d23834f29b38   2 days ago     1.24MB
httpd        latest    ad17c88403e2   13 days ago    143MB
nginx        latest    ea335eea17ab   2 weeks ago    141MB
centos       latest    5d0da3dc9764   2 months ago   231MB

//后台交互方式运行centos
[root@localhost ~]# docker run -dit centos /bin/bash
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
f362f392badb   centos    "/bin/bash"   2 minutes ago   Up 2 minutes             lucid_lewin

2、进入容器编译安装nginx

[root@localhost ~]# docker exec -it f362f392badb /bin/bash
[root@f362f392badb /]# useradd -r -M -s /sbin/nologin nginx
[root@f362f392badb /]# id nginx
uid=998(nginx) gid=996(nginx) groups=996(nginx)
[root@f362f392badb /]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make vim
[root@f362f392badb /]# yum -y groups mark install 'Development Tools'
[root@f362f392badb /]# mkdir -p /var/log/nginx
[root@f362f392badb /]# chown -R nginx.nginx /var/log/nginx
[root@f362f392badb /]# cd /usr/src/
[root@f362f392badb src]# yum -y install wget
//下载nginx源码包
[root@f362f392badb src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
--2021-12-02 10:25:56--  http://nginx.org/download/nginx-1.12.1.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5704::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 981093 (958K) [application/octet-stream]
Saving to: ‘nginx-1.12.1.tar.gz’

nginx-1.12.1.tar 100%[==========>] 958.10K   294KB/s    in 3.3s    

2021-12-02 10:26:00 (294 KB/s) - ‘nginx-1.12.1.tar.gz’ saved [981093/981093]
[root@f362f392badb src]# ls
debug  kernels  nginx-1.12.1.tar.gz

[root@f362f392badb src]# tar xf nginx-1.12.1.tar.gz 
[root@f362f392badb src]# cd nginx-1.12.1
[root@f362f392badb src]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log

[root@f362f392badb nginx-1.12.1]# make && make install

//配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh
[root@f362f392badb nginx]# nginx
[root@f362f392badb nginx]# ss -antl     //此时容器已经在运行
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process                                                      
LISTEN 0      128           0.0.0.0:80          0.0.0.0:*       

//把现在的nginx打包成镜像
[root@localhost ~]# docker commit -p f362f392badb
#生成镜像时不指定名称
sha256:ed87ed4bff8a42b4ee23bfed44cac8158dd58ce3ff6aa80b73e4a3d5dd70921c
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
<none>       <none>    ed87ed4bff8a   18 seconds ago   572MB
busybox      latest    d23834f29b38   2 days ago       1.24MB


//跟改成可以上传自己镜像厂库的名称
[root@localhost ~]# docker tag ed87ed4bff8a wjm1734321/nginx:v0.1
#tag跟改镜像的名称
[root@localhost ~]# docker images
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
wjm1734321/nginx   v0.1      ed87ed4bff8a   20 minutes ago   572MB
busybox             latest    d23834f29b38   2 days ago       1.24MB

//生成新的镜像
[root@localhost ~]# docker commit -p -a 'wjm 1@2.com' -c 'CMD ["/export/server/nginx/sbin/nginx","-g","daemon off;"]' naughty_jackson wjm1734321/nginx:v0.4
#-p暂停容器再创建 -a指定镜像的信息 -c容器运行起来后要执行的任务  
#naughty_jackson 随机生成的主机名 wjm1734321/nginx:v0.4要生成的镜像名称

[root@localhost ~]# docker images
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
wjm1734321/nginx    v0.4      3a7536f0abf7   12 seconds ago   572MB
wjm1734321/nginx    v0.3      2a3bd6e7fb28   23 minutes ago   572MB
wjm1734321/nginx    v0.2      1acbfeda222f   42 minutes ago   572MB
wjm1734321/nginx    v0.1      ed87ed4bff8a   15 hours ago     572MB
wang1734321/nginx   v0.1      ed87ed4bff8a   15 hours ago     572MB
busybox             latest    d23834f29b38   2 days ago       1.24MB
httpd               latest    ad17c88403e2   2 weeks ago      143MB
nginx               latest    ea335eea17ab   2 weeks ago      141MB
centos              latest    5d0da3dc9764   2 months ago     231MB

//启动容器并且配置端口映射 这样就可以用本机的ip地址访问nginx服务
[root@localhost ~]# docker run -d --name nginx01 -p80:80 wjm1734321/nginx:v0.4
5b66dd7a900b40124578334a9ac28a213227a795193fde29a8ced3dd2e33bbc3
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE                   COMMAND                  CREATED         STATUS         PORTS                               NAMES
5b66dd7a900b   wjm1734321/nginx:v0.4   "/export/server/ngin…"   5 seconds ago   Up 4 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx01


[root@localhost ~]# docker inspect eef7db2b3499
......
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                   ......
                   ......
[root@localhost ~]# curl 172.17.0.3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
##此时就可以可以看见nginx页面内容表示可以正常开启容器服务自动运行
##此时就可以可以看见nginx页面内容表示可以正常开启容器服务自动运行

//登陆到自己的镜像仓库上传镜像
[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: wjm1734321
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

## 上传镜像

```bash
[root@localhost ~]# docker push wjm1734321/nginx:v0.4
The push refers to repository [docker.io/wjm1734321/nginx]
c3b4c1ee1050: Pushed 
0abde9e07443: Pushed 
63d2c2042dd4: Layer already exists 
74ddd0ec08fa: Layer already exists 
v0.4: digest: sha256:87a686140fc2efb252ff4b386527012992707fdb9adc6d608edc7073703d1624 size: 1157

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值