docker镜像
docker 镜像没有内核也没有内核文件,是直接调用的宿主机内核,通过宿主机内核调用硬件。
制作镜像的两种方式
- 基于正在运行中的容器制作镜像
- 基于dockerfile制作镜像
第一种方式制作
制作yum安装的nginx镜像
- 下载基础镜像
docker pull centos:7.7.1908
- 基于基础镜像yum安装nginx
1.启动基础镜像
docker run -d -it -p 8080:80 --name nginx01 centos:7.7.1908
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1bfda33fff2a centos:7.7.1908 "/bin/bash" 11 minutes ago Up 11 minutes 0.0.0.0:8080->80/tcp nginx01
2. 进入镜像yum
docker exec -it nginx01 /bin/bash
3.###安装epel-release以及nginx,常用工具
yum -y install epel-release
yum -y install nginx vim wget iotop net-tools psmisc
4.创建网页文件(代码存放位置)
[root@1bfda33fff2a ~]# cat /usr/share/nginx/html/index.html
Time Rovers
5.启动nginx并访问映射的主机端口
nginx
6.查看容器内端口
[root@1bfda33fff2a ~]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 49/nginx: master pr
tcp6 0 0 :::80 :::* LISTEN 49/nginx: master pr
7.关闭nginx后台启动
vim /etc/nginx/nginx.conf
daemon off;
8.退出容器#因为是后台启动的,所以可以退出
exit
- 将容器提交成镜像
[root@localhost ~]# docker commit 1bfda33fff2a nginx:1.16.1
sha256:184ae68f61d90cc8e9fc39cced5862d639a6cfd63dca07aec35644c292df16d7
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.16.1 184ae68f61d9 31 seconds ago 446MB
centos 7.7.1908 08d05d1d5859 16 months ago 204MB
- 运行新生成nginx:1.16.1
注意:后边加nginx命令,否则会是bash运行
docker run -d -it -p 8090:80 --name nginx1.16.1 nginx:1.16.1 nginx
如果不修改之前容器配置文件daemon off,可以如下启动
docker run -d -it -p 8090:80 --name nginx1.16.1 nginx:1.16.1 nginx "-g daemon off;"
- 访问8090
[root@localhost ~]# curl http://192.168.8.10:8090
Time Rovers
制作编译安装的nginx镜像
- 基于基础镜像编译安装nginx
1.启动基础镜像
docker run -d -it -p 8080:80 --name nginx02 centos:7.7.1908
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
22cbbd54f92f centos:7.7.1908 "/bin/bash" 5 seconds ago Up 4 seconds 0.0.0.0:8080->80/tcp nginx02
2. 进入镜像
docker exec -it nginx02 /bin/bash
3.安装常用工具以及编译环境
yum -y install vim wget iotop net-tools psmisc gcc gcc-c++ make pcre-devel zlib-devel
4.下载nginx源码包
wget http://nginx.org/download/nginx-1.16.1.tar.gz
5.编译安装nginx
tar -zxf nginx-1.16.1.tar.gz
./configure --prefix=/apps/nginx
make
make install
ln -s /apps/nginx/sbin/nginx /usr/bin/
6.启动
nginx
7.查看容器内端口
[root@22cbbd54f92f ~]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5160/nginx: master
8.关闭nginx后台启动
vim /etc/nginx/nginx.conf
daemon off;
8.退出容器#因为是后台启动的,所以可以退出
exit
- 将容器提交成镜像
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
22cbbd54f92f centos:7.7.1908 "/bin/bash" 27 minutes ago Up 26 minutes 0.0.0.0:8080->80/tcp nginx02
[root@localhost ~]# docker commit 22cbbd54f92f nginx:v2
sha256:b0cf58c9769aae7341a9fc08f2f83306b5d684d6fe840d40c3d4171b421e107e
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v2 b0cf58c9769a 7 seconds ago 484MB
centos 7.7.1908 08d05d1d5859 16 months ago 204MB
- 运行新生成nginx:v2
注意:后边加nginx命令,否则会是bash运行
docker run -d -it -p 8090:80 --name nginxv2 nginx:v2 nginx
如果不修改之前容器配置文件daemon off,可以如下启动
docker run -d -it -p 8090:80 --name nginxv2 nginx:v2 nginx "-g daemon off;"
- 访问8090
[root@localhost ~]# curl http://192.168.8.10:8090
Time Rovers