美文网首页
docker基础知识梳理

docker基础知识梳理

作者: watson168 | 来源:发表于2017-11-14 17:54 被阅读83次

    一、加速器配置

    加速器配置(每个用户的配置不同):
    首先需要到daocloud 中注册获得一个加速配置的脚本

    [root@localhost ~]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://32399471.m.daocloud.io
    docker version >= 1.12
    {"registry-mirrors": ["http://32399471.m.daocloud.io"]}
    Success.
    You need to restart docker to take effect: sudo systemctl restart docker 
    

    二、docker基础常用的知识点

    1.获取镜像

    docker pull [选项] [Docker Registry地址]<仓库名>:<标签>
    如果不指定registry地址、标签 会默认使用官方地址,最新版本
    [root@localhost ~]# docker pull nginx
    Using default tag: latest
    latest: Pulling from library/nginx
    bc95e04b23c0: Pull complete 
    110767c6efff: Pull complete 
    f081e0c4df75: Pull complete 
    Digest: sha256:ecc3e131cf16c20d9f4559b03ef3ffcd3b5d01a1559c7bb9d3070890f3fca061
    Status: Downloaded newer image for nginx:latest
    

    2.列出所下载的镜像

    [root@localhost ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              1e5ab59102ce        9 hours ago         108MB
    

    3.运行容器

    docker run --name webserver -d -p 81:80 nginx:latest

    4.Dockerfile 案例

    FROM debian:jessie
    RUN buildDeps='gcc libc6-dev make' \
        && apt-get update \
        && apt-get install -y $buildDeps \
        && wget -O redis.tar.gz "http://download.redis.io/releases/redis-3.2.5.tar.gz" \
        && mkdir -p /usr/src/redis \
        && tar -xzf redis.tar.gz -C /usr/src/redis  \
        && make -C /usr/src/redis \
        && make -C /usr/src/redis install \
        && rm -rf /var/lib/apt/lists/* \
        && rm redis.tar.gz \
        && rm -r /usr/src/redis \
        && apt-get purge -y --auto-remove $buildDeps
    

    5.查看所有的容器运行状态

    [root@watson-docker ~]# docker  ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
    cdf2d059cf44        nginx               "nginx -g 'daemon ..."   16 hours ago        Up 16 hours         80/tcp                 temp_nginx
    6bba12865819        nginx:V2            "nginx -g 'daemon ..."   17 hours ago        Up 17 hours         0.0.0.0:8000->80/tcp   web
    94e24c4c98cf        nginx:latest        "nginx -g 'daemon ..."   17 hours ago        Up 17 hours         0.0.0.0:8001->80/tcp   test_nginx
    

    6.查看容器的端口映射

    [root@watson-docker ~]# docker port test_nginx
    80/tcp -> 0.0.0.0:8001
    

    7.获取容器的所有的变量

    [root@watson-docker ~]# docker inspect test_nginx
    [
        {
            "Id": "94e24c4c98cfd574f916a7c351709333299fe22f57d0a0409947a5c3c6947536",
            "Created": "2017-10-12T08:47:13.525189618Z",
            "Path": "nginx",
            "Args": [
                "-g",
                "daemon off;"
                ......
    

    8.进入容器内部[-i 表示启用交互模式,-t表示启用一个终端,-d 表示在后台运行]

    [root@watson-docker ~]# docker exec -it  test_nginx bash
    root@94e24c4c98cf:/#             
    

    9.容器互联
    使用 --link 参数可以让容器之间安全的进行交互。[--link 参数的格式为 --link name:alias ,其中 name 是要链接的容器的名称, alias 是这个连接的别名]
    ①先创建一个db容器

    [root@watson-docker ~]# docker run --name db -d training/postgres
    55745b7b7707e53d446222049d3e697abfa90f7d4edf6253428c7e47aa216b3f
    

    ②创建一个web服务,连接到db容器(并未指定外部的端口映射)

    [root@watson-docker ~]# docker run  -itd --name web --link db:xx  nginx:V2
    31ae5af08109e14d52e080c548f7d942f363b0b40e61312036a099c63ccb4543
    

    ③查看db、web并未有对外的端口映射

    [root@localhost ~]# docker port web 
    [root@localhost ~]# docker port db
    

    10.指定容器的主机名、dns

    [root@localhost ~]# docker run -itd -h test01 --name test-web --dns=192.168.6.6 nginx:latest    
    

    11.设定容器访问外部网络

    设置:net.ipv4.ip_forward = 1

    1. Docker 底层的核心技术包括
    • Linux 上的命名空间(Namespaces)
    • 控制组(Control groups):资源控制
    • Union 文件系统(Union file systems):一种分层、轻量级并且高性能的文件系统
      容器格式(Container format)

    相关文章

      网友评论

          本文标题:docker基础知识梳理

          本文链接:https://www.haomeiwen.com/subject/csmfvxtx.html