In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
one。 Brief introduction
Docker is an open source application container engine that allows developers to package their applications and dependency packages into a portable container, publish them to any popular Linux machine, and virtualize them. Containers are fully sandboxed and will not have any interface with each other. There is almost no performance overhead and can be easily run in machines and data centers.
II. Basic composition of Docker
1. Mirror (Image)
Mirroring, which is a class in object-oriented, is equivalent to a template. In essence, a mirror is equivalent to a file system. Docker image is a special file system, which not only provides programs, libraries, resources, configuration and other files needed by the container, but also contains some configuration parameters (such as anonymous volumes, environment variables, users, etc.) prepared for the runtime. The mirror does not contain any dynamic data, and its content will not be changed after it is built.
two。 Container (Container)
The container, which is the instance created by the class, is the entity created based on the mirror template. A container is essentially a process, but unlike a process executed directly in the host, the container process runs in its own independent namespace. So the container can have its own root file system, its own network configuration, its own process space, and even its own user ID space. Processes in the container run in an isolated environment and are used as if they were operating on a system independent of the host. This feature makes container-encapsulated applications more secure than running directly in the host.
3. Warehouse (Repository)
Warehouse, from a cognitive point of view, is like a software package upload and download station, where different versions of various software are uploaded for users to download. After the image is built, it can be easily run on the current host. However, if you need to use this image on other servers, we need a centralized service to store and distribute images, such as Docker Registry.
III. Advantages of Docker
1. More efficient use of system resources: because the container does not need to perform hardware virtualization and run a complete operating system and other additional overhead, Docker has a higher utilization of system resources. Whether it is application execution speed, memory consumption or file storage speed, it is more efficient than traditional virtual machine technology. Therefore, compared with virtual machine technology, a host with the same configuration can often run more applications.
two。 Faster startup time: traditional virtual machine technology often takes several minutes to start application services, while Docker container applications can achieve seconds or even milliseconds startup time because they run directly in the host kernel and do not need to start the complete operating system.
3. Consistent running environment: a common problem in the development process is environmental consistency. Due to the differences in development environment, test environment and production environment, some bug have not been found in the development process. On the other hand, the image of Docker provides a complete runtime environment except the kernel, ensuring the consistency of the application running environment, so that this kind of problem will not occur again.
4. Continuous delivery and deployment: Docker is build once,run everywhere. Using Docker, you can customize application images to achieve continuous integration, continuous delivery, and deployment. Developers can build the image through Dockerfile and conduct integration testing with a continuous integration (Continuous Integration) system, while operators can quickly deploy the image directly in a production environment.
5. Easier migration: the tiered storage and mirroring technology used by Docker make it easier to reuse the repeated parts of the application, make it easier to maintain and update the application, and make it very easy to further expand the image based on the basic image.
Traditional development process
Docker environment development process
four。 Compared with traditional virtual machine
5. Docker command
1. Create a mirror
1.1 create based on an existing mirror container
Docker commit [options] container [repository [: tag]]
Option:-a,-- author= "" # author information-mmawashi message = "" # submission message-p,-- pause=true # suspend the container when it is submitted
1.2 create based on local template import
Docker load
< ***.tar --本地模板文件tar 1.3基于Dockerfile文件构建镜像 docker build -t image-name basedir 2.删除镜像 docker rmi image #image可以是标签或者ID docker rmi -f image #强制删除镜像注意:用docker rmi 命令删除镜像时,首先要删除容器,再删除镜像。否则会提示镜像在容器中运行。 镜像管理指令 3.创建/启动/停止/删除容器 docker create image #创建的容器是停止状态 docker start/stop container_id #启动/停止容器 docker run image #创建并启动容器 docker rm container_id #删除容器 创建容器常用选项 管理容器常用命令 4.镜像与容器联系 镜像不是一个单一的文件,而是有多层构成。我们可以通过docker history 查看镜像中各层内容及大小,每层对应着Dockerfile中的一条指令。Docker镜像默认存储在/var/lib/docker/中。 容器其实是在镜像的最上面加了一层读写层,在运行容器里做的任何文件改动,都会写到这个读写层。如果容器删除了,最上面的读写层也就删除了,改动也就丢失了。 Docker使用存储驱动管理镜像每层内容及可读写层的容器层。 5.将主机数据挂载到容器 Docker提供三种不同的方式将数据从宿主机挂载到容器中:volumes,bind mounts和tmpfs。 volumes:Docker管理宿主机文件系统的一部分(/var/lib/docker/volumes)。bind mounts:可以存储在宿主机系统的任意位置。tmpfs:挂载存储在宿主机系统的内存中,而不会写入宿主机的文件系统。 5.1 volume 注意: # 如果没有指定卷,自动创建。# 建议使用--mount,更通用。 5.2 Bind Mounts 注意: # 如果源文件/目录没有存在,不会自动创建,会抛出一个错误。# 如果挂载目录在容器中非空目录,则该目录现有内容将被隐藏。 5.3 tmpfs 容器中使用 tmpfs: # docker run -d -it --name nginx-test --mount type=tmpfs,destination=/usr/share/nginx/html nginx# docker run -d -it --name nginx-test --tmpfs /usr/share/nginx/html nginx 注意: # tmpfs方式仅存储在主机系统的内存中,不会写入主机的文件系统。# tmpfs挂载不能在容器间共享。# tmpfs只能在Linux容器上工作,不能在Windows容器上工作。六.Docker实战--构建lnmp环境,搭建WordPress博客 实验环境: 1.Docker安装 首先安装依赖包Install Docker (previously installed, so prompt that it has been installed)
Check to see if Docker is installed successfully
Start Docker and join Boot Auto Boot
[root@localhost ~] # systemctl start docker [root@localhost ~] # systemctl enable docker
two。 Use Dockerfile to build an image
Dockerfile instruction
Environment description:
In this article, I am based on the centos 7.5 system, nginx and php use the source package to build, if you do not want to use the source package, you can also use yum to build.
Nginx, built with source package, version nginx-1.12.2.tar.gz, download address http://nginx.org/en/download.html/php, also built with source package, version php-5.6.31.tar.gz, download address http://php.net/downloads.php
Files required to create an image
Create two directories (nginx,php) under the Dockerfiles directory, which store the Dockerfile file and the source code package respectively. The nginx.conf configuration file is distributed in the nginx directory, and the php.ini configuration file is also placed in the php directory. (in the actual environment, these two files often need to be modified. After being taken out separately, when you start the container, you can mount these two files into the container for easy management. ).
2.1 nginx build
Dockerfile content:
Analyze the contents of Dockerfile. When you build an image, it will be executed step by step according to what you have choreographed, and if one of these steps cannot be performed, it will stop building immediately. Most of the above instructions are easy to understand, which can be understood in comparison with the Dockerfile instruction diagram above. I would like to elaborate on the last instruction: CMD [". / sbin/nginx", "- g", "daemon off;"]
. / sbin/nginx, which means to start the nginx service normally
-g: set the global directive outside the configuration file, that is, the daemon off parameter is set when starting nginx. The default parameter is on, and whether to run nginx as a daemon. A daemon refers to a process that leaves the terminal and runs in the background. This is set to off, that is, it is not allowed to run in the background. Why do we not let the nginx container run in the background when we start it? by default, the docker container will use the first process in the container, that is, the program of pid=1, as the basis for whether the docker container is running or not. If the docker container pid fails, then the docker container will exit directly.
Nginx.conf content
Location ~ .php is mainly added to the configuration, where the lnmp_php of fastcgi_pass is the name of the php container when it is started later. When the request is matched to php, it is forwarded to lnmp_php, the container php-fpm service, for processing. Normally, if the php service is not running in the container, the lnmp_php content is usually written to the Ip address of the php server.
Build to build nginx image
Change to the nginx directory:
Build:
[root@localhost nginx] # docker build-t nginx:1.12.2
Check whether the image is built successfully:
2.2 PHP build
Dockerfile content
Php.ini content (the default configuration content of PHP is good. If you can't find it, you can copy and paste mine.)
[PHP] engine = On short_open_tag = Off asp_tags = Off precision = 14 output_buffering = 4096 zlib.output_compression = Off implicit_flush = Off unserialize_callback_func = serialize_precision = 17 disable_functions = disable_classes = zend.enable_gc = On expose_php = On max_execution_time = 300 max_input_time = 300 memory_limit = 128m error_reporting = E_ALL & ~ E_DEPRECATED & ~ E _ STRICT display_errors = Off display_startup_errors = Off log_errors = On log_errors_max_len = 1024 ignore_repeated_errors = Off ignore_repeated_source = Off report_memleaks = On track_errors = Off html_errors = On variables_order = "GPCS" request_order = "GP" register_argc_argv = Off auto_globals_jit = On post_max_size = 32m auto_prepend_file = auto_append_file = default_mimetype = "text/html" always_populate_raw_post_data =-1 doc_root = user_dir = enable_dl = Off file_uploads = On upload_max_filesize = 2m max_file_uploads = 20 allow_url_fopen = On allow_url_include = Off default_socket_timeout = 60 [CLI Server] cli_server.color = On [Date] date.timezone = Asia/Shanghai [filter [iconv] [intl] [sqlite] [sqlite3] [Pcre] [Pdo_mysql] pdo_mysql.cache_size = 2000 pdo_mysql.default_socket= [Phar] [mail function] SMTP = localhost smtp_port = 25 mail.add_x_header = On [SQL] sql.safe_mode = Off [ODBC] odbc.allow_persistent = On odbc. Check_persistent = On odbc.max_persistent =-1 odbc.max_links =-1 odbc.defaultlrl = 4096 odbc.defaultbinmode = 1 [Interbase] ibase.allow_persistent = 1 ibase.max_persistent =-1 ibase.max_links =-1 ibase.timestampformat = "% Y-%m-%d% H:%M:%S" ibase.dateformat = "% Y-%m-%d" ibase.timeformat = "% HRV% MVO% S "[MySQL] mysql.allow_local_infile = On mysql.allow_persistent = On mysql.cache_size = 2000 mysql.max_persistent =-1 mysql.max_links =-1 mysql.default_port = mysql.default_socket = mysql.default_host = mysql.default_user = mysql.default_password = mysql.connect_timeout = 60 mysql.trace_mode = Off [MySQLi] mysqli.max_persistent =-1 mysqli .allow _ persistent = On mysqli.max_links =-1 mysqli.cache_size = 2000 mysqli.default_port = 3306 mysqli.default_socket = mysqli.default_host = mysqli.default_pw = mysqli.reconnect = Off [mysqlnd] mysqlnd.collect_statistics = On mysqlnd.collect_memory_statistics = Off [OCI8] [PostgreSQL] pgsql.allow_persistent = On pgsql.auto_reset_persistent = Off pgsql .max _ persistent =-1 pgsql.max_links =-1 pgsql.ignore_notice = 0 pgsql.log_notice = 0 [Sybase-CT] sybct.allow_persistent = On sybct.max_persistent =-1 sybct.max_links =-1 sybct.min_server_severity = 10 sybct.min_client_severity = 10 [bcmath] bcmath.scale = 0 [browscap] [Session] session.save_handler = files session. Use_strict_mode = 0 session.use_cookies = 1 session.use_only_cookies = 1 session.name = PHPSESSID session.auto_start = 0 session.cookie_lifetime = 0 session.cookie_path = / session.cookie_domain = session.cookie_httponly = session.serialize_handler = php session.gc_probability = 1 session.gc_divisor = 1000 session.gc_maxlifetime = 1440 session.referer_check = session.cache_limiter = nocache session. Cache_expire = 180 session.use_trans_sid = 0 session.hash_function = 0 session.hash_bits_per_character = 5 url_rewriter.tags = "a=href Area=href,frame=src,input=src Form=fakeentry "[MSSQL] mssql.allow_persistent = On mssql.max_persistent =-1 mssql.max_links =-1 mssql.min_error_severity = 10 mssql.min_message_severity = 10 mssql.compatibility_mode = Off mssql.secure_connection = Off [Assertion] [COM] [mbstring] [gd] [exif] [Tidy] tidy.clean_output = Off [soap] soap.wsdl_ Cache_enabled=1 soap.wsdl_cache_dir= "/ tmp" soap.wsdl_cache_ttl=86400 soap.wsdl_cache_limit = 5 [sysvshm] [ldap] ldap.max_links =-1 [mcrypt] [dba] [opcache] [curl]
Build to build php image
Now that the php source package, php.ini, and Dockerfile are ready, we can use docker build to build the image:
Change to the php directory:
Build a php image:
[root@localhost php] # docker build-t php:5.6.31.
Check whether the image is built successfully
3. Run the container
3.1 create a custom network lnmp
First, create a custom network and add it to the lnmp network when you run ningx and php containers:
# View default network: [root@localhost php] # docker network ls creation: [root@localhost php] # docker network create lnmp
3.2 create a php container
Create a container: [root@localhost php] # docker run-itd-- name lnmp_php-- network lnmp-v / app/wwwroot:/usr/local/nginx/html php:5.6.31
Parameter description:
-itd: # Open a pseudo terminal in the container for interactive operation and run it in the background;-- name: # assign a name to the container lnmp_php;--network: # specify a network environment for the container as lnmp network;-- mout: # Mount the host's / app/wwwroot directory to the container's / usr/local/nginx/html directory, which is also equivalent to data persistence Php:5.6.31: # specify the php image you just built to start the container
Check to see if the php container is running:
Create a nginx container:
Create a container: [root@localhost php] # docker run-itd-- name lnmp_nginx-- network lnmp-p 80:80-v / app/wwwroot:/usr/local/nginx/html nginx:1.12.2
Check to see if the container is running:
3.4 Test access
Create an index.html static page to access:
[root@localhost wordpress] # echo "Dockerfile lnmp test" > / app/wwwroot/index.html
Use a browser to access the host's IP:
Create another index.php file to test:
[root@localhost wordpress] # echo "
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.