1. nginx的dockerfile源码方式编写
1. 创建一个目录,并进入目录创建一个文件
[root@localhost ~]# mkdir nginx
[root@localhost ~]# cd nginx/
[root@localhost nginx]# mkdir software
[root@localhost nginx]# ls
Dockerfile software
2. 下载源码包到这个software目录下
[root@localhost nginx]# tree .
.
├── Dockerfile
└── software
└── nginx-1.18.0.tar.gz
1 directory, 2 files
3. 编写配置文件
[root@localhost nginx]# cat Dockerfile
#基础镜像信息
FROM alpine
ENV version 1.18.0
ENV install_path /usr/local
ENV PATH $install_path/nginx/sbin:$PATH
# 维护者信息
LABEL MAINTAINER="MQ <2571798411@qq.com>"
# 镜像操作指令
ADD software/nginx-$version.tar.gz $install_path/src/
WORKDIR $install_path/src/nginx-$version
RUN echo 'https://mirrors.ustc.edu.cn/alpine/v3.12/main/' > /etc/apk/repositories && \
echo 'https://mirrors.ustc.edu.cn/alpine/v3.12/community/' >> /etc/apk/repositories && \
addgroup -S nginx && \ // -S表示创建系统用户,系统组
adduser -DHS -s /sbin/nologin -G nginx nginx && \ //-D表示不需要密码;-H表示不创建家目录;-s表示登陆shell;-G表示加一个组,这里表示添加到nginx组里
apk add --no-cache gcc libc-dev make pcre-dev openssl-dev openssl gd-dev zlib-dev && \
./configure --prefix=$install_path/nginx --user=nginx --group=nginx && make && make install
ENTRYPOINT ["nginx"]
# 容器启动时默认要执行的指令
CMD ["-g", "daemon off;"]
4. 生成镜像
[root@Docker]# docker build -t nginx:3.0 nginx
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 3.0 213e1c4e946f 14 seconds ago 211MB
alpine latest 389fef711851 3 weeks ago 5.58MB
5. 运行容器
[root@localhost ~]# docker run -d -p 80:80 nginx:3.0
f88bebbbbb09c0ec71124272c5b5ec3cedd4e5798fee7228bfc54b9c3b2d15a7
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
6. 访问网页
[root@localhost ~]# curl 192.168.50.157:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost ~]#
2. apache的dockerfile源码方式编写
1. 创建目录
[root@localhost ~]# mkdir apache 创建一个apache目录
[root@localhost ~]# ls
anaconda-ks.cfg apache mq nginx
[root@localhost ~]# cd apache/
[root@localhost apache]# ls
[root@localhost apache]# mkdir soft script 进入apache目录创建两个目录,一个放脚本的目录,一个放软件的目录
[root@localhost apache]# ls
script soft
[root@localhost apache]# cd soft/
[root@localhost soft]# ls
[root@localhost soft]# ls 在软件目录下把该下载的软件下载下来
apr-1.6.5.tar.gz httpd-2.4.46.tar.bz2
apr-util-1.6.1.tar.gz
2. 编写Dockerfile
[root@localhost soft]# cd ..
[root@localhost apache]# ls
script soft
[root@localhost apache]# vim Dockerfile
FROM alpine
ENV PATH /usr/local/apache/bin:$PATH
COPY soft /usr/src/
WORKDIR /usr/src/
RUN echo 'https://mirrors.ustc.edu.cn/alpine/v3.12/main/' > /etc/apk/repositories && \
echo 'https://mirrors.ustc.edu.cn/alpine/v3.12/community/' >> /etc/apk/repositories && \
apk update && \
apk add --no-cache -U gcc g++ make openssl-dev pcre-dev expat-dev libtool && \
addgroup -S apache && \
adduser -DHS -s /sbin/nologin -G apache apache && \
tar xf apr-1.6.5.tar.gz && \
tar xf apr-util-1.6.1.tar.gz && \
tar xf httpd-2.4.46.tar.bz2 && \
sed -i '/$RM "$cfgfile"/d' apr-1.6.5/configure && \
cd apr-1.6.5 && \
./configure --prefix=/usr/local/apr && make && make install && \
cd ../apr-util-1.6.1 && \
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
make && make install && \
cd ../httpd-2.4.46 && \
./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork && \
make && make install && \
sed -i '/^#ServerName/s/#//g' /etc/httpd24/httpd.conf && \
rm -rf /usr/src/* /var/cache/* && \
apk del gcc g++ make
EXPOSE 80 443
CMD ["-X"]
ENTRYPOINT ["httpd"]
3. 生成一个镜像
[root@localhost ~]# docker build -t test:v0.8 apache
......
Successfully built 1c1eccc24453
Successfully tagged test:v0.8
4. 运行容器
[root@localhost ~]# docker run -itd --name web6 test:v0.8
81323edc71fc644381cfd69949f18ee83b053efade46c50165b519560d62b7c1
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
81323edc71fc test:v0.8 "httpd -X" 18 seconds ago Up 17 seconds 80/tcp, 443/tcp web6
5. 查看容器内容
[root@localhost ~]# docker inspect 81323edc71fc
......
"Cmd": [
"-X"
],
"Image": "test:v0.8",
"Volumes": null,
"WorkingDir": "/usr/src",
"Entrypoint": [
"httpd"
],
......
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.6",
......
6. 访问网页
[root@localhost ~]# curl 172.17.0.6
<html><body><h1>It works!</h1></body></html>
7. 1. 创建目录
[root@localhost ~]# mkdir apache 创建一个apache目录
[root@localhost ~]# ls
anaconda-ks.cfg apache mq nginx
[root@localhost ~]# cd apache/
[root@localhost apache]# ls
[root@localhost apache]# mkdir soft script 进入apache目录创建两个目录,一个放脚本的目录,一个放软件的目录
[root@localhost apache]# ls
script soft
[root@localhost apache]# cd soft/
[root@localhost soft]# ls
[root@localhost soft]# ls 在软件目录下把该下载的软件下载下来
apr-1.6.5.tar.gz httpd-2.4.46.tar.bz2
apr-util-1.6.1.tar.gz
[root@localhost apache]# tree .
.
├── Dockerfile
├── script
│ └── install.sh
└── soft
├── apr-1.6.5.tar.gz
├── apr-util-1.6.1.tar.gz
└── httpd-2.4.46.tar.bz2
8. 编写脚本
[root@localhost apache]# cat script/install.sh
#!/bin/sh
echo 'https://mirrors.ustc.edu.cn/alpine/v3.12/main/' > /etc/apk/repositories && \
echo 'https://mirrors.ustc.edu.cn/alpine/v3.12/community/' >> /etc/apk/repositories && \
apk update && \
apk add --no-cache -U gcc g++ make openssl-dev pcre-dev expat-dev libtool && \
addgroup -S apache && \
adduser -DHS -s /sbin/nologin -G apache apache && \
tar xf apr-1.6.5.tar.gz && \
tar xf apr-util-1.6.1.tar.gz && \
tar xf httpd-2.4.46.tar.bz2 && \
sed -i '/$RM "$cfgfile"/d' apr-1.6.5/configure && \
cd apr-1.6.5 && \
./configure --prefix=/usr/local/apr && make && make install && \
cd ../apr-util-1.6.1 && \
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr &&
make && make install && \
cd ../httpd-2.4.46 && \
./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork && \
make && make install && \
sed -i '/^#ServerName/s/#//g' /etc/httpd24/httpd.conf && \
rm -rf /usr/src/* /var/cache/* && \
apk del gcc g++ make
9. 编写dockerfile
[root@localhost apache]# cat Dockerfile
FROM alpine
ENV PATH /usr/local/apache/bin:$PATH
COPY soft /usr/src/
COPY script /tmp/ 把script目录下的脚本copy到/tmp/下
WORKDIR /usr/src/
RUN /bin/sh /tmp/install.sh && \ 以/bin/sh来运行/tmp/install.sh
rm -rf /tmp/* 运行完删除该目录下的脚本,卸磨杀驴
EXPOSE 80 443
CMD ["-X"]
ENTRYPOINT ["httpd"]
10. 创建一个镜像
[root@localhost ~]# docker build -t test:v0.9 apache
......
Successfully built 98b6e064cbe6
Successfully tagged test:v0.9
......
11. 运行容器
[root@localhost ~]# docker run -itd --name web7 test:v0.9
552ef18b37b0042cc1adf225bf3a236923e453f1a1feff22e351eea2b0c17f91
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
552ef18b37b0 test:v0.9 "httpd -X" 3 seconds ago Up 2 seconds 80/tcp, 443/tcp web7
12. 进入容器查看
[root@localhost ~]# docker exec -it web7 /bin/sh
/usr/src # ls
/usr/src #
/usr/src # ls /tmp/ 此时该目录下的脚本已被删除
/usr/src #
/usr/src # exit
13. 访问网站
[root@localhost ~]# curl 172.17.0.7
<html><body><h1>It works!</h1></body></html>
3. lamp的dockerfile的编写
3.1 先编写httpd
创建目录,并把相应的包放在各目录下
[root@localhost ~]# tree .
.
├── anaconda-ks.cfg
├── apache
│ ├── Dockerfile
│ ├── script
│ │ └── install.sh
│ └── soft
│ ├── apr-1.6.5.tar.gz
│ ├── apr-util-1.6.1.tar.gz
│ └── httpd-2.4.46.tar.bz2
├── mysql
│ ├── Dockerfile
│ ├── script
│ └── soft
│ └── mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
└── php
├── Dockerfile
├── script
└── soft
└── php-7.2.8.tar.xz
9 directories, 10 files
3.2 编写mysql
[root@localhost mysql]# vim Dockerfile
COPY soft /usr/src/
ENV PATH /usr/local/mysql/bin:$PATH
WORKDIR /usr/src/
RUN echo 'https://mirrors.ustc.edu.cn/alpine/v3.12/main/' > /etc/apk/repositories && \
echo 'https://mirrors.ustc.edu.cn/alpine/v3.12/community/' >> /etc/apk/repositories && \
apk update && \
apk add --no-cache -U gcc g++ make ncurses-dev openssl-dev openssl cmake mariadb-dev && \
addgroup -S mysql && \
adduser -DHS -s /sbin/nologin -G mysql mysql && \
tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ && \
ln -s /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/mysql && \
chown -R mysql.mysql /usr/local/mysql* && \
mkdir /opt/data && \
chown -R mysql.mysql /opt/data
[root@localhost ~]# docker build -t test:v0.7 mysql
......
Successfully built 3da3b0158c4d
Successfully tagged test:v0.7
[root@localhost ~]# docker run -it --name web7 test:v0
/usr/src # ls
mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
/usr/src # which mysql
/usr/local/mysql/bin/mysql
/usr/src # cd /usr/local/
/usr/local # ls
bin
lib
mysql
mysql-5.7.31-linux-glibc2.12-x86_64
share
/usr/local # apk list mysql
mysql-10.4.15-r0 x86_64 {mariadb} (GPL-2.0-or-later)
/usr/local # apk list mysql*
mysql-10.4.15-r0 x86_64 {mariadb} (GPL-2.0-or-later)
/usr/local #