Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction and practical Application of Dockerfile

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Overview of Dockerfile

Dockerfile is a text document that contains commands for combining images. You can use to invoke any command from the command line. Docker automatically generates an image by reading instructions in Dockerfile.

The general process for Docker to execute Dockerfile is as follows: (1) docker runs a container from the underlying image; (2) executes an instruction and modifies the container; (3) performs an operation similar to docker commit to submit a new mirror layer; (4) docker runs a new container based on the image that has just been committed; and (5) executes the next instruction in dockerfile until all instructions are 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

one, Build sshd image [root@localhost ~] # cd / opt/ [root@localhost opt] # mkdir sshd # # create directory [root@localhost opt] # cd sshd/ [root@localhost sshd] # vim Dockerfile # # write dockerfile file FROM centos # # download image MAINTAINER this is sshd # # description information RUN 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 22 # # Port CMD ["/ usr/sbin/sshd" "- D" [root@localhost sshd] # docker build-t sshd:new. # # create an image 89432272695ab560b18de75a064428e7c4a52dfce223afd2e85132ae6c3c72 [root @ localhost sshd] # docker run-d-P sshd:new # # create a mapping and container [root@localhost sshd] # docker ps-a # # View container status CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES89432272695a sshd:new "/ usr/sbin/sshd-D "7 seconds ago Up 6 seconds 0.0.0.0 22/tcp sad_ 32768-> 22/tcp sad_ fermi [root @ localhost sshd] # ssh localhost-p 32768 # # sign in to local 2 with ssh Build systemctl image [root@localhost ~] # cd / opt/ [root@localhost opt] # mkdir systemctl # # create directory [root@localhost opt] # cd systemctl/ [root@localhost systemctl] # vim Dockerfile # # write dockerfile file FROM sshd:newENV container docker # # environment 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"] [root@localhost systemctl] # docker build-t systemd:lasted. # # create 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 only an external ordinary user right. [root@localhost ~] # docker exec-it 23a50d568c75 bash # # enter the container [root@23a50d568c75 /] # systemctl status sshd # # View status 3 Build a Nginx image [root@localhost ~] # cd / opt/ [root@localhost opt] # mkdir nginx # # create a Nginx directory [root@localhost opt] # cd nginx/ [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 wgetADD nginx-1.12.2.tar.gz / usr/localWORKDIR / usr/local/nginx-1.12.2/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 "] [root@localhost nginx] # vim run.shrun.shrun.nginx # # start the Nginx service [root@localhost nginx] # mount.cifs / / 192.168.100.3/LNMP-C7 / mnt/ # # Mount the image Password for root@//192.168.100.3 / LNMP-C7: [root@localhost nginx] # cp / mnt/nginx-1.12.2.tar.gz. / # # copy to the current directory [root@localhost nginx] # docker build-t nginx:new. # # create Image [root@localhost nginx] # docker run-d-P nginx:new # # create Container 228c1f5b8070d52c6f19d03159ad93a60d682a586c0b1f944dc651ee40576a3e [root@localhost nginx] # docker ps-a # # View Container CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES228c1f5b8070 nginx:new "/ run.sh" 9 seconds ago Up 8 seconds 0.0.0.0 seconds 32770-> 80/tcp 0.0.0.0 32769-> 443/tcp busy_booth## visits the web page with a browser

four, Build a Tomcat image [root@localhost opt] # mkdir tomcat [root@localhost opt] # cd tomcat [root@localhost tomcat] # cp / mnt/tomcat/jdk-8u91-linux-x64.tar.gz. / # # copy to the current directory [root@localhost tomcat] # cp / mnt/Tomcat1/tomcat/apache-tomcat-9.0.16.tar.gz. / [root@localhost tomcat] # vim DockerfileFROM centos:7MAINTAINER this is tomcatADD jdk-8u91-linux-x64.tar.gz / usr/localWORKDIR / Usr/localRUN mv jdk1.8.0_91 / usr/local/javaENV JAVA_HOME / usr/local/java # # set the environment variable ENV 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- 8.5.16.tar.gz / usr/localWORKDIR / usr/localRUN mv apache-tomcat-8.5.16 / usr/local/tomcat8EXPOSE 8080ENTRYPOINT ["/ usr/local/tomcat8/bin/catalina.sh" "run"] [root@localhost tomcat] # docker build-t tomcat:centos. # # create an image [root@localhost tomcat] # docker run-- name tomcat01-p 1234mot8080-it tomcat:centos / bin/bash## create a container # # access using a browser

five, Build a MySQL image [root@localhost opt] # mkdir mysql [root@localhost opt] # cd mysql [root@localhost mysql] # cp / mnt/mysql-boost-5.7.20.tar.gz. / # # copy the package to the current directory [root@localhost mysql] # vim my.cnf # # create a configuration file template [client] port = 3306default-character-set=utf8socket = / usr/local/mysql/ MySQL. Sock [MySQL] port = 3306 default- 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_ quotes [root @ localhost mysql] # vim Dockerfile # # write the dockerfile file FROM 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\-DMYSQL_DATADIR=/usr/local/mysql/data\-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 "] [root@localhost mysql] # docker build-t centos:mysql. # # create an image [root@localhost mysql] # docker run-- name=mysql_server-d-P-- privileged centos:mysql # # create a container [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 localhost mysql 32768-> 3306/tcp mysql_ server [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'; [root@localhost] # mysql-h 192.168.13.128-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)] > 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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report