dockerfile 制作基于alpine 的nginx 镜像

dockerfile 制作基于alpine 的nginx 镜像

一、alpine 常见用法

Alpine Linux是基于musl libc和busybox的面向安全的轻量级Linux发行版。有些命令用法和包名和centos/ubuntu 之类的不一样

1.1 包管理工具apk

apk add 包名 #安装 xx包

apk del 包名 #卸载 xx包

apk update 更新源

1.2 apk 的源

使用阿里云

a. 编辑 /etc/apk/repositories
b. 将里面 dl-cdn.alpinelinux.org 的 改成 mirrors.aliyun.com ; 保存退出即可

1.3 寻找apk 的包名

在阿里云的镜像地址中搜索相关的包名即可

http://mirrors.aliyun.com/alpine/v3.11/main
http://mirrors.aliyun.com/alpine/v3.11/community
1.4 支持的shell

默认不支持bash shell

/ # cat /etc/shells
# valid login shells
/bin/sh
/bin/ash

1.5 添加用户

使用adduser 而不是useradd

/ # adduser -h
adduser: option requires an argument: h
BusyBox v1.31.1 () multi-call binary.

Usage: adduser [OPTIONS] USER [GROUP]

Create new user, or add USER to GROUP

	-h DIR		Home directory
	-g GECOS	GECOS field
	-s SHELL	Login shell
	-G GRP		Group
	-S		Create a system user
	-D		Don't assign a password
	-H		Don't create home directory
	-u UID		User id
	-k SKEL		Skeleton directory (/etc/skel)

二、具体步骤

2.1 下载镜像
docker pull alpine

当前默认latest 是 3.11 版本

2.2 准备相关文件
2.2.1 alpine 源文件

cat repositories

#http://dl-cdn.alpinelinux.org/alpine/v3.10/main
#http://dl-cdn.alpinelinux.org/alpine/v3.10/community

https://mirrors.aliyun.com/alpine/v3.10/main
https://mirrors.aliyun.com/alpine/v3.10/community

2.2.2 nginx conf
user nginx ;
daemon off ;
location /linux37 {
	root /data/nginx/html/linux37;
	index index.html;
}
2.2.3 页面文件
root@z2:/opt/dockerfile/alpine# mkdir  linux37
root@z2:/opt/dockerfile/alpine# echo alpine_nginx_test >linux37/index.html
root@z2:/opt/dockerfile/alpine# tar zcvf linux37.tar.gz  linux37/
linux37/
linux37/index.html
2.2.4 nginx 源码包
root@z2:/opt/dockerfile/alpine#  wget http://nginx.org/download/nginx-1.16.1.tar.gz
2.3 编写dockerfile
FROM alpine

maintainer zt "25958889@qq.com"

COPY repositories /etc/apk/repositories
RUN  apk update && apk add  iotop  gcc libgcc libc-dev libcurl libc-utils pcre-dev  zlib-dev  libnfs make  pcre pcre2 zip unzip net-tools pstree wget libevent libevent-dev iproute2

ADD nginx-1.16.1.tar.gz /opt/
RUN cd /opt/nginx-1.16.1 && ./configure  --prefix=/apps/nginx && make && make install && ln -sv /apps/nginx/sbin/nginx  /usr/bin/

RUN addgroup  -g 2019 -S nginx && adduser  -s /sbin/nologin -S -D  -u 2019 -G nginx nginx

COPY nginx.conf /apps/nginx/conf/nginx.conf
ADD linux37.tar.gz /data/nginx/html
RUN chown  nginx.nginx  /data/nginx/ /apps/nginx/ -R

EXPOSE 80 443

CMD ["nginx"]
root@z2:/opt/dockerfile/alpine# ll -h
total 1.1M
drwxr-xr-x 3 root root  4.0K May 16 10:00 ./
drwxr-xr-x 5 root root  4.0K May 16 09:47 ../
-rw-r--r-- 1 root root   695 Sep 13  2019 Dockerfile
drwxr-xr-x 2 root root  4.0K May 16 09:49 linux37/
-rw-r--r-- 1 root root   174 May 16 09:50 linux37.tar.gz
-rw-r--r-- 1 root root 1009K Aug 14  2019 nginx-1.16.1.tar.gz
-rw-r--r-- 1 root root  2.7K Sep 13  2019 nginx.conf
-rw-r--r-- 1 root root   199 Sep 13  2019 repositories

2.4 构建镜像,启动容器测试
root@z2:/opt/dockerfile/alpine# cat build-command.sh 
#!/bin/bash
docker build -t alpine-nginx:v1.16.1 .
root@z2:/opt/dockerfile/alpine# bash build-command.sh 

docker  run -d -p 8081:80  alpine-nginx:v1.16.1
root@z2:~# curl 192.168.1.102:8081/linux37/
alpine_nginx_test
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读