003.Docker镜像制作之基于Dockerfile制作镜像

1. 制作带有sshd服务的CentOS6.9镜像

# 1.编写dockerfile,文件名必须是Dockerfile或者dockerfile

# FROM: 指定基础镜像,两种语法
# FROM centos:6.9
# FROM imageId
FROM 2199b8eb8390
# RUN: 进入容器后执行的命令,尽量少的使用新的RUN命令行,因为每次执行一次RUN,都会生成一个新的临时容器
# RUN有两种语法
# RUN command && command
# RUN ["command","params",...], 例如: ["mysqld", "--initialize-insecure", "--user=mysql", "--basedir=/usr/local/mysql", "--datadir=/data/mysql/data"]
RUN yum install wget -y && mv /etc/yum.repos.d/* /tmp/ && wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo && wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo && sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo && yum clean all && yum makecache && yum install openssh-server -y 
RUN /etc/init.d/sshd start && echo "123456" | passwd root --stdin
# 暴露端口
EXPOSE 22
# CMD: 启动镜像后执行的命令
CMD ["/usr/sbin/sshd", "-D"]

# 2. 使用Dockerfile构建镜像,docker会自动寻找指定目录下的dockerfile文件
[root@bdc01 dockerfile]# docker image build -t yyh/centos6.9-sshd:v1.0 ./
...
Successfully built 98452bf34065

# 3.启动容器做测试
[root@bdc01 dockerfile]# docker container run -d --name sshd yyh/centos6.9-sshd:v1.0
[root@bdc01 dockerfile]# docker ps -a
CONTAINER ID  IMAGE                    COMMAND              CREATED        STATUS        PORTS   NAMES
b482cfda75ca  yyh/centos6.9-sshd:v1.0  "/usr/sbin/sshd -D"  8 seconds ago  Up 7 seconds  22/tcp  sshd
[root@bdc01 dockerfile]# docker container inspect b482cfda75ca | grep -i ipaddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",
[root@bdc01 dockerfile]# ssh 172.17.0.2
[email protected]'s password: 
[root@b482cfda75ca ~]# exit
logout
Connection to 172.17.0.2 closed.

2. 制作带有LAMP+BBS系统的CentOS6.9镜像

# 1.准备好bbs系统的安装包,bbs.tar.gz
wget https://github.com/Discuz-X/DiscuzX/archive/Common.zip
unzip Common.zip
cd DiscuzX-Common
tar -zcvf bbs.tar.gz ./*
# 这样就可以得到bbs.tar.gz

# 2.准备一个脚本init.sh,在制作镜像时,容器中需要执行这个脚本来启动服务和初始化MySQL,其中内容如下
#!/bin/bash
/etc/init.d/mysqld start
mysql -e "grant all on *.* to root@'%' identified by '123456';grant all on *.* to discuz@'%' identified by '123456';flush privileges;create database discuz charset utf8;"
/etc/init.d/httpd start
/usr/sbin/sshd -D

# 3.编写dockerfile文件
[root@bdc01 lamp]# vim Dockerfile

FROM 2199b8eb8390
RUN yum install wget -y && mv /etc/yum.repos.d/* /tmp/ && wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo && wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo && sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo && yum clean all && yum makecache && yum install openssh-server httpd mysql mysql-server php php-mysql unzip -y
RUN /etc/init.d/sshd start && echo "123456" | passwd root --stdin && /etc/init.d/mysqld start && /etc/init.d/httpd start
# RUN mysql -e "grant all on *.* to root@'%' identified by '123456';grant all on *.* to discuz@'%' identified by '123456';flush privileges;create database discuz charset utf8;"
# COPY: 将宿主机的与Dockerfile在同一目录的文件复制到容器内,支持通配符,如果拷贝是目录,只能拷贝目录下的子目录和子文件
COPY init.sh /
# ADD: 将压缩包复制到容器内并自动解压,仅支持.tar.xxx格式的压缩包
# ADD支持URL的方式,例如:ADD https://mirrors.bfsu.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz /tmp/
# 如果是从网络链接下载文件,那么就不会自动解压
ADD bbs.tar.gz /var/www/html/
RUN chmod 777 -R /var/www/html/*
EXPOSE 22
EXPOSE 80
EXPOSE 3306
CMD ["/bin/bash", "/init.sh"]

# 4.制作镜像
[root@bdc01 lamp]# ll
total 17324
-rw-r--r-- 1 root root 17727527 2020-10-30 13:23 bbs.tar.gz
-rw-r--r-- 1 root root      896 2020-10-30 13:30 Dockerfile
-rw-r--r-- 1 root root      250 2020-10-30 13:22 init.sh
[root@bdc01 lamp]# docker image build -t yyh/centos6.9-lamp:v1.0 ./
...
Successfully built f2ea7a774e2e

# 5.测试
[root@bdc01 lamp]# docker images
REPOSITORY           TAG           IMAGE ID            CREATED             SIZE
yyh/centos6.9-lamp   v1.0          f2ea7a774e2e        7 minutes ago       746 MB
nginx                latest        f35646e83998        2 weeks ago         133 MB
centos               latest        0d120b6ccaa8        2 months ago        215 MB
centos               6.9           2199b8eb8390        19 months ago       195 MB
centos               7.5.1804      cf49811e3cdb        19 months ago       200 MB
centos               7.6.1810      f1cb7c7d58b7        19 months ago       202 MB
[root@bdc01 lamp]# docker container run -d -p 8080:80 -p 2222:22 -p 33060:3306 --name centos6.9-lamp-bbs yyh/centos6.9-lamp:v1.0
9ae62b1f7060e3395dfb3f6f6127a527d6e91682e03850542d66be3e9e8de703
[root@bdc01 lamp]# docker container ls -a
CONTAINER ID  IMAGE                    COMMAND               CREATED         STATUS         PORTS                                                                 NAMES
9ae62b1f7060  yyh/centos6.9-lamp:v1.0  "/bin/bash /init.sh"  21 seconds ago  Up 21 seconds  0.0.0.0:2222->22/tcp, 0.0.0.0:8080->80/tcp, 0.0.0.0:33060->3306/tcp   centos6.9-lamp-bbs

3. 其他Dockerfile指令

# ENV: 设置环境变量
ENV HTMLDIR /var/www/html/
ADD bbs.tar.gz ${HTMLDIR}

# VOLUMN: 数据卷映射
# 设置挂载点,无法设置宿主机的目录,自动生成宿主机的目录,具体/dir1和/dir2对应宿主机哪个目录,可以通过查看容器详情来查看
VOLUMN ["/dir1", "/dir2"]

# WORKDIR:设置工作目录,设置之后,后续的RUN、CMD、ENTRYPOINT、COPY或者ADD指令都会在指定的WORKDIR中执行
# WORKDIR指令可以在Dockerfile中多次使用,如果提供了一个相对路径,它就是相对于前面的WORKDIR目录的路径
# 例如
# WORKDIR /a
# WORKDIR b
# WORKDIR c
# 那么实际的WORKDIR是/a/b/c
WORKDIR /var/www/html

# ENTRYPOINT,ENTRYPOINT和CMD的用法是一样的
# 如果使用CMD设置容器启动时执行/bin/bash/init.sh
# 那么这个操作可以被外部命令覆盖
# 例如启动容器:docker container run -d yyh/centos6.9-lamp:v1.0 /bin/bash
# 最后指定的/bin/bash就会覆盖掉CMD指定的命令
# 而使用ENTRYPOINT设置的命令是不会被覆盖
ENTRYPOINT ["/bin/bash", "/init.sh"]

你可能感兴趣的:(003.Docker镜像制作之基于Dockerfile制作镜像)