docker

201 阅读2分钟

docker命令

  • 查看docker安装的镜像
docker images ls
  • 拉取镜像
docker pull 镜像名
  • 删除镜像
docker rmi 镜像名
  • 查看正在运行的容器
docker container ps
  • 查看正在所有的容器,不管在没在运行
docker container ps -a
  • 启动容器
docker run 容器名
  • 交互式,并显示一个虚拟的窗口(-i -t)
docker run -i -t 容器名
  • 在后台启动容器,不会看起来一直在运行(-d)
docker run -d nginx
  • 自动映射端口号
docker run -d -P nginx

屏幕快照 2021-09-24 上午11.37.00.png 尝试访问 屏幕快照 2021-09-24 上午11.35.22.png

  • 进入正在运行的容器(exec)
docker exec -it eb22e0e8fef9(容器id) /bin/bash
  • 退出当前正在运行容器的界面
exit
  • 停止容器
docker container stop 容器id|容器名
  • 强制停止
docker kill 容器id|容器名
  • 删除容器
docker rm 容器id
  • 删除所有的容器
docker rm $(docker ps -a -q)
  • 移除所有停止的容器
docker container prune

docker nginx

docker exec -it eb22e0e8fef9(容器id) /bin/bash进入nginx容器之后

  1. cd /etc/nginx
  2. cat nginx.conf

image.png

user  nginx;  # 启动nginx用户
worker_processes  1; # worker的进程数,一般和cpu数相等


error_log  /var/log/nginx/error.log warn;  # 错误日志路径,日志的格式
pid        /var/run/nginx.pid;   #进程的id

events {

    worker_connections  1024;  # 工作进程的最大连接数

}

http {
    include       /etc/nginx/mime.types;  # 服务器返回服务的content-type的格式
    default_type  application/octet-stream;
    
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;  #保持连接的时长 
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}
  1. cd /etc/nginx/conf.d
  2. cat default.conf

浏览器输入http://localhost:32768/ 32768是nginx被配置的80映射的端口

image.png

image.png

server {
    listen       80;  # 监听端口
    listen  [::]:80;
    server_name  localhost; # 服务器名称或者ip
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    location / {
        root   /usr/share/nginx/html;  # 跟路径根目录
        index  index.html index.htm;  # 路径下的静态资源

    }
    #error_page  404              /404.html;  # 服务器返回404可以访问的页面
    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #location ~ /\.ht {
    #    deny  all;
    #}
}
  1. cd /usr/share/nginx/html;

image.png 6. echo hello > hello.html(添加一个文件)

屏幕快照 2021-09-24 上午11.48.43.png 7. 退出正在运行的容器,执行curl http://localhost:32768/hello.html,可以访问到文件中的内容

  1. cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {

        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}

新增服务

  • 容器里面修改hosts
vi /etc/hosts

127.0.0.1 www.wytest1.com
  • 在容器/bin/bash目录下
mkdir data/wy/index.html

index.html 输入一些文本

  • 在/etc/nginx/conf.d下把default.conf 拷贝一份到wy.conf
cp default.conf wy.conf
  • 修改wy.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  www.wytest1.com;
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    location / {
        root   /data/wy;
        index  index.html index.htm;
    }
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

保存hosts ,容器中ping www.wytest1.com看出保存成功

配置查看请求的信息

  • vi /etc/nginx/conf.d/default.conf
  • 配置status
location /status {
    sub_status on;
}
  • 重启
  • 访问http://localhost:32769/status
Active connections: 2  // 当前nginx正在处理的活动连接数
server accepts handled requests
 9 9 12 
Reading: 0 Writing: 1 Waiting: 1 

随机主页

随机读取root配置的文件夹下的html文件

  • vi /etc/nginx/conf.d/default.conf
  • 配置
location / {
    root /usr/share/ginx/html
    random_index on
}
  • 重启

gzip压缩

location / {
    gzip on;
    gzip_static on;
}

nginx 配置跨域

location ~ .*\.json$ {
    root /data/www/json. # 假设请求的是json文件
    add_header Access-Control-Allow-Origin http://xxxx:xxx; # 可以解决get请求的跨域, http://xxxx:xxx 代表发起请求的地址,服务器允许这个地址的访问跨域
    add_header Access-Control-Allow-Methods POST,GET,PUT,DELETE,OPTIONS; # 配置可以跨域的请求
}

容器中使用vim

报错bash: vi: command not found 解决: 容器中

  1. apt-get
  2. apt-get install vim

Dockerfile

  • 查看镜像或容器
docker image inspect nginx
  1. mkdir docker-wy
  2. docker-wy/ mkdir wynode
  3. wynode/ touch Dockerfile