还不了解什么是Dockerfile的小伙伴可以查看我之前的博客:
https://blog.51cto.com/14557905/2489466
Dockerfile镜像制作实战
Dockerfile制作镜像的三个步骤
一、制作sshd镜像
1.编写Dockerfile文件
[root@docker sshd]# vim Dockerfile
#基础镜像
FROM centos:7
#用户信息
MAINTAINER this is sshd project
#基于基础镜像系统更新,软件安装
RUN yum -y update
RUN yum -y install openssh* net-tools lsof telnet passwd
#设置用户密码
RUN echo '123456' | passwd --stdin root
#关闭PAM认证
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
#添加非对称密钥
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
#关闭pam会话模块
RUN sed -i '/^session\s\+requied\s\+pam_loginuid.so/s/^/#/' /etc/pam.d/sshd
#创建ssh工作目录,设置权限
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
#设置端口号
EXPOSE 22
#启动容器时执行操作
CMD ["/usr/sbin/sshd","-D"]
2.创建镜像
[root@docker sshd]# docker build -t sshd:new .
[root@docker sshd]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sshd new 43e794cf8abd 19 seconds ago 585MB
centos 7 5e35e350aded 5 months ago 203MB
3.创建容器验证
[root@docker sshd]# docker run -d -P sshd:new
[root@docker sshd]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99cd8001ff28 sshd:new "/usr/sbin/sshd -D" 4 seconds ago Up 3 seconds 0.0.0.0:32768->22/tcp compassionate_beaver
#宿主机使用ssh登陆容器
[root@docker sshd]# ssh localhost -p 32768
The authenticity of host '[localhost]:32768 ([::1]:32768)' can't be established.
RSA key fingerprint is SHA256:DND5bet+Io1sjjiKpNS6BvoeJC+YD07ejhRcp7s8VtQ.
RSA key fingerprint is MD5:a4:0e:3d:5a:18:1e:b2:17:85:f6:df:a5:ce:ab:d2:82.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:32768' (RSA) to the list of known hosts.
root@localhost's password:
[root@2a9bcdd7681f ~]#
二、创建systemctl镜像
创建完成sshd镜像后,无法使用systemctl status sshd命令查看ssh状态,可以根据sshd的镜像添加systemctl功能
1.编写Dockerfile文件
[root@docker systemctl]# vim Dockerfile
#基础镜像
FROM sshd:new
#设置环境变量
ENV container docker
#进入指定目录,用for循环遍历目录下所有文件并删除指定文件
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*; \
rm -f /etc/systemd/system/*.wants/*; \
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*; \
rm -f /lib/systemd/system/anaconda.target.wants/*;
#挂载宿主机的cgrups卷,以便执行容器
VOLUME ["/sys/fs/cgroup"]
#执行初始化命令
CMD ["/usr/sbin/init"]
2.创建镜像
[root@docker systemctl]# docker build -t systemctl:new .
[root@docker systemctl]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
systemctl new b029bbbd8a6b 9 minutes ago 585MB
sshd new 4b69240205be 33 minutes ago 585MB
centos 7 5e35e350aded 5 months ago 203MB
3.创建容器验证
[root@docker systemctl]# docker run --privileged -it -v /sys/fs/cgroup/:/sys/fs/cgroup:ro systemctl:new /sbin/init
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d98f478ccaf3 systemctl:new "/sbin/init" 2 minutes ago Up 2 minutes 22/tcp practical_fermat
[root@docker ~]# docker exec -it d98f478ccaf3 bash
#可正常使用systemctl命令查看
[root@d98f478ccaf3 /]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:sshd(8)
man:sshd_config(5)
三、创建nginx镜像(手工编译)
1.编写dockerfile文件
[root@docker nginx]# vim Dockerfile
#基础镜像
FROM centos:7
#用户信息
MAINTAINER This is Nginx test
#基于基础镜像系统更新,环境包安装
RUN yum -y update
RUN yum -y install make gcc gcc-c++ pcre-devel zlib-devel tar
#创建nginx用户
RUN useradd -M -s /sbin/nologin nginx
#上传nginx软件包,解压(使用ADD选项可以直接解压上传的包)
COPY nginx-1.12.2.tar.gz /opt/
RUN tar zxvf /opt/nginx-1.12.2.tar.gz -C /opt
#在指定目录下,开始编译安装nginx
WORKDIR /opt/nginx-1.12.2/
RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
RUN make && make install
#设置环境变量
ENV PATH /usr/local/nginx/sbin:$PATH
#指定端口
EXPOSE 80
#修改Nginx配置文件,以非daemon方式启动
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
#添加运行脚本,设置权限
ADD run.sh /run.sh
RUN chmod 755 /run.sh
#指定容器开启时执行的脚本
CMD ["/run.sh"]
2.编写run.sh启动脚本,将nginx软件包上传
[root@docker nginx]# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
3.创建镜像
[root@docker nginx]# docker build -t nginx:new .
[root@docker nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx new 24b44e614799 14 seconds ago 585MB
centos 7 5e35e350aded 5 months ago 203MB
4.创建容器验证
#创建容器,随机生成映射端口
[root@docker nginx]# docker run -d -P nginx:new
#查看容器及映射的端口号
[root@docker nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b51160232ad8 nginx:new "/run.sh" 4 seconds ago Up 3 seconds 0.0.0.0:32775->80/tcp sad_rhodes
浏览器中访问验证
四、创建Tomcat镜像
1.编写Dockerfile文件
[root@docker tomcat]# vim Dockerfile
#基础镜像
FROM centos:7
#用户信息
MAINTAINER Tomcat test
#基于基础镜像更新系统
RUN yum -y update
#安装java环境包
COPY jdk-8u201-linux-x64.rpm /opt/
RUN rpm -ivh /opt/jdk-8u201-linux-x64.rpm
#设置java的环境变量
ENV JAVA_HOME /usr/java/jdk1.8.0_201-amd64
ENV CLASSPATH $JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
ENV PATH $JAVA_HOME/bin:$PATH
#安装tomcat
ADD apache-tomcat-9.0.16.tar.gz /opt/tomcat
RUN mv /opt/tomcat/apache-tomcat-9.0.16/ /usr/local/tomcat
#指定端口
EXPOSE 8080
#指定容器启动时的操作,此处的ENTERYPOINT也可更换为CMD
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
2.将所需安装包上传至当前目录下
[root@docker tomcat]# ls
apache-tomcat-9.0.16.tar.gz Dockerfile jdk-8u201-linux-x64.rpm
3.镜像制作
[root@docker tomcat]# docker build -t tomcat:new .
[root@docker tomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat new 8b86bbc483a4 3 minutes ago 981MB
centos 7 5e35e350aded 5 months ago 203MB
4.创建容器验证
[root@docker tomcat]# docker run -d -P tomcat:new
[root@docker tomcat]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3d94d5eddca0 tomcat:new "/usr/local/tomcat/b…" 3 seconds ago Up 2 seconds 0.0.0.0:32776->8080/tcp optimistic_dubinsk
5.在浏览器上访问验证
创建mysql镜像
1.编写Dockerfile文件
[root@docker mysql]# vim Dockerfile
#基础镜像
FROM centos:7
#用户信息
MAINTAINER This is MySQL Project
#基于基础镜像系统更新和环境包安装
RUN yum -y update
RUN yum install -y ncurses-devel autoconf cmake gcc gcc-c++ make pcre-devel expat-devel pcre
#添加mysql-5.6安装包
ADD mysql-5.6.26.tar.gz /opt
#进入指定目录编译安装mysql
WORKDIR /opt/mysql-5.6.26
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DSYSCONFIDIR=/etc \
-DMYSQL_DATADIR=/home/mysql/ \
-DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock
RUN make && make install
#添加mysql配置文件
RUN rm -f /etc/my.cnf
RUN cp /opt/mysql-5.6.26/support-files/my-default.cnf /etc/my.cnf
#添加mysql的启动脚本,设置权限
RUN cp /opt/mysql-5.6.26/support-files/mysql.server /etc/init.d/mysqld
RUN chmod 755 /etc/init.d/mysqld
#设置环境变量
ENV PATH $PATH:/usr/local/mysql/bin
#创建mysql用户,更改mysql安装目录的权限
RUN useradd -s /sbin/nologin mysql
RUN chown -R mysql:mysql /usr/local/mysql/
#初始化数据库
RUN /usr/local/mysql/scripts/mysql_install_db \
--user=mysql \
--ldata=/var/lib/mysql \
--basedir=/usr/local/mysql \
--datadir=/home/mysql
#建立sock文件软连接,更改启动脚本文件
RUN ln -s /var/lib/mysql/mysql.sock /home/mysql/mysql.sock
RUN sed -i '46 s/basedir=/basedir=\/usr\/local\/mysql/' /etc/init.d/mysqld
RUN sed -i '47 s/datadir=/datadir=\/home\/mysql/' /etc/init.d/mysqld
#指定端口
EXPOSE 3306
#指定容器启动时的操作
CMD ["mysqld_safe"]
2.将安装包上传到当前目录
[root@docker mysql]# ls
Dockerfile mysql-5.7.26.tar.gz
3.创建镜像
[root@docker mysql]# docker build -t mysqld:new .
[root@docker mysql]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysqld new 9b2703bd15c0 54 minutes ago 5.11GB
centos 7 5e35e350aded 5 months ago 203MB
4.创建容器验证
[root@docker mysql]# docker run -d -P mysqld:new
[root@docker mysql]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fff4ac6a524f mysqld:new "mysqld_safe" 4 seconds ago Up 3 seconds 0.0.0.0:32777->3306/tcp infallible_babbage
#进入容器设置mysql的权限账户
[root@docker mysql]# docker exec -it fff4ac6a524f /bin/bash
[root@fff4ac6a524f ~]# mysql -uroot -p
mysql> grant all privileges on *.* to 'root'@'localhost' identified by '123456';
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
#在宿主机安装mysql客户端测试验证
[root@docker mysql]# yum install mariadb -y
[root@docker ~]# mysql -h 192.168.7.168 -uroot -p123456 -P 32777
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
总结:
以上就是一些常用的Dockerfile创建镜像的案例,在使用Dockerfile创建镜像时,最重要就是编写dockerfile文件的过程,我们需要知道每一步操作的意义,而且要细心不能因为当中一些正则表达式的使用而出错。