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

Dockerfile Source Separation deployment LNMP (Centos7)

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

Share

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

Common Dockerfile directives:

1. FROM: the image on which the construction image is based

For example: FROM centos

2. MAINTAINER: name or email address of image maintainer

For example: MAINTAINER Sun qiu ming

3. RUN: the shell command that runs when the image is built

For example:

RUN ["yum", "install", "httpd"]

RUN yum-y install httpd

4. CMD: the shell command executed when the container is run

For example:

CMD ["/ bin/bash"]

5. EXPOSE declares the service port of the container

For example: EXPOSE 80443

EXPOSE declaration port

The format is EXPOSE [...].

The EXPOSE directive declares that the runtime container provides a service port, which is just a declaration that the service on this port will not be opened by the application at runtime because of this declaration. Writing such a declaration in Dockerfile has two advantages, one is to help mirror consumers understand the daemon port of the mirror service to facilitate configuration mapping, and the other is to automatically randomly map EXPOSE ports when random port mapping, that is, docker run-P, is used at run time.

Distinguish EXPOSE from using-p: at run time. -p, which maps the host port and the container port, in other words, exposes the corresponding port service of the container to external access, while EXPOSE only declares what port the container intends to use and does not automatically map the port in the host.

6. ENV: set container environment variables

For example: ENV MYSQL_ROOT_PASSWORD 123.com

7. ADD: copy a file or directory to the image. If it is a URL or compressed package, it will be downloaded or decompressed automatically.

ADD...

ADD ["source file"... "destination directory"]

For example:

ADD https://xxx.com/html.tar.gz / var/www/html

ADD html.tar.gz / var/www/html

8. COPY: copy files or directories into the image container, similar to ADD, but without automatic download or decompression

For example: COPY. / start.sh / start.sh

9. ENTRYPOINT: the shell command that runs the container is executed

For example:

ENTRYPOINT ["/ bin/bash", "- c", "/ start.sh"]

ENTRYPOINT / bin/bash-c'/ start.sh'

10. VOLUME: specify the container mount point to the directory or other container automatically generated by the host

For example:

VOLUME ["/ var/lib/mysql"]

11. USER: specify the running user for RUN, CMD, and ENTRYPOINT to execute commands

USER [:] OR USER [:]

For example:

USER sunpengjun

12. WORKDIR: set working directories for RUN, CMD, ENTRYPOINT, COPY, and ADD

For example:

WORKDIR / data

13. HEALTHCHECK: health check

14. ARG: specify some parameters when building

For example:

FROM centos

ARG user

USER $user

Be careful

1. RUN runs when building and can write multiple messages.

2. CMD and ENTRYPOINT can only write one entry when running container. If more than one entry is written, the last one will take effect.

3. CMD can be overridden by COMMAND during run, and ENTRYPOINT will not be overwritten by COMMAND, but you can specify-- entrypoint override.

Docker cache properties:

If you need to use a previously cached image in the same layer, you don't need to download it again. However, if the upper layer of this mirror layer changes, even in the same layer, there is no cache. You can add the-- no-cache parameter if you have to not use caching.

Each instruction in Dockerfile creates a mirror layer, and the upper layer depends on the lower layer. Whenever a layer changes, the cache of all layers above it will be invalidated.

That is, if we change the order in which Dockerfile instructions are executed, or modify or add instructions, the cache will be invalidated.

Advantages of Dockerfile:

Making a mirror through dockerfile, you can clearly see the operation of each layer of the mirror, with high security and strong portability.

Dockerfile detached deployment LNMP (binary installation):

Tip: the installation package required for nginx,php,mysql needs to be downloaded locally in advance (the structure directory is as follows).

Prepare for deployment:

1. Solve the problem of fixed ip address of container:

Ensure that the ip address will not change after the container is restarted, and it is easy to manage and operate.

1) Custom network: [root@sqm-docker01 nginx] # docker network create-d bridge-- subnet 172.16.10.0 mynet1 24-- gateway 172.16.10.1

2. Create a mount directory:

Web page root: / wwwroot

Configuration file directory: / docker_conf

[root@sqm-docker01 nginx] # mkdir / wwwroot [root@sqm-docker01 nginx] # mkdir / docker_conf deployment nginx:

[root@sqm-docker01 ~] # vim Dockerfile

The code is as follows:

FROM centos:7ADD nginx-1.8.0.tar.gz / ADD nginx-sticky-module.zip / ADD ngx_cache_purge-2.3.tar.gz / RUN yum-y install gcc* pcre-devel openssl-devel zlib-devel unzip make vim net-tools elinks tree\ & & groupadd nginx\ & & useradd nginx- g nginx-s / sbin/nologinWORKDIR nginx-1.8.0/RUN. / configure-prefix=/usr/local/nginx\-user=nginx\-group=nginx\ -- with-http_stub_status_module\-- with-http_realip_module\-- with-http_ssl_module\-- with-http_gzip_static_module\-- http-client-body-temp-path=/var/tmp/nginx/client\-- http-proxy-temp-path=/var/tmp/nginx/proxy\-- http-fastcgi-temp-path=/var/tmp/nginx/fcgi\-- with-pcre\-add-module=. . / ngx_cache_purge-2.3\-- with-http_flv_module\ & make & & make install\ & & ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/\ & mkdir-p / var/tmp/nginx/client/\ & chown-R nginx/ var/tmp/nginx/client/\ & & sed-I-e's Sigma NobodyGan'- e'2s / ^ # / /'/ usr/local/nginx/conf/nginx.conf EXPOSE 80CMD ["nginx" "- g", "daemon off "]

/ / build a nginx image:

[root@sqm-docker01 nginx] # docker build-t nginx.

Note: names cannot have uppercase and special characters, the last. Represents a dockerfile file in the current directory, and only one dockerfile can exist in a directory.

/ / run the nginx container: [root@sqm-docker01 mysql] # docker run-itd-- name nginx1.8.0-- network mynet1-- ip 172.16.10.10-- restart=always-p 80:80 nginx deployment php-fpm:

[root@sqm-docker01 php] # vim Dockerfile

The code is as follows:

FROM centos:7ADD libmcrypt-2.5.7.tar.gz / ADD php-5.6.27.tar.gz / RUN yum-y install gcc* pcre-devel zlib-devel tree unzip make vim net-tools elinks libxml2-devel libcurl-devel openssl-devel bzip2-develWORKDIR / libmcrypt-2.5.7/RUN. / configure-- prefix=/usr/local/libmcrypt & & make & & make install\ & & cd / php-5.6.27\ & &. / configure-- prefix=/usr/local/php5 .6-- with-mysql=mysqlnd\-- with-pdo-mysql=mysqlnd\-- with-mysqli=mysqlnd\-- with-openssl\-- enable-fpm\-- enable-sockets\-- enable-sysvshm\-- enable-mbstring\-- with-freetype-dir\-- with-jpeg-dir\-- with-png-dir\-with-zlib\-with-libxml-dir=/usr\-- enable-xml\-- with-mhash\ -- with-mcrypt=/usr/local/libmcrypt\-- with-config-file-path=/etc\-- with-config-file-scan-dir=/etc/php.d\-- with-bz2-- enable-maintainer-zts\ & & make & & make install\ & & cp php.ini-production / etc/php.ini\ & & cp sapi/fpm/init.d.php-fpm / etc/init.d/php-fpm\ & & chmod + x / etc/init .d/php-fpm\ & & chkconfig-- add php-fpm\ & & chkconfig php-fpm on\ & & cp / usr/local/php5.6/etc/php-fpm.conf.default / usr/local/php5.6/etc/php-fpm.conf\ & & sed-I-e's / Daemonize = yes/daemonize = no/g'-e-usr/local/php5.6/etc/php-fpm.conf 127.0.0.1 usr/local/php5.6/etc/php-fpm.conf EXPOSE 9000CMD 0.0.0.0 Compact ["/ usr/local/php5.6/sbin/php-fpm", "- c", "/ usr/local/php5.6/etc/php-fpm.conf"]

/ / build a php-fpm image:

[root@sqm-docker01 php] # docker build-t php-fpm.

/ / run the php container: [root@sqm-docker01 mysql] # docker run-itd-- name php-fpm-- network mynet1-- ip 172.16.10.20-- restart=always-p 9000 php-fpm deployment mysql5.7.28:

[root@sqm-docker01 mysql] # vim Dockerfile

The code is as follows:

FROM centos:7RUN yum-y install gcc* zlib-devel make vim net-tools cmake bison ncurses-devel gcc gcc-c++ pcre pcre-devel openssl openssl-devel libaio initscriptsADD boost_1_59_0.tar.gz / optADD mysql-5.7.28.tar.gz / optWORKDIR / opt/mysql-5.7.28RUN cmake-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.28\-DWITH_BOOST=/opt/boost_1_59_0\-DMYSQL_UNIX_ADDR=/data/mysql/ Tmp/mysql.sock\-DMYSQL_DATADIR=/data/mysql\-DDEFAULT_CHARSET=utf8mb4\-DDEFAULT_COLLATION=utf8mb4_general_ci\-DWITH_EXTRA_CHARSETS=all\-DWITH_MYISAM_STORAGE_ENGINE=1\-DWITH_INNOBASE_STORAGE_ENGINE=1\-DWITH_MEMORY_STORAGE_ENGINE=1\-DWITH_READLINE=1\-DWITH_INNODB_MEMCACHED=1\-DWITH_DEBUG=OFF\-DENABLED_LOCAL_INFILE=1\-DENABLED_PROFILING=ON\-DMYSQL_MAINTAINER_MODE=OFF\-DMYSQL_TCP_PORT=3306\ & & make & & make install RUN Ln-s / usr/local/mysql-5.7.28 / usr/local/mysql\ & & echo "export PATH=/usr/local/mysql/bin:$PATH" > > / etc/profile\ & & echo "export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH" > > / etc/profile\ & & source / etc/profile\ & & mkdir-p / data/mysql/ {data Tmp,logs,pids Share}\ & & touch / data/mysql/logs/mysqld.log\ & & touch / data/mysql/pids/mysqld.pid\ & useradd-s / sbin/nologin-M mysql\ & & chown-R mysql:mysql / data/mysql COPY my.cnf / etc/my.cnfRUN cd / usr/local/mysql\ & & cp support-files/mysql.server / etc/init.d/mysqld\ & & chmod axix / etc/init.d/mysqld\ & & chkconfig-- add Mysqld\ & & chkconfig mysqld on\ & & ln-s / usr/local/mysql/bin/* / usr/local/bin\ & & mysqld-- initialize-insecure-- user=mysql-- basedir=/usr/local/mysql-- datadir=/data/mysql/dataCOPY mysql-start.sh / optCOPY user.sh / optRUN chmod astatx / opt/mysql-start.sh\ & & chmod adepx / opt/user.shEXPOSE 3306CMD ["/ opt/mysql-start.sh"]

# # the script code in dockerfile is as follows:

[root@sqm-docker01 mysql] # cat my.cnf [mysqld] character-set-server=utf8mb4collation-server=utf8mb4_general_cidatadir=/data/mysql/datasocket=/data/mysql/tmp/ mysql.sock [mysqld _ safe] log-error=/data/mysql/logs/mysqld.logpid-file=/data/mysql/pids/mysqld.pid [client] default-character-set=utf8mb4 [root@sqm-docker01 mysql] # cat mysql-start.sh #! / bin/bashchown-R mysql:mysql / data/mysql/sbin/service mysqld start / bin/bash # keep a terminal Prevent the container from automatically exiting / / modifying the database password and authorize the user: [root@sqm-docker01 mysql] # cat user.sh #! / bin/bashmysqlpwd= `grep password / data/mysql/logs/mysqld.log | awk-F 'root@localhost:' {print $2} '`mysql-uroot-p$ {mysqlpwd}-e' alter user root@localhost identified by "pwd@123"-- connect-expired-passwordmysql-uroot-ppwd@123 > EOF

3) Mount the directory on dockerhost to the container:

/ / We need to delete the nginx and php containers, rerun and mount them. In fact, after building the service above, you don't have to run the service first, you can run it now. I want to test whether the installation is successful. Of course, you can also write a web page file from the beginning and mount it directly.

[root@sqm-docker01 html] # docker rm-f nginx1.8.0 [root@sqm-docker01 html] # docker run-itd-- name nginx1.8.0-- network mynet1\ >-- ip 172.16.10.10-- restart=always-p 80:80\ >-v / docker_conf/conf/:/usr/local/nginx/conf/-v / wwwroot/html/:/usr/local/nginx/html nginx [root@sqm-docker01] # docker rm-f php-fpm [ Root@sqm-docker01 ~] # docker run-itd-- name php-fpm-- network mynet1\ >-- ip 172.16.10.20-- restart=always-p 9000 name php-fpm 9000\ >-v / wwwroot/html/:/usr/local/nginx/html/ php-fpm test visits the nginx and php parsing pages:

Set up a wordpress test lnmp environment:

1) download the wordpress installation package to the root of the web page:

[root@sqm-docker01 ~] # cd / wwwroot/html/ [root@sqm-docker01 html] # wget https://cn.wordpress.org/wordpress-4.7.4-zh_CN.tar.gz// is downloaded, extract the installation package: [root@sqm-docker01 html] # tar zxf wordpress-4.7.4-zh_CN.tar.gz

2) browser test access:

URL:http:// Container Host IP/wordpress

The login is successful and the lnmp is completed.

-this is the end of this article. 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