【Docker学习笔记】一 Dockers简介跟环境配置

196 阅读1分钟

简介

类似于虚拟机,可以通过一个镜像创建好几个容器。但是对于硬件要求就很高了,每一个容器都需要分配cpu 内存 硬盘资源。虚拟机是基于操作系统的。Docker是微型的虚拟机,是线程调度的,基于线程。所以对于镜像 容器 网络 等等的操作就会有一堆的操作命令。

安装nginx

  1. docker pull nginx //拉取一个nginx的镜像
  2. 通过镜像创建出nginx容器
docker run -p:80:80 nginx  //启动容器
docker run -p 80:80 -it -d nginx bash  //后台启动,并进入容器,显示命令行窗口

此时访问宿主的ip地址就出现了nginx的默认成功界面

image.png 3. 进入刚才创建的容器中 查看配置文件参数 docker exec -it nginx bash //进入容器 cat /etc/nginx/nginx.conf //查看默认配置


user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
   worker_connections  1024;
}


http {
   include       /etc/nginx/mime.types;
   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;
}

创建nginx容器 并后台运行 指定端口 映射配置文件 映射项目目录

docker container run -d --name nginx -p 80:80 -v /www/config/nginx:/etc/nginx/conf.d -v /www/webroot:/usr/share/nginx --privileged nginx
[root@localhost www]# tree
.
├── config
│   └── nginx
│       ├── default.conf
│       └── liiy_demo_com.conf
├── log
│   └── nginx
└── webroot
    ├── html
    │   └── index.html
    └── liiy_demo_com
        ├── index.html
        └── index.php
        
[root@localhost www]# cat config/nginx/default.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #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;

    # 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;
    #}
}