您的位置:首页 > 运维架构 > Docker

docker用法与实例

2018-11-26 08:58 956 查看
  1. 镜像:
    a. 拉取:
    docker  pull  名称

    b. 查看:

    docker  image  ls  -a

    c. 删除指定镜像:

    docker  image  rm  -f  名称

    d. 删除无用镜像:

    docker  image  prune
  2. 容器:
    a. 初始化:
    docker  run  -p  本地端口:镜像内端口  -v  本地文件:远程文件  -d(后台运行)  -it(交互式运行)  镜像名

    b. 启动:

    docker  container  start  短名称

    c. 停止:

    docker  container  stop  短名称

    d. 删除指定容器:

    docker  container  rm  -f  名称

    e. 删除停止中的容器:

    docker  container  prune
  3. 实例(使用官方centos镜像搭建nginx):
    a. 创建文件夹:
    mkdir  -p  nginx/{conf,www}

    b. 创建nginx配置文件:
    vim nginx/conf/nginx.conf

    daemon off;
    user nginx;
    worker_processes  1;
    events {
    worker_connections  1024;
    }
    http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
    listen       80;
    server_name  localhost;
    location / {
    root   html;
    index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   html;
    }
    }
    }

    c. 创建测试文件:
    vim nginx/www/index.html

    hello

    d. 创建Dockerfile文件:
    vim Dockerfile

    FROM centos
    RUN yum -y install pcre-devel \
    && yum -y install zlib-devel \
    && yum -y install wget \
    && yum -y install gcc automake autoconf libtool make \
    && mkdir /data \
    && cd /data \
    && wget https://nginx.org/download/nginx-1.15.6.tar.gz \
    && tar -xzf nginx-1.15.6.tar.gz \
    && cd nginx-1.15.6 \
    && ./configure \
    && make \
    && make install \
    && useradd -s /sbin/nologin -M nginx
    COPY $PWD/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf
    COPY $PWD/nginx/www/index.html /usr/local/nginx/html/index.html
    CMD ["/usr/local/nginx/sbin/nginx"]

    e. 创建镜像:

    docker  build  -t  test  .


    f. 运行:

    docker  run  -p  80:80  -d  test


    g. 测试:

    curl  localhost

  4. 参考文档:
    a. 源码安装nginx:
    https://www.geek-share.com/detail/2747887004.html
    b. docker中文教程:
    https://yeasy.gitbooks.io/docker_practice/content/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  docker 用法 实例