In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you an introduction and how to use Dockerfile, and analyze and describe it from a professional point of view. I hope you can get something after reading this article.
1. Dockerfile
Dockerfile is the description file of the image file in docker. To put it bluntly, what the image file is made up of step by step.
For example, you bought a clothes hanger on Taobao, but the seller did not send you a complete clothes hanger, but some components and a drawing. You assemble the clothes hanger step by step according to this drawing and become what you need. Then Dockerfile is the drawing, and the image file is the coat hanger you need. Dockerfile does not recommend naming it casually, just use Dockerfile.
Therefore, Dockerfile contains one instruction after another, each of which builds a layer, so the content of each instruction is to describe how the layer should be built.
The general process for Docker to execute Dockerfile:
(1) docker runs a container from the basic image
(2) execute an instruction and make changes to the container
(3) perform an operation similar to docker commit to submit a new mirror layer
(4) docker runs a new container based on the image just submitted
(5) execute the next instruction in dockerfile until all instructions have been executed.
Different stages:
1. Dockerfile: is the raw material of the software, you need to define a Dockerfile,Dockerfile that defines everything the process needs. Dockerfile involves executing code or files, environment variables, dependent packages, runtime environments, dynamic link libraries, and so on.
2. Docker image: it is a deliverable of the software. After a file is defined with Dockerfile, a Docker image will be generated when docker build is run. When Docker image is run, the service will actually be provided.
3. Docker container: it can be considered as the running state of the software, and the container provides services directly.
Detailed description of the parameters of Dockerfile:
FROM: indicates the basic image from which the new image is built
MAINTAINER: indicates the image maintainer and its contact information
RUN: what command to execute
CMD: specifies the command to be run when a container starts. There can be multiple CMD instructions in Dockerfile, but only the last one takes effect, and CMD will be replaced by the parameter after docker run.
EXPOSE: declares the service port on which the container is running
ENV: setting environment variables during the construction of an image
ADD: copy the directory or file on the host to the image (it will help you decompress automatically without additional operation)
COPY: works like ADD, but does not support automatic download and decompression
ENTRYPOINT: specifies the command to run when a container starts, using similar to CMD, except that programs started by ENTRYPOINT are not overridden by the parameters specified by the docker run command line, and these command line parameters are passed as arguments to the program specified by ENTRYPOINT
VOLUME: container data volume, which specifies the container mount point to the directory automatically generated by the host or other containers (data preservation and persistence work, but is not generally used in Dockerfile. More common is to specify the-v data volume when you command docker run. )
WORKDIR: equivalent to the cd command, switch the directory path
II. Dockerfile actual combat:
(1) build a sshd image:
1. Create a directory first:
[root@localhost ~] # cd / opt/ [root@localhost opt] # mkdir sshd [root@localhost opt] # cd sshd/
2. Edit the dockerfile file
[root@localhost sshd] # vim DockerfileFROM centosMAINTAINER this is sshdRUN yum-y updateRUN yum-y install openssh* net-tools lsof telnet passwdRUN echo '123456' | passwd-- stdin rootRUN sed-I' s/UsePAM yes/UsePAM no/g' / etc/ssh/sshd_configRUN ssh-keygen-t rsa-f / etc/ssh/ssh_host_rsa_keyRUN sed-I'/ ^ session\ s\ + required\ s\ + pam_loginuid.so/s/ ^ / # /'/ etc/pam.d/sshdRUN mkdir- P / root/.ssh & & chown root.root / root & & chmod 700 / root/.sshEXPOSE 22CMD ["/ usr/sbin/sshd" "- D"]
3. Create an image
[root@localhost sshd] # docker build-t sshd:new .89432272695ab560b18de75a064428e4a7c4a52dfce223afd2e85132ae6c3c72 [root@localhost sshd] # docker ps-a / / View recent status CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES89432272695a sshd:new "/ usr/sbin/sshd-D" 7 seconds ago Up 6 seconds 0.0 .0.0For 32768-> 22/tcp sad_fermi
4. Start the container and change the password
[root@localhost sshd] # docker run-d-P sshd:new [root@localhost sshd] # ssh localhost-p 32768
(2) build a Systemctl image:
Based on the sshd:new image above, we create another systemctl image:
1. Create a separate directory:
[root@localhost ~] # cd / opt/ [root@localhost opt] # mkdir systemctl [root@localhost opt] # cd systemctl/
2. Edit dockerfile
[root@localhost systemctl] # vim DockerfileFROM sshd:newENV container dockerRUN (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"]
3. Create an image:
[root@localhost systemctl] # docker build-t systemd:lasted.
4. Start the image:
[root@localhost systemctl] # docker run-- the root in privileged-it-v / sys/fs/cgroup/:/sys/fs/cgroup:ro systemd:lasted / sbin/init//privateged container has the real root permission, otherwise, the root in the container is just an external ordinary user right.
Docker ps-a View status:
5. Enter the container
[root@localhost ~] # docker exec-it 23a50d568c75 bash [root@23a50d568c75 /] # systemctl status sshd
You can use systemctl and sshd commands at this point
(3) build a Nginx image:
1. Create a directory:
[root@localhost ~] # cd / opt/ [root@localhost opt] # mkdir nginx [root@localhost opt] # cd nginx/
2. Edit Dockerfile:
[root@localhost nginx] # vim DockerfileFROM centos:7MAINTAINER The is nginx RUN yum install-y proc-devel gcc gcc-c++ zlib zlib-devel make openssl-devel wgetRUN wget http://nginx.org/download/nginx-1.9.7.tar.gzRUN tar zxvf nginx-1.9.7.tar.gzWORKDIR nginx-1.9.7/RUN. / configure-prefix=/usr/local/nginx & & make & & make installEXPOSE 80EXPOSE 443RUN echo "daemon off "> > / usr/local/nginx/conf/nginx.confWORKDIR / root/nginxADD run.sh / run.shRUN chmod 755 / run.shCMD [" / run.sh "]
3. Create a run.sh script:
[root@localhost nginx] # vim run.sh#!/bin/bash/usr/local/nginx/sbin/nginx
4. Create an image:
[root@localhost nginx] # docker build-t nginx:new.
5. Start the image
[root@localhost nginx] # docker run-d-P nginx:new228c1f5b8070d52c6f19d03159ad93a60d682a586c0b1f944dc651ee40576a3e [root@localhost nginx] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES228c1f5b8070 nginx:new "/ run.sh" 9 seconds ago Up 8 seconds 0.0.0.0 80/tcp 32769-> 0.0.0.0VOVA 32768-> 443/tcp busy_booth
6. Verify: browser access: http://192.168.220.131:32769/
(4) build a Tomcat image:
You need to use the apache-tomcat-9.0.16 and jdk1.8.0_91 packages, which can be remotely mounted locally and then copied to the tomcat folder.
[root@localhost tomcat] # lsapache-tomcat-9.0.16 Dockerfile jdk1.8.0_911, edit Dockerfile:FROM centos:7MAINTAINER this is tomcat# and install JDK environment Set its environment variables ADD jdk1.8.0_91 / usr/local/javaENV JAVA_HOME / usr/local/javaENV JAVA_BIN / usr/local/java/binENV JRE_HOME / usr/local/java/jreENV PATH $PATH:/usr/local/java/bin:/usr/local/java/jre/binENV CLASSPATH / usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jarADD apache-tomcat-9.0.16 / usr/local/tomcat8EXPOSE 80802, Generate an image: [root@localhost tomcat] # docker build-t tomcat:centos. 3. Generate container: [root@localhost tomcat] # docker run-- name tomcat01-p 1234ren 8080-it tomcat:centos / bin/bash// specify port number 12344, after entering the container Start the service: [root@34682d7dc7b8 /] # cd / usr/local/tomcat8/bin/ [root@34682d7dc7b8 bin] #. / startup.sh
Authentication: browser accesses 192.168.220.131purl 1234
(5) build a Mysql image:
The mysql working directory contains the following files: my.cnf Dockerfile mysql-boost-5.7.20.tar.gzvim my.cnf [client] port = 3306default-character-set=utf8socket = / usr/local/mysql/ mysql.sock [MySQL] port = 3306default-character-set=utf8socket = / usr/local/mysql/ mysql.sock [mysqld] user = mysqlbasedir = / usr/local/mysqldatadir = / usr/local/mysql/dataport = 3306character_set_server=utf8pid-file = / usr/local/mysql/mysqld.pidsocket = / usr/ Local/mysql/mysql.sockserver-id = 1sql_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_QUOTESvim DockerfileFROM centos:7RUN yum-y install\ ncurses\ ncurses-devel\ bison\ cmake\ make\ gcc\ gcc-c++RUN useradd-s / sbin/nologin mysqlADD mysql-boost-5.7.20.tar.gz / usr/local/srcWORKDIR / usr/local/src/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\-DWITH_BOOST=boost\-DWITH_SYSTEMD=1 & & make & & make installRUN chown-R mysql:mysql / usr/local/mysql/RUN rm-rf / etc/my.cnfADD my.cnf / etcRUN chown mysql:mysql / etc/my.cnfENV PATH= / usr/local/mysql/bin:/usr/local/mysql/lib:$PATHWORKDIR / usr/local/mysql/RUN bin/mysqld\-initialize-insecure\-user=mysql\-basedir=/usr/local/mysql\-datadir=/usr/local/mysql/dataRUN cp / usr/local/mysql/usr/lib/systemd/system/mysqld.service / usr/lib/systemd/system/EXPOSE 3306RUN echo-e "#! / bin/sh\ nsystemctl enable mysqld" > / run.shRUN chmod 755 / run.shRUN sh / run.shCMD ["init"]
1. Create a container:
[root@localhost mysql] # docker build-t centos:mysql.
2. Run the container:
[root@localhost mysql] # docker run-name=mysql_server-d-P-privileged centos:mysql998dc97971022135b822b8b29154df3eaaf5dc977b2a91eaf8afc2b3cea7e
3. Check the container status:
[root@localhost mysql] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES998dc9797102 centos:mysql "init" About a minute ago Up About a minute 0.0.0.0 aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES998dc9797102 centos:mysql 32768-> 3306/tcp mysql_server
4. Raise the rights of the database:
[root@localhost mysql] # docker exec-it 998dc9797102 / bin/bash [root@998dc9797102 mysql] # mysqlmysql > grant all privileges on *. * to 'root'@'%' identified by' abc123';mysql > grant all privileges on *. * to 'root'@'localhost' identified by' abc123'
5. Test: open another virtual machine, install the mysql service with yum, and connect:
[root@localhost] # mysql-h 192.168.220.131-u root-P 32768-pabc123Welcome to the MariaDB monitor. Commands end with; or\ g.Your MySQL connection id is 4Server version: 5.7.20 Source distributionCopyright (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)] >
After reading the above, do you have any further understanding of the introduction and use of Dockerfile? If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.