创建新镜像-从已创建的容器中更新镜像并提交镜像(以Nginx为例)

  • 2019 年 10 月 11 日
  • 筆記

目标:现在我们主要是修改nginx的index.html,然后做一个新镜像

1、基于nginx:1.12运行一个容器
docker run -d -p 8080:80 --name nginx nginx:1.12  
2、进入容器nginx的bash
docker exec -it nginx bash  
3、nginx.conf路径:/etc/nginx/nginx.conf,我们可看看里面的内容
root@5096ffe0b74f:/etc/nginx# cat nginx.conf    user  nginx;  worker_processes  1;    error_log  /var/log/nginx/error.log warn;  pid        /var/run/nginx.pid;      events {      worker_connections  1024;  }      http {      include       /etc/nginx/mime.types;      default_type  application/octet-stream;        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                        '$status $body_bytes_sent "$http_referer" '                        '"$http_user_agent" "$http_x_forwarded_for"';        access_log  /var/log/nginx/access.log  main;        sendfile        on;      #tcp_nopush     on;        keepalive_timeout  65;        #gzip  on;  	# 包含config.d/下的配置文件      include /etc/nginx/conf.d/*.conf;  }  
4、那么我们可以猜到,基本配置应该在conf.d的某个配置文件中
root@5096ffe0b74f:/etc/nginx# cd conf.d/  root@5096ffe0b74f:/etc/nginx/conf.d# ls  default.conf  

default.conf的内容:

server {      listen       80;      server_name  localhost;        #charset koi8-r;      #access_log  /var/log/nginx/host.access.log  main;        location / {          root   /usr/share/nginx/html;          index  index.html index.htm;      }        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html      #      error_page   500 502 503 504  /50x.html;      location = /50x.html {          root   /usr/share/nginx/html;      }  }  
5、最后我们可以到/usr/share/nginx/html下修改index.html:

我就简单修改成一句话。

欢迎来到Nginx!  
6、提交容器副本:
docker commit -m="update index.html" -a="Howinfun" nginx(容器ID或容器名) howinfun/nginx:1.0(创建的目标镜像名)  
7、docker images可以看到我们创建的镜像:
docker@default:~$ docker images  REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE  howinfun/nginx      1.0                 03fd1aac0888        12 minutes ago      155MB  nginx               1.12                4037a5562b03        17 months ago       108MB  ubuntu              15.10               9b9cb95443b5        3 years ago         137MB  
8、基于此镜像启动新容器:
docker run -d -p 8080:80 --name=hyfnginx howinfun/nginx:1.0  
9、可以查看绑定的宿主端口:
docker port hyfnginx -> 返回端口号  docker ps -> PORTS有端口号的信息  
10、浏览器访问Nginx:

因为我是在Windows上安装Docker,所以我们访问前必须查看default虚拟机的ip,打开Git Bash,输入命令:

Howinfun@LAPTOP-BHT98468 MINGW64 ~/Desktop  $ docker-machine ip default  192.168.99.100 -> default虚拟机的IP地址    

那么如果我们想通过Windows系统直接访问呢,那么可在Oracle VM VirtualBox配置端口转发。最后,我们可以访问192.168.99.100:8080或者127.0.0.1:8080都可以访问到nginx的主页。