镜像制作
手动制作yum源nginx镜像
1、进入容器
$ docker run -it -p 8802:80
2、容器操作
$ yum install wget -y
$ rm -rf ./etc/yum.repos.d/*
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
$ wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
$ yum install -y vim wget pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
$ yum install nginx –y
$ vim /etc/nginx/nginx.conf
daemon off;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
# 添加location
location /pansn {
root /data/nginx/html;
index index.html;
}
$ mkdir -p /data/nginx/html/pansn
$ echo "test linux37" > /data/nginx/html/pansn/index.html
$ mv 1.jpg /data/nginx/html/pansn
3、创建镜像
重开一个终端窗口创建镜像
$ docker commit -a "1349019125@qq.com" -m "nginx yum v1" --change="EXPOSE 80 443" 373b8f018f4c docker.io/linux37/centos-nginx:v1
$ docker commit -a "1349019125@qq.com" -m "nginx yum v1" --change="EXPOSE 80 443" 05b7e99cf4b8 www.pansn.cn/linux37/centos-nginx:v1
4、启动镜像并传递命令nginx启动命令
$ docker run -d -it -p 82:80 www.pansn.cn/linux37/centos-nginx:v1 nginx
$ ss -ntl #确定82端口是否开启成功
验证
最后备份镜像导 到另外终端验证
$ docker save www.pansn.cn/linux37/centos-nginx:v1 > /opt/nginx-v1
$ ll /opt
-rw-r--r-- 1 root root 259636224 Sep 13 17:38 nginx-v1
root@docker-node1:~# scp /opt/nginx-v1 192.168.7.102:/opt
root@192.168.7.102's password:
nginx-v1 100% 428MB 105.7MB/s 00:04
root@docker-node1:~#
导入镜像并启动
root@docker-node2:/opt# docker load -i nginx-v1
877b494a9f30: Loading layer [==================================================>] 209.6MB/209.6MB
57ea5df43423: Loading layer [==================================================>] 239.4MB/239.4MB
Loaded image: www.pansn.cn/linux37/centos-nginx:v1
root@docker-node2:/opt# docker run -it -d www.pansn.cn/linux37/centos-nginx:v1
aaaf2d83949fccf833b26dc40310bbba7a747b7578d439e03bd795c7de21aa38
映射端口
$ docker run -it -d -p 80:80 www.pansn.cn/linux37/centos-nginx:v1 nginx
再次验证即可
DockerFile 制作 yum 版 nginx 镜像
DockerFile 可以说是一种可以被 Docker 程序解释的脚本,DockerFile 是由一条条的命令组成的,每条命令对应 linux 下面的一条命令,Docker 程序将这些DockerFile 指令再翻译成真正的 linux 命令,其有自己的书写方式和支持的命令,Docker 程序读取 DockerFile 并根据指令生成 Docker 镜像,相比手动制作镜像的方式,DockerFile 更能直观的展示镜像是怎么产生的,有了 DockerFile,当后期有额外的需求时,只要在之前的 DockerFile 添加或者修改响应的命令即可重新生成新的 Docke 镜像,避免了重复手动制作镜像的麻烦
官方dockerfile:https://github.com/nginxinc/docker-nginx/blob/master/stable/buster/Dockerfile
环境准备:
nginx软件:
nginx-1.16.1.tar.gz
nginx.conf
user nginx;
daemon off;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /linux37 {
root /data/nginx/html;
index index.html;
}
location /app {
root /data/nginx/html/linux37;
index index.html;
}
$ mkdir /data/nginx/html/linux37/apps -pv
$ vim /data/nginx/html/linux37/apps/index.html
app index.html
$ useradd nginx -u 2019
下载基础镜像 Centos:
$ docker pull centos
创建镜像目录:
$ mkdir /opt/dockerfile/{web/{nginx,tomcat,jdk,apache},system/{centos,ubuntu,redhat}} -pv
构建Nginx镜像
$ cd /opt/dockerfile/system/centos/
$ vim Dockerfile
# Nginx centos
FROM centos:latest
MAINTAINER pansn 134901925@qq.com
ENV PASSWD 123456
ENV USER nginx
RUN cd /opt && touch linux37.txt
#执行命令,将编译nginx的步骤执行一遍
RUN rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
RUN yum install -y vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
ADD nginx-1.16.1.tar.gz /usr/local/src
RUN cd /usr/local/src/nginx-1.16.1 && ./configure --prefix=/apps/nginx && make && make install && ln -sv /apps/nginx/sbin/nginx /usr/bin
RUN useradd nginx -u 2019 && mkdir /data/nginx/html/linux37 -pv
ADD nginx.conf /apps/nginx/conf/nginx.conf
ADD app.tar.gz /data/nginx/html/linux37
RUN chown -R nginx.nginx /data/nginx/ /apps/nginx
EXPOSE 80 443
CMD ["nginx"]
~
执行构建镜像:
$ docker build -t centos-nginx:v1 . #后面有个点表示在本目录执行Dockerfile
验证:
···
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-nginx latest 6095d33ccf33 2 hours ago 543MB
$ docker run -it --rm -p 80:80 centos-nginx:latest
···