• 基于Dockerfile创建镜像实战


    SSH镜像

    1. cd /opt/sshd
    2. vim Dockerfile
    1. #第一行必须指明基于的基础镜像
    2. FROM centos:7
    3. #作者信息
    4. MAINTAINER this is ssh image <hmj>
    5. #镜像的操作指令
    6. RUN yum -y update
    7. RUN yum -y install openssh* net-tools lsof telnet passwd
    8. RUN echo 'abc1234' | passwd --stdin root
    9. RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config                     
    10.  #不使用PAM认证
    11. RUN sed -ri '/^session\s+required\s+pam_loginuid.so/ s/^/#/' /etc/pam.d/sshd  
    12.  #取消pam限制
    13. RUN ssh-keygen -t rsa -A                                                      
    14. #生成密钥认证文件
    15. RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
    16. EXPOSE 22
    17. CMD ["/usr/sbin/sshd" , "-D"]            #/usr/sbin/sshd -D 用于前台启动sshd服务
    1. //生成镜像
    2. docker build -t sshd:centos .
    3. //启动容器并修改root密码
    4. docker run -d -P sshd:centos
    5. docker ps -a
    6. ssh localhost -p 49155

    Systemctl镜像

    1. mkdir /opt/systemctl
    2. cd /opt/systemctl
    1. vim Dockerfile
    2. FROM sshd:centos
    3. MAINTAINER this is systemctl image <hmj>
    4. ENV container docker
    5. #除了systemd-tmpfiles-setup.service,删除其它所有文件
    6. RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \    
    7. rm -f /lib/systemd/system/multi-user.target.wants/*; \
    8. rm -f /etc/systemd/system/*.wants/*; \
    9. rm -f /lib/systemd/system/local-fs.target.wants/*; \
    10. rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
    11. rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
    12. rm -f /lib/systemd/system/basic.target.wants/*;\
    13. rm -f /lib/systemd/system/anaconda.target.wants/*;
    14. VOLUME [ "/sys/fs/cgroup" ]
    15. #CMD ["/usr/sbin/init"]
    1. //生成镜像
    2. docker build -t systemd:centos .
    3. //启动容器,并挂载宿主机目录挂载到容器中,和进行初始化
    4. docker run --privileged -d -P -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:centos /sbin/init
    5. #--privileged:使container内的root拥有真正的root权限。
    6. 否则,container内的root只是外部的一个普通用户权限。
    7. docker ps -a
    8. //进入容器
    9. docker exec -it a0d624d2bfa9 bash
    10. systemctl status sshd
    11. 方法二:
    12. docker run --privileged -it -P -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:centos /sbin/init &

    nginx镜像

    1. mkdir /opt/nginx
    2. cd /opt/nginx/
    3. cp /opt/nginx-1.12.0.tar.gz /opt/nginx
    1. vim Dockerfile
    2. #基于基础镜像
    3. FROM centos:7
    4. #用户信息
    5. MAINTAINER this is nginx image <hmj>
    6. #添加环境包
    7. RUN yum -y update
    8. RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
    9. RUN useradd -M -s /sbin/nologin nginx
    10. #上传nginx软件压缩包,并解压
    11. ADD nginx-1.12.0.tar.gz /opt/
    12. #指定工作目录
    13. WORKDIR /opt/nginx-1.12.0
    14. RUN ./configure \
    15. --prefix=/usr/local/nginx \
    16. --user=nginx \
    17. --group=nginx \
    18. --with-http_stub_status_module && make && make install
    19. ENV PATH /usr/local/nginx/sbin:$PATH
    20. #指定http和https端口
    21. EXPOSE 80
    22. EXPOSE 443
    23. RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf          
    24. #关闭 nginx 在后台运行
    25. #添加宿主机中run.sh到容器中
    26. ADD run.sh /run.sh
    27. RUN chmod 755 /run.sh
    28. CMD ["/run.sh"]
    29. #CMD ["/usr/local/sbin/nginx", "-g", "daemon off;"]
    1. vim run.sh
    2. #!/bin/bash
    3. /usr/local/nginx/sbin/nginx
    1. //创建新镜像
    2. docker build -t nginx:centos .
    3. docker run -d -P nginx:centos
    4. docker ps -a
    5. http://20.0.0.50:32769

    tomcat 镜像

    1. mkdir /opt/tomcat
    2. cd /opt/tomcat
    3. cp /opt/jdk-8u91-linux-x64.tar.gz /opt/tomcat
    4. cp /opt/apache-tomcat-8.5.16.tar.gz /opt/tomcat
    1. vim Dockerfile
    2. FROM centos:7
    3. MAINTAINER this is tomcat image <hmj>
    4. ADD jdk-8u91-linux-x64.tar.gz /usr/local/
    5. WORKDIR /usr/local/
    6. RUN mv jdk1.8.0_91 /usr/local/java
    7. ENV JAVA_HOME /usr/local/java
    8. ENV JRE_HOME ${JAVA_HOME}/jre
    9. ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
    10. ENV PATH $JAVA_HOME/bin:$PATH
    11. ADD apache-tomcat-8.5.16.tar.gz /usr/local/
    12. WORKDIR /usr/local/
    13. RUN mv apache-tomcat-8.5.16 /usr/local/tomcat
    14. EXPOSE 8080
    15. #CMD ["/usr/local/tomcat/bin/catalina.sh","run"]
    16. ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
    1. //创建新镜像
    2. docker build -t tomcat:centos .
    3. docker run -d --name tomcat01 -p 1216:8080 tomcat:centos 
    4.  
    5. http://20.0.0.50:1216

    mysql镜像

    1. mkdir /opt/mysqld
    2. cd /opt/mysqld
    1. vim Dockerfile
    2. FROM centos:7
    3. MAINTAINER this is mysql image <hmj>
    4. RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
    5. RUN useradd -M -s /sbin/nologin  mysql
    6. ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
    7. WORKDIR /usr/local/src/mysql-5.7.20/
    8. RUN cmake \
    9. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
    10. -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
    11. -DSYSCONFDIR=/etc \
    12. -DSYSTEMD_PID_DIR=/usr/local/mysql \
    13. -DDEFAULT_CHARSET=utf8  \
    14. -DDEFAULT_COLLATION=utf8_general_ci \
    15. -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    16. -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
    17. -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
    18. -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
    19. -DMYSQL_DATADIR=/usr/local/mysql/data \
    20. -DWITH_BOOST=boost \
    21. -DWITH_SYSTEMD=1 && make && make install
    22. RUN chown -R mysql:mysql /usr/local/mysql/
    23. RUN rm -rf /etc/my.cnf
    24. ADD my.cnf /etc/
    25. RUN chown mysql:mysql /etc/my.cnf
    26. ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
    27. WORKDIR /usr/local/mysql/
    28. RUN bin/mysqld \
    29. --initialize-insecure \
    30. --user=mysql \
    31. --basedir=/usr/local/mysql \
    32. --datadir=/usr/local/mysql/data
    33. RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
    34. EXPOSE 3306
    35. ADD run.sh /usr/local/src
    36. RUN chmod 755 /usr/local/src/run.sh
    37. RUN sh /usr/local/src/run.sh
    38. #CMD ["/usr/sbin/init"]
    1. vim my.cnf
    2. [client]
    3. port = 3306
    4. default-character-set=utf8
    5. socket = /usr/local/mysql/mysql.sock
    6. [mysql]
    7. port = 3306
    8. default-character-set=utf8
    9. socket = /usr/local/mysql/mysql.sock
    10. [mysqld]
    11. user = mysql
    12. basedir = /usr/local/mysql
    13. datadir = /usr/local/mysql/data
    14. port = 3306
    15. character_set_server=utf8
    16. pid-file = /usr/local/mysql/mysqld.pid
    17. socket = /usr/local/mysql/mysql.sock
    18. server-id = 1
    19. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,
    20. NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,
    21. PIPES_AS_CONCAT,ANSI_QUOTES
    1. vim run.sh
    2. #!/bin/bash
    3. /usr/local/mysql/bin/mysqld    
    4. systemctl enable mysqld
    1. //创建新镜像
    2. docker build -t mysql:centos .
    3. //启动容器,并进行初始化
    4. docker run --name=mysql_server -d -P --privileged mysql:centos /usr/sbin/init
    1. //进容器给权限
    2. docker ps -a
    3. //进入容器,授权远程连接 mysql
    4. docker exec -it f9a4d8f6c65f /bin/bash
    5. mysql -u root -p
    6. grant all privileges on *.* to 'root'@'%' identified by 'abc123';
    7. grant all privileges on *.* to 'root'@'localhost' identified by 'abc123';
    8. flush privileges;
    9. //在客户端连接mysql容器
    10. mysql -h 20.0.0.50 -u root -P 49153 -pabc123

  • 相关阅读:
    【ybtoj】二分算法例题
    Redis进军磁盘存储
    CISP-PTE真题演示
    86.Linux系统下复制进程fork(逻辑地址和物理地址)
    Nodejs+vue+Elementui课程教学网站系统
    【网安神器篇】——wmic_info信息收集工具
    ​Kali-linux攻击路由器​
    Tomcat下载安装以及配置(详细教程)
    P02014186陈镐镐
    vue3.0+ant design vue3.2.6框架的基本使用
  • 原文地址:https://blog.csdn.net/weixin_54959847/article/details/133638582