1 Star 1 Fork 1

左浪国 / dnmj

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

使用Docker 部署 LNMJ+Redis 环境

目录

安装Docker

# 下载安装
#移除旧版本docker
sudo yum remove docker \
                docker-client \
                docker-client-latest \
                docker-common \
                docker-latest \
                docker-latest-logrotate \
                docker-logrotate \
                docker-engine

#安装一些必要的系统工具
yum install -y yum-utils device-mapper-persistent-data lvm2 git vim lsof net-tools lrzsz wget zip unzip


#添加软件源信息
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

#更新 yum 缓存
yum makecache fast

#安装 Docker-ce
yum install -y docker-ce docker-ce-cli containerd.io

#启动 Docker 后台服务
systemctl start docker

#docker加入开机自启动
systemctl enable docker
systemctl disable docker

安装docker-compose

https://github.com/docker/compose/releases

curl -L https://get.daocloud.io/docker/compose/releases/download/v2.12.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

http://get.daocloud.io/#install-compose

离线安装
sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
接着执行如下命令添加可执行权限:
sudo chmod +x /usr/local/bin/docker-compose
  • centos 8 yum install containerd.io-1.2.6-3.3.el7.x86_64.rpm

创建镜像与安装

直接使用docker-compose一键制作镜像并启动容器

当前版本(推荐)

git clone https://gitee.com/zuolg/dnmj.git

启动:

$ cd dnmj                                           # 进入项目目录
$ docker-compose up                                 # 启动

添加快捷命令

在开发的时候,我们可能经常使用docker exec -it进入到容器中,把常用的做成命令别名是个省事的方法。

首先,在主机中查看可用的容器:

$ docker ps           # 查看所有运行中的容器
$ docker ps -a        # 所有容器

输出的NAMES那一列就是容器的名称,如果使用默认配置,那么名称就是nginxmysql等。

然后,打开~/.bashrc或者~/.zshrc文件,加上:

alias dnginx='docker exec -it nginx /bin/sh'
alias dmysql='docker exec -it mysql /bin/bash'
alias dredis='docker exec -it redis /bin/sh'

下次进入容器就非常快捷了,如进入php容器:

$ dnginx

查看docker网络

ifconfig docker0

用于填写extra_hosts容器访问宿主机的hosts地址

使用Log Nginx日志

Nginx日志是我们用得最多的日志,所以我们单独放在根目录log下。

log会目录映射Nginx容器的/var/log/nginx目录,所以在Nginx配置文件中,需要输出log的位置,我们需要配置到/var/log/nginx目录,如:

error_log  /var/log/nginx/nginx.localhost.error.log  warn;

使用Log MySQL日志

因为MySQL容器中的MySQL使用的是mysql用户启动,它无法自行在/var/log下的增加日志文件。所以,我们把MySQL的日志放在与data一样的目录,即项目的mysql目录下,对应容器中的/var/lib/mysql/目录。

slow-query-log-file     = /var/lib/mysql/mysql.slow.log
log-error               = /var/lib/mysql/mysql.error.log

以上是mysql.conf中的日志文件的配置。

常用命令

  • docker start 容器名(容器ID也可以)
  • docker stop 容器名(容器ID也可以)
  • docker run 命令加 -d 参数,docker 会将容器放到后台运行
  • docker ps 正在运行的容器
  • docker logs --tail 10 -tf 容器名 查看容器的日志文件,加-t是加上时间戳,f是跟踪某个容器的最新日志而不必读整个日志文件
  • docker top 容器名 查看容器内部运行的进程
  • docker exec -d 容器名 touch /etc/new_config_file 通过后台命令创建一个空文件
  • docker inspect 容器名 对容器进行详细的检查,可以加 --format='{(.State.Running)}' 来获取指定的信息
  • docker rm 容器ID 删除容器,注,运行中的容器无法删除
  • docker rm $(docker ps -aq) 删除所有容器
  • docker rmi $(docker images -aq) 删除所有镜像
  • docker images 列出镜像
  • docker pull 镜像名:标签 拉镜像
  • docker search 查找docker Hub 上公共的可用镜像
  • docker login 登陆到Docker Hub,个人认证信息将会保存到$HOME/.dockercfg,
  • docker commit -m="comment " --author="AT" 容器ID 镜像的用户名/仓库名:标签 不推荐这种方法,推荐dockerfile
  • docker history 镜像ID 深入探求镜像是如何构建出来的
  • docker port 镜像ID 端口 查看映射情况的容器的ID和容器的端口号,假设查询80端口对应的映射的端口
  • docker push 镜像名 将镜像推送到 Docker Hub
  • docker rmi 镜像名 删除镜像
  • docker attach 容器ID 进入容器
  • ############################################################
  • docker network create --subnet=172.171.0.0/16 docker-at 选取172.172.0.0网段
  • docker build 就可以加 -ip指定容器ip 172.171.0.10 了

Dockerfile语法

  • MAINTAINER 标识镜像的作者和联系方式
  • EXPOSE 可以指定多个EXPOSE向外部公开多个端口,可以帮助多个容器链接
  • FROM 指令指定一个已经存在的镜像
  • \#号代表注释
  • RUN 运行命令,会在shell 里使用命令包装器 /bin/sh -c 来执行。如果是在一个不支持shell 的平台上运行或者不希望在shell 中运行,也可以 使用exec 格式 的RUN指令
  • ENV REFRESHED_AT 环境变量 这个环境亦是用来表明镜像模板最后的更新时间
  • VOLUME 容器添加卷。一个卷是可以 存在于一个或多个容器内的特定的目录,对卷的修改是立刻生效的,对卷的修改不会对更新镜像产品影响,例:VOLUME["/opt/project","/data"]
  • ADD 将构建环境 下的文件 和目录复制到镜像 中。例 ADD nginx.conf /conf/nginx.conf 也可以是取url 的地址文件,如果是压缩包,ADD命令会自动解压、
  • USER 指定镜像用那个USER 去运行
  • COPY 是复制本地文件,而不会去做文件提取(解压包不会自动解压) 例:COPY conf.d/ /etc/apache2/ 将本地conf.d目录中的文件复制到/etc/apache2/目录中

docker-compose.yml 语法说明

  • image 指定为镜像名称或镜像ID。如果镜像不存在,Compose将尝试从互联网拉取这个镜像
  • build 指定Dockerfile所在文件夹的路径。Compose将会利用他自动构建这个镜像,然后使用这个镜像
  • command 覆盖容器启动后默认执行的命令
  • links 链接到其他服务容器,使用服务名称(同时作为别名)或服务别名(SERVICE:ALIAS)都可以
  • external_links 链接到docker-compose.yml外部的容器,甚至并非是Compose管理的容器。参数格式和links类似
  • ports 暴露端口信息。宿主机器端口:容器端口(HOST:CONTAINER)格式或者仅仅指定容器的端口(宿主机器将会随机分配端口)都可以(注意:当使用 HOST:CONTAINER 格式来映射端口时,如果你使用的容器端口小于 60 你可能会得到错误得结果,因为 YAML 将会解析 xx:yy 这种数字格式为 60 进制。所以建议采用字符串格式。)
  • expose 暴露端口,与posts不同的是expose只可以暴露端口而不能映射到主机,只供外部服务连接使用;仅可以指定内部端口为参数
  • volumes 设置卷挂载的路径。可以设置宿主机路径:容器路径(host:container)或加上访问模式(host:container:ro)ro就是readonly的意思,只读模式
  • volunes_from 挂载另一个服务或容器的所有数据卷
  • environment 设置环境变量。可以属于数组或字典两种格式。如果只给定变量的名称则会自动加载它在Compose主机上的值,可以用来防止泄露不必要的数据
  • env_file 从文件中获取环境变量,可以为单独的文件路径或列表。如果通过docker-compose -f FILE指定了模板文件,则env_file中路径会基于模板文件路径。如果有变量名称与environment指令冲突,则以后者为准(环境变量文件中每一行都必须有注释,支持#开头的注释行)
  • extends 基于已有的服务进行服务扩展。例如我们已经有了一个webapp服务,模板文件为common.yml。编写一个新的 development.yml 文件,使用 common.yml 中的 webapp 服务进行扩展。后者会自动继承common.yml中的webapp服务及相关的环境变量
  • net 设置网络模式。使用和docker client 的 --net 参数一样的值
  • pid 和宿主机系统共享进程命名空间,打开该选项的容器可以相互通过进程id来访问和操作
  • dns 配置DNS服务器。可以是一个值,也可以是一个列表
  • cap_add,cap_drop 添加或放弃容器的Linux能力(Capability)
  • dns_search 配置DNS搜索域。可以是一个值也可以是一个列表
  • networks的值frontend、backend
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

docker+nginx+mysql+java 展开 收起
Java 等 2 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/zuolg/dnmj.git
git@gitee.com:zuolg/dnmj.git
zuolg
dnmj
dnmj
master

搜索帮助