Dockerfile writing
Dockerfile writing
| 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 |
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
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
docker build -t sshd:test .
docker run -d -p 111:22 sshd:test
ssh 127.0.0.1 -p 111
be based on SSHD Image continues to build
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
docker build -t systemctl:test .
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 '
docker exec -it systemctl /bin/bash
systemctl status sshd_config " Check docker Inside systemctl Can you use "
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
CMD ["/usr/local/src/tomcat.run.sh"]
'// 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
cat > Dockerfile<<EOF
FROM centos:7
MAINTAINER this is tomcat
ADD jdk-8u91-linux-x64.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv jdk1.8.0_91 java
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
ADD apache-tomcat-9.0.16.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv apache-tomcat-9.0.16 /usr/local/tomcat9
EXPOSE 8080
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
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
docker build -t nginx:test .
docker run -d -P nginx:test1
docker ps -a " View port number "
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 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
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 )
Format : ENTRYPOINT command param1 param2 (shell Pattern )
ENTRYPOINT ["executable","param1","param2"] (exec Pattern ) recommend