docker镜像分为两种,一种是手动构建,另一种是自动构建(dockerfile)

1.手动构建

基于centos镜像进行构建,制作nginx镜像

docker run --name ccku -it centos
yum -y install wget
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install nginx

修改nginx的配置文件,前台运行

vi /etc/nginx/nginx.conf
    daemon off;

修改完成后退出容器

[root@919 docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
de134ed9dfd0        centos              "/bin/bash"         2 minutes ago       Exited (0) 4 seconds ago                       ccku

修改完之后需要commit

[root@919 docker]# docker commit -m "nginx" de134ed9dfd0 ccku/ccku:v1
sha256:6b7698d16bf47f78811e887ec5f59fc06bcbb78726f2d6d11b7e0092d796c5b5
[root@919 docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ccku/ccku           v1                  6b7698d16bf4        7 seconds ago       329MB
centos              latest              0d120b6ccaa8        5 weeks ago         215MB

#
# -m 描述
# 容器ID
# 第一个ccku是仓库的名称
# 第二个ccku:v1是镜像的名称及标签

启动制作好的nginx镜像

[root@919 docker]# docker run --name nginxv1 -d -p 81:80 ccku/ccku:v1 nginx
1f434e0d2685fabdadab8495969bd8d1a0ad2bed1d26c20a5c4c922781c1a8d7
[root@919 docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                NAMES
1f434e0d2685        ccku/ccku:v1        "nginx"             3 seconds ago       Up 3 seconds        0.0.0.0:81->80/tcp   nginxv1

docker构建镜像的两种方式

查看nginx的访问日志

[root@919 ~]# docker exec 1f434e0d2685 tail -f /var/log/nginx/access.log
172.17.0.1 - - [19/Sep/2020:13:05:57 +0000] "GET / HTTP/1.1" 200 4057 "-" "curl/7.29.0" "-"
117.136.78.108 - - [19/Sep/2020:13:07:07 +0000] "GET / HTTP/1.1" 200 4057 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
117.136.78.108 - - [19/Sep/2020:13:07:07 +0000] "GET /nginx-logo.png HTTP/1.1" 200 368 "http://106.13.67.120:81/" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
117.136.78.108 - - [19/Sep/2020:13:07:08 +0000] "GET /poweredby.png HTTP/1.1" 200 4148 "http://106.13.67.120:81/" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
117.136.78.108 - - [19/Sep/2020:13:07:08 +0000] "GET /favicon.ico HTTP/1.1" 404 3971 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"

2.自动构建(dockerfile)

[root@919 ~]# mkdir -p /dockerfile/nginx
[root@919 ~]# cd !$
cd /dockerfile/nginx

[root@919 nginx]# vim Dockerfile
# Dockerfile

# Base Image
FROM centos

# Maintainer
MAINTAINER ccku

# Commands
RUN rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
RUN yum -y install nginx 
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
ADD index.html /usr/share/nginx/html/index.html
EXPOSE 80
CMD ["nginx"]
[root@919 nginx]# echo "www.ccku.cn" >>index.html

docker build进行构建,"."代表的是构建的位置

[root@919 nginx]# docker build -t mynginx:v2 .

查看构建结果

[root@919 nginx]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mynginx             v2                  1c8743910d8c        5 seconds ago       340MB
ccku/ccku           v1                  6b7698d16bf4        16 minutes ago      329MB
centos              latest              0d120b6ccaa8        5 weeks ago         215MB

启动镜像

[root@919 nginx]# docker run --name mynginxv2 -d -p 82:80 mynginx:v2
25534d5924f020e0e64335839b4e8f05bd600d13f2e17f6cf879d0c8a1eb3d87

docker构建镜像的两种方式