跳至主要內容

docker

Emilia Zhen大约 4 分钟linux

虚拟机常用命令

ll ls 查询同目录下的文件 su root 切换为 root 用户 :q! 退出 chmod 777 [dirname/filename] 改变档案权限 ifconfig 获取 IP 地址 若ifconfig返回Not found (CentOS) https://www.cnblogs.com/dunitian/p/4974761.html

文档各权限分数:r:4 x:2 e:1 owner/group/others

Docker

Centos7安装 dockeropen in new window

  1. Docker要求CentOS系统的内核版本高于3.10,先验证CentOS版本是否支持Docker
uname -r
  1. 使用root权限登录 su ,确保yun包更新到最新
yum update
  1. 卸载旧版本(如果安装过旧版本)
yum remove docker docker-common docker-selinux docker-engine
  1. 安装需要的软件包,yum-util提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
yum install -y yum-utils device-mapper-persistent-data lvm2
  1. 设置yum
yum yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  1. 可以查看所有仓库中所有docker版本,并选择特定版本安装
yum list docker-ce --showduplicates | sort -r
  1. 安装docker
yum install docker-ce
#由于repo中默认只开启stable仓库,故这里安装的是最新稳定版17.12.0
yum install <FQPN>
# 例如:sudo yum install docker-ce-17.12.0.ce
  1. 启动并加入开机启动
systemctl start docker
systemctl enable docker
  1. 验证安装是否成功(有 clent 和 service 两步分表示 docker 安装启动都成功了)
docker version
[root@localhost emiliazhen]# docker version
Client:
 Version:           18.06.1-ce
 API version:       1.38
 Go version:        go1.10.3
 Git commit:        e68fc7a
 Built:             Tue Aug 21 17:23:03 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.1-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       e68fc7a
  Built:            Tue Aug 21 17:25:29 2018
  OS/Arch:          linux/amd64
  Experimental:     false
[root@localhost emiliazhen]#

安装 docker 问题

因为之前装过旧版本docker在安装时报错如下

ansaction check error:
  file /usr/bin/docker from install of docker-ce-17.12.0.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
  file /usr/bin/docker-containerd from install of docker-ce-17.12.0.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
  file /usr/bin/docker-containerd-shim from install of docker-ce-17.12.0.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
  file /usr/bin/dockerd from install of docker-ce-17.12.0.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
  1. 卸载旧版本的包
yum erase docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
  1. 再次安装Docker
yum install docker-ce
命令说明
docker rmi [image id]删除镜像
docker rm [container id]删除一个处于终止状态的容器
docker images列出镜像
docker ps列出容器
docker ps -a列出所有容器
docker run -d -p 80:80 [imagesname]:[tag]运行镜像 -d 后台运行 -p 端口 8080:8099 本地主机的 8080 被映射到了容器的 8099 端口
docker build -t [imagename]:[tag] .制作镜像
docker start [container id]将一个已经终止的容器启动运行
docker stop [container id]终止容器
docker restart将一个运行态的容器终止,然后再重新启动

部署

  1. 项目打包生成
npm run build
  1. 在虚拟机上新建一个文件夹,并将打包好的文件放入 app 文件夹中/www
├─ stationUI
└─ storeUI
    └─ dev
        ├─ Dockerfile
        ├─ nginx.conf
        └─ app
            ├─ index.html
            └─ static
  1. app文件夹同目录新建Dockerfile文件,并编辑
FROM nginx
MAINTAINER  MY Name <myname@whruobei.com>
#把当前打包工程的html复制到虚拟地址
ADD ./app /usr/share/nginx/html
#使用自定义nginx.conf配置端口和监听
COPY nginx.conf /etc/nginx/nginx.conf
RUN /bin/bash -c 'echo init ok!!!'
  1. 在 app 文件夹同目录新建nginx.conf文件,并编辑
worker_processes auto;
#pid /usr/local/nginx/logs/nginx.pid;
#error_log /usr/local/nginx/logs/error.log crit;
worker_rlimit_nofile 1000000;
events {
worker_connections 65536;
multi_accept on;
use epoll;
}

http {
include mime.types;
default_type application/octet-stream;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off;

keepalive_timeout 10;
client_header_timeout 10;
client_body_timeout 10;
reset_timedout_connection on;
send_timeout 10;

limit_conn_zone $binary_remote_addr zone=addr:5m;
limit_conn addr 100;

gzip on;
gzip_disable "msie6"
gzip_static on;
gzip_proxied any;
gzip_min_length 1000;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;

# include /etc/nginx/conf.d/*.conf;
# include /etc/nginx/sites-enabled/*;

server {
listen 80;
# 接口服务的IP地址
server_name localhost;
charset utf-8;
access_log off;
# ElecManageSystem-应用文件夹名称 app-index.html页面所在文件夹
root /usr/share/nginx/html;
location / {
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
  1. 在虚拟机进入到已新建的文件夹目录,并执行制作镜像命令
docker build -t lcbstore:dev .
  1. 在返回Successfully后,先使用docker images查看镜像
  2. 运行镜像 docker run -d -p 80:80 lcbstore:dev 然后就可以在虚拟机localhost:80或在桌面浏览器 主机IP:端口号访问了

Docker cloud

将本地的提交到网络提供给其他人pull 在 hub.cloud.com 上注册账号并新建存储仓库 yourID:cloudname

命令说明
docker login登录 docker
docker build yourID/cloudname:tag .打镜像
docker tag [image id] yourID/cloudname:tag将本地镜像标记为对应格式
docker push yourID/cloudname:tag推镜像到仓库
docker pull yourID/cloudname:tag拉取镜像
若其他用户无法拉取,需要先在hub.docker上Collaboratorsadd user

Nginx

Nginx是一个使用 c 语言开发的高性能的http服务器、反向代理服务器及电子邮件(IMAP/POP3)代理服务器

应用场景

  • http服务器:可以做网页静态服务器
  • 虚拟主机:可以实现在一台服务器虚拟出多个网站。不同端口/不同域名
  • 反向代理,负载均衡:当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况