Dockerfile writing

osc_qgfjs4a5 2020-11-11 14:12:55
dockerfile writing


Dockerfile writing

 Dockerfile writing
# Writing specifications 
| Instructions | meaning |
| ------------------------------------ | ------------------------------------------------------------ |
| FROM Mirror image | Specify the image on which the new image is based , The first order must be FROM Instructions , For every image you create, you need a FROM Instructions |
| MAINTAINER name | Explain the maintainer information of the new image |
| RUN command | Execute the command on the image on which it is based , And commit to a new image |
| CMD [“ The program to run ”," Parameters 1"," Parameters 2"] | Specifies the command or script to run when starting the container ,Dockerfile There can only be one CMD command , If more than one is specified, only the last one is executed |
| EXPOSE Port number | Specifies that the new image is loaded into Docker Port to open when |
| ENV environment variable A variable's value | Set the value of an environment variable , It will be RUN Use |
| ADD Source file / Catalog Target file / Catalog | Copy the source file to the destination file , Source files should be associated with Dockerfile In the same directory , Or a URL |
| COPY Source file / Catalog Target file / Catalog | Put the source file on the local host / Copy directory to destination , Source file / The table of contents should be consistent with Dockerfile In the same directory |
| VOLUME [" Catalog "] | Create a mount point in the container |
| USER user name /UID | Specifies the user who runs the container |
| WORKDIR route | For the subsequent RUN、CMD、ENTRYPOINT assign work directory |
| ONBUILD command | Specifies the command to run when the generated image is used as a base image |
# How to handle shell Script changed to Dockerfile
1. stay shell Add before order RUN
2. Declare variables hold
for example echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH'>> /etc/profile
ENV PATH /usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
So that's how it works
3.ADD Transfer files from the host to the container ADD Including compression package decompression function "ADD Must be created in the host directory "
4.COPY and RUN cp It's the same , Refers to replication within the container
5.WORKDIR and cd Not much difference
6. Pay attention to the way the container starts itself
Such as :ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
Such as :CMD ["/usr/sbin/sshd","-D"] '// Start when container is loaded sshd service '
Such as :CMD ["/usr/sbin/init"]
Such as :CMD ["/run.sh"]
EOF
cat > run.sh <<EOF
#!/bin/bash
/usr/local/nginx/sbin/nginx
EOF
# One :------------------------------SSHD-Dockerfile
#1、 Create directory , To write Dockerfile file 
mkdir sshd && cd sshd
cat > Dockerfile<<EOF
FROM centos:7 '// Specify base image '
MAINTAINER this is centos7-sshd project '// Description information '
RUN yum -y update '// Update container yum Source '
RUN yum -y install openssh* net-tools losf telnet passwd '// Deployment environment tools '
RUN echo "123456"|passwd --stdin root '// Set up root The login password '
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config '// Ban ssh Medium pam verification '
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key '// Create asymmetric key , And specify the file path '
RUN sed -i '/^session\s\+required\s\+pam_loginuid.so/s/^/#/' /etc/pam.d/sshd '// Ban pam Of ssh Of pam Session module '
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh '// establish ssh Working directory and permission settings '
EXPOSE 22 '// to open up 22 port '
CMD ["/usr/sbin/sshd","-D"] '// Start when container is loaded sshd service '
EOF
#2. Create a mirror image 
docker build -t sshd:test .
#3. Start the container , And specify the port number as 1111
docker run -d -p 111:22 sshd:test
#4. Test landing 
ssh 127.0.0.1 -p 111
# Two .-------------------------------------Systemctl-Dockerfile
be based on SSHD Image continues to build
#1、 Create directory , To write Dockerfile file 
mkdir systemctl && cd systemctl
cat > Dockerfile<<EOF
FROM sshd:new
MAINTAINER built image systemctl <tang>
ENV container docker
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/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]
EOF
#2、 Create a mirror image 
docker build -t systemctl:test .
#3、 Create a container 
docker run --privileged -it --name systemctl -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemctl:test sbin/init
'//–privateged send container Internal root Have real root jurisdiction , No reduction of rights . otherwise ,container The internal user is just an ordinary external user '
#4. Open terminal test 
docker exec -it systemctl /bin/bash
systemctl status sshd_config " Check docker Inside systemctl Can you use "
# 3、 ... and .-------------------Tomcat-Dockerfile
1、 Create a directory and write Dockerfile file , Upload relevant files
mkdir tomcat && cd tomcat
cat > Dockerfile<<EOF
FROM centos:7
MAINTAINER this is tomcat <li>
ADD jdk-8u201-linux-x64.rpm /usr/local
WORKDIR /usr/local
RUN rpm -ivh jdk-8u201-linux-x64.rpm
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
ADD apache-tomcat-9.0.16.tar.gz /usr/local
WORKDIR /usr/local
RUN mv apache-tomcat-9.0.16 /usr/local/tomcat
EXPOSE 8080
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
EOF
# notes 
CMD ["/usr/local/src/tomcat.run.sh"]
#ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
'// In addition to using CMD, You can also use ENTRYPOINT'
'//CMD And ENTRYPOINT The difference between '
//ENRYPOINT Refers to that the image has executed the commands in brackets before opening the container
//CMD When you open the container , Instructions to execute , Set the default command and its parameters after the container is started , but CMD It can be docker run Replace with the command line argument
// be based on Dockerfile There are CMD perhaps ENTRYPOINT When creating a mirror ,docker run Don't add instructions to the back (/bin/bash) 了 , Will overwrite Dockerfile An error is reported in the instruction or syntax of
#### Another version of , Different jdk package , No need to install jdk, You can use it by decompressing it 
cat > Dockerfile<<EOF
FROM centos:7
MAINTAINER this is tomcat
# decompression java To directory /usr/local Next , build tomcat Environment 
ADD jdk-8u91-linux-x64.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv jdk1.8.0_91 java
# Set the environment variable 
ENV JAVA_HOME /usr/local/java
ENV JRE_HOME /usr/local/java/jre
ENV CLASSPATH /usr/local/java/lib:/usr/local/java/jre/lib
ENV PATH /usr/local/java/bin:$PATH
# Unpack the installation tomcat package 
ADD apache-tomcat-9.0.16.tar.gz /usr/local/
# Entry directory /usr/local/
WORKDIR /usr/local/
# Renaming is easy to manage 
RUN mv apache-tomcat-9.0.16 /usr/local/tomcat9
# Let go tomcat Service 8080 port 
EXPOSE 8080
# What I use here is catalina.sh File to start the ,startup.sh Files are ok .
ENTRYPOINT ["/usr/local/tomcat9/bin/catalina.sh","run"]
EOF
ls
apache-tomcat-9.0.16.tar.gz Dockerfile jdk-8u201-linux-x64.rpm
2、 Create a mirror image
docker build -t tomcat:test .
3、 Create a container
docker run -d -P tomcat:test
4、 test
# Four .-----------------Nginx-Dockerfile
#1、 Create a directory and write Dockerfile file 
mkdir nginx && cd nginx
cat > Dockerfile<<EOF
FROM centos:7
MAINTAINER this is kgc-nginx image <li>
RUN yum -y update
RUN yum install pcre pcre-devel zlib-devel gcc gcc-c++ make -y
RUN useradd -s /sbin/nologin nginx -M
ADD nginx-1.12.0.tar.gz /usr/local/src
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
RUN ln -s /usr/local/nginx-1.12.0/sbin/ /usr/local/bin/
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]
EOF
cat > run.sh <<EOF
#!/bin/bash
/usr/local/nginx/sbin/nginx
EOF
ls
//Dockerfile nginx-1.12.0.tar.gz run.sh
#2、 Create a mirror image 
docker build -t nginx:test .
#3、 Create a container , Designated port 
docker run -d -P nginx:test1
docker ps -a " View port number "
#4. Browser Test 
# 5、 ... and .------------------------Mysql5.7-Dockerfile
1、 Create a directory and write Dockerfile file
cat > Dockerfile<<EOF
FROM centos:7
MAINTAINER this is kgc-li
RUN yum -y update
RUN yum -y install gcc \
gcc-c++ \
make \
ncurses \
ncurses-devel \
bison \
cmake
RUN useradd -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/
WORKDIR /usr/local/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
RUN make && make install
RUN chown -R mysql.mysql /usr/local/mysql
RUN rm -rf /etc/my.cnf
ADD my.cnf /etc/my.cnf
RUN chown mysql:mysql /etc/my.cnf
ENV PATH /usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN echo 'export PATH' >> /etc/profile
RUN source /etc/profile
RUN /usr/local/mysql/bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /lib/systemd/system/
EXPOSE 3306
ADD run.sh /run.sh
RUN chmod 755 /run.sh
RUN chmod +x /run.sh
RUN sh /run.sh
CMD ["init"]
EOF
2、 Write configuration files and scripts , Upload source code package
cat > my.cnf<<EOF
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
EOF
cat >run.sh<<EOF
#!/bin/bash
systemctl enable mysqld
EOF
ls
Dockerfile mysql-boost-5.7.20.tar.gz run.sh
3、 Create a mirror image
docker build -t mysql:test .
4. Create a container
docker run -d -P --privileged mysql:test
" No addition --privileged Will report authority to refuse , No power down to start "
docker exec -it id /bin/bash
mysql -uroot -p
mysql> grant all privileges on *.* to 'root'@'%' identified by '123123';
mysql> grant all privileges on *.* to 'root'@'localhost' identified by '123123';
5. test
mysql -h 192.168.233.133 -uroot -p123123 -P 32776
" Can log in the interface normally "
#----------------------------------------Mysql The path to the script file 
mysql 5.6 Of service Service script mysql.server
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
mysql 5.7 Of systemctl Service script systemd.service
/usr/local/mysql/bin/mysqld/usr/lib/systemd/system/mysqld.service
cp /usr/local/mysql/bin/mysqld/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
mysql 5.6 Start script for mysql_safe
/usr/local/mysql/bin/mysqld_safe
mysql 5.7 Start script for
/usr/local/mysql/bin/mysqld
pide file
/usr/local/mysql/mysqld.pid
/usr/local/mysql/scripts/mysql_install_db Execute installation script
#CMD: Is the default command to execute when the container is started , Will be docker run Provided parameter coverage 
Format : CMD command param1 param2 (shell Pattern )
CMD ["executable","param1","param2"] (exec Pattern ) recommend
CMD ["param1","param2"] Provide to ENTRYPOINT Default parameters
give an example : CMD ["usr/sbin/nginx","-g","daemon off;"]
usr/sbin/nginx : nginx command
-g : Set global directives outside the configuration file
daemon off; : Background daemons open mode ( close )
#ENTRYPOINT: Is the default command to execute when the container is started , But it won't be docker run Provided parameter coverage 
Format : ENTRYPOINT command param1 param2 (shell Pattern )
ENTRYPOINT ["executable","param1","param2"] (exec Pattern ) recommend 
版权声明
本文为[osc_qgfjs4a5]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云