zoukankan      html  css  js  c++  java
  • Docker基础

    Docker

    1. Docker 基础用法和命令帮助

    1.1 常用命令

    • docker pull && docker push
      • $ sudo docker pull --help
      • pull 拉取镜像 Usage: docker pull [OPTIONS] NAME[:TAG] Pull an image or a repository from the registry
      • -a, --all-tags=false Download all tagged images in the repository
      • $ sudo docker push
      • push 推送指定镜像 Usage: docker push NAME[:TAG] Push an image or a repository to the registry
    • 示例:
    $ sudo docker pull ubuntu # 下载官方 ubuntu docker 镜像,默认下载所有 ubuntu 官方库镜像 $ sudo docker pull ubuntu:14.04 # 下载指定版本 ubuntu 官方镜像 
    $ sudo docker push 192.168.0.100:5000/ubuntu # 推送镜像库到私有源[可注册 docker 官方账户,推送到官方自有账户] $ sudo docker push 192.168.0.100:5000/ubuntu:14.04 # 推送指定镜像到私有源 
    
    • 阿里云docker镜像库:https://dev.aliyun.com/search.html
    • docker run :
      • Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] Run a command in a new container
      • --name="" Assign a name to the container # 设置容器名
      • --net="bridge" Set the Network mode for the container # 设置容器网络模式
        • 'bridge': creates a new network stack for the container on the docker bridge
        • 'none': no networking for this container
        • 'container:<name|id>': reuses another container network stack
        • 'host': use the host network stack inside the container. Note: the host mode gives the container full access to local system services such as D-bus and is therefore considered insecure.
      • --expose=[] Expose a port from the container without publishing it to your host # 指定对外提供服务端口
      • -P, --publish-all=false Publish all exposed ports to the host interfaces # 自动映射容器对外提供服务的端口
      • -p, --publish=[] Publish a container's port to the host # 指定端口映射
        • format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort (use 'docker port' to see the actual mapping)
      • -h, --hostname="" Container host name # 设置容器主机名
      • -i, --interactive=false Keep stdin open even if not attached # 保持标准输出开启即使没有 attached
      • --link=[] Add link to another container (name:alias) # 添加链接到另外一个容器
      • --name="" Assign a name to the container # 设置容器名
      • --rm=false Automatically remove the container when it exits (incompatible with -d) # 如果容器退出自动移除容器和 -d选项冲突
      • -d, --detach=false Detached mode: Run container in the background, print new container id # 后台运行容器
      • -u, --user="" Username or UID # 指定运行容器的用户uid 或者用户名
      • -v, --volume=[] Bind mount a volume (e.g., from the host: -v /host:/container, from docker: -v /container) # 挂载卷
      • --volumes-from=[] Mount volumes from the specified container(s) # 从指定容器挂载卷
      • -w, --workdir="" Working directory inside the container # 指定容器工作目录
      • --env=[] Set environment variables # 定义环境变量
      • --entrypoint="" Overwrite the default entrypoint of the image
      • -h, --hostname="" Container host name # 设置容器主机名
      • -t, --tty=false Allocate a pseudo-tty # 分配伪终端
    • docker attach CONTAINER-ID
      • 当前 shell 下 attach 连接指定运行镜像
    • 相关快捷键
      • 使容器退出:Ctrl-D exit
      • 从容器断开连接deattach:Ctrl-P + Ctrl-Q
    • docker start CONTAINER [CONTAINER...]
      • 运行一个或多个停止的容器
    • docker stop CONTAINER [CONTAINER...]
      • 停掉一个或多个运行的容器-t选项可指定超时时间
    • docker kill [OPTIONS] CONTAINER [CONTAINER...]
      • 默认 kill 发送 SIGKILL 信号-s可以指定发送 kill 信号类型
    • docker restart [OPTIONS] CONTAINER [CONTAINER...]
      • 重启一个或多个运行的容器-t选项可指定超时时间
    • docker pause CONTAINER
      • 暂停一个容器,方便 commit
    • docker unpause CONTAINER
      • 继续暂停的容器
    • docker inspect CONTAINER|IMAGE [CONTAINER|IMAGE...]
      • 查看容器或者镜像的详细信息
    • docker docker history [OPTIONS] IMAGE
      • 显示一个镜像的历史

    2. Docker管理

    • Docker Web管理工具
    • Portainer:
      1. Portainer将docker的命令行模式能提供的信息和操作通过web来实现
      2. 可以在它提供的web中实现docker的命令行操作

    Docker 网络配置

    • docker port
      • Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口对应的容器内部源端口

    Docker 创建镜像:

    • docker import *.tar
      • Create a new filesystem image from the contents of a tarball
      • 从tar包中的内容创建一个新的文件系统映像[对应 export]
    • docker build Dockerfile
      • Build an image from a Dockerfile
      • 通过 Dockerfile 定制镜像
    • 参考:两种方式创建你自己的 Docker 基本映像
      • 方法2:
        • 在 Docker registry 中,有一个被称为 Scratch 的使用空 tar 文件构建的特殊库,如下能创建一个空镜像:
        • 注意:因为这个空镜像没有运行shell的基本环境,并不能运行
    //表示从/dev/null中创建一个新的tar包,重定向到docker import中,成为scratch
    james@james:~/software/docker > tar cv --files-from /dev/null |sudo  docker import - scratch
    sha256:ff266ec37e11e1759166c7d0212d5516e2b5bb54673059ae7888c89528ebe8c8
    james@james:~/software/docker > sudo docker images
    REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
    scratch               latest              ff266ec37e11        8 seconds ago       0 B
    portainer/portainer   latest              d449426bc5a4        4 weeks ago         9.162 MB
    nfnty/arch-mini       archCustum          393a394f60f0        6 weeks ago         544.8 MB
    nfnty/arch-mini       latest              393a394f60f0        6 weeks ago         544.8 MB
    hello-world           latest              48b5124b2768        6 weeks ago         1.84 kB
    
    • 之后再使用这个镜像配合Dockerfile来创建一个新的小容器:
      • 注意:运行docker build命令的目录下需要有一个Dockerfile目录,在该目录下放置Dockerfile配置文件和相关文件。命令才能运行,否则会有错。
    james@james:~/code/docker/docker-test-image > cat Dockerfile
    #version 0.1
    #author chaolong.jiang
    #author James Yang
    
    FROM    java:latest
    MAINTAINER "James drawnkid@gmail.com"
    
    ADD bin/* /usr/local/
    
    ADD conf/*.yml /meta/
    
    ADD james-spring-boot-core-web/james-spring-boot-core-0.0.1-SNAPSHOT.jar /home/admin/james-spring-boot-core-web
    
    ADD maven/apache-maven-3.3.9  /usr/local/maven
    
    #add dependency package into image
    #RUN sh /usr/local/install.sh
    ENTRYPOINT ["sh","/usr/local/install.sh"]
    
    #define log path
    #Volume ["/usr/local/test/log"]
    
    #run test script
    #ENTRYPOINT ["python", "/usr/local/runTest.py"]
    
    #CMD ["--testsuite", "smoking"]
    james@james:~/code/docker/docker-test-image > 
    james@james:~/code/docker/docker-test-image > docker build .
    [sudo] password for james:
    Sending build context to Docker daemon 25.97 MB
    Step 1 : FROM java:latest
     ---> d23bdf5b1b1b
    Step 2 : MAINTAINER "James drawnkid@gmail.com"
     ---> Running in caef1531da40
     ---> 84677306cf1b
    Removing intermediate container caef1531da40
    Step 3 : ADD bin/* /usr/local/
     ---> 44ebf30c2368
    Removing intermediate container 2f7e45dc30d2
    Step 4 : ADD conf/*.yml /meta/
     ---> 925759453f19
    Removing intermediate container 1fa08beeaad5
    Step 5 : ADD james-spring-boot-core-web/james-spring-boot-core-0.0.1-SNAPSHOT.jar /home/admin/james-spring-boot-core-web
     ---> 991f1116acd8
    Removing intermediate container 66d975c774d1
    Step 6 : ADD maven/apache-maven-3.3.9 /usr/local/maven
     ---> dbb353eac85c
    Removing intermediate container 449fcb4314d5
    Step 7 : ENTRYPOINT sh /usr/local/install.sh
     ---> Running in 4cb310d5036a
     ---> dfd7d4ac308b
    Removing intermediate container 4cb310d5036a
    Successfully built dfd7d4ac308b
    
    
    • 此时新创建的images的REPOSITORY和TAG都是空的,使用上面的命令可以修改。
      • docker tag imageid name:tag

    Dockerfile

    FROM reg.docker.alibaba-inc.com/rds-mysql-client:1.0 //指定构建镜像的基础源镜像
    
    MAINTAINER james <james@james.com> //指定创建镜像的用户
    COPY bin /alidata/bin //COPY <src>... <dest> 复制文件或目录到容器指定路径中,和ADD用法一样,但是不能指定远程url
    #RUN sh /alidata/bin/run.sh //每条RUN指令将在当前镜像基础上执行指定命令,并提交为新的镜像,后续的RUN都在之前RUN提交后的镜像为基础
    #ENTRYPOINT tail -f /etc/hosts //配置容器启动后执行的命令,并且不可被 docker run 提供的参数覆盖,而CMD是可以被覆盖的。每个 Dockerfile 中只能有一个ENTRYPOINT,当指定多个时,只有最后一个生效。
    ### 安装tsar
    RUN yum install -y tsar.x86_64
    ### copy采集script
    RUN mkdir -p /usr/alisys
    RUN wget http://tianjimon.oss-cn-hangzhou.aliyuncs.com/tjmonitor-docker.tar.gz
    RUN tar -zxvf tjmonitor-docker.tar.gz -C /usr/alisys/
    RUN echo "*/1 * * * * /usr/alisys/tianjimon/tjmonitor >/dev/null 2>&1" >> /var/spool/cron/root
    
    ENTRYPOINT ["/usr/bin/python", "/alidata/bin/init_metadb.py"] //配置容器启动后执行的命令,并且不可被 docker run 提供的参数覆盖,而CMD是可以被覆盖的.通过ENTRYPOINT使用 exec form 方式设置稳定的默认命令和选项,而使用CMD添加默认之外经常被改动的选项
    CMD ["--action", "upgrade"] //CMD指定在 Dockerfile 中只能使用一次,如果有多个,则只有最后一个会生效。
    CMD的目的是为了在启动容器时提供一个默认的命令执行选项。如果用户启动容器时指定了运行的命令,则会覆盖掉CMD指定的命令。
    ### CMD会在启动容器的时候执行,build 时不执行,而RUN只是在构建镜像的时候执行,后续镜像构建完成之后,启动容器就与RUN无关了,这个初学者容易弄混这个概念,这里简单注解一下。
    
    

    使用Dockerfile生成镜像

    • 这是一个基于docker官方java镜像的测试镜像
    james@james:~/code/docker/docker-test-image > cat Dockerfile
    #version 0.1
    #author James Yang
    
    FROM    java:latest
    MAINTAINER "James drawnkid@gmail.com"
    
    ADD bin/* /usr/local/
    
    ADD conf/*.yml /meta/
    
    ADD james-spring-boot-core-web /home/admin/james-spring-boot-core-web
    
    #add dependency package into image
    RUN sh /usr/local/install.sh
    
    #define log path
    Volume ["/usr/local/test/log"]
    
    #run test script
    ENTRYPOINT ["python", "/usr/local/runTest.py"]
    
    CMD ["--testsuite", "smoking"]
    
    

    5.2 更新镜像

    • docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
      • 提交指定容器为镜像
        • -a, --author="" Author (e.g., "John Hannibal Smith hannibal@a-team.com")
        • -m, --message="" Commit message
        • -p, --pause=true Pause container during commit
        • 默认 commit 是暂停状态
    airs-MacBook-Air:python air$ docker commit -a James -m "add vi editer" 182 local/archlinux:vi
    sha256:b1744e6892723d6ae5a6203ebbe594e3f7704c92fa7ba55ad3b3328800f07fd2
    

    5.3 镜像操作

    • Usage: docker save [OPTIONS] IMAGE [IMAGE...]

      • Save an image(s) to a tar archive (streamed to STDOUT by default)
      • 保存一个镜像为一个 tar 包[对应 load]
    • Usage: docker load [OPTIONS]

      • Load an image from a tar archive or STDIN
      • 从一个 tar 包中加载一个镜像[对应 save]
    [root@dmsag /root/dtdream]
    #docker load < dtdream-webapp-base-tomcat4.tar
    
    
  • 相关阅读:
    hdu1150&&POJ1325 Machine Schedule---最小点覆盖
    hdu-1068&&POJ1466 Girls and Boys---最大独立集
    hdu-2680 Choose the best route---dijkstra+反向存图或者建立超级源点
    hdu-1317 XYZZY---Floyd判连通+bellman最短路
    hdu-1874 畅通工程续---模板题
    hdu-2112 HDU Today---dijkstra+标号
    hdu-2066 一个人的旅行---模板题
    hdu-3790 最短路径问题---dijkstra两重权值
    hdu-2544 最短路---模板题
    BZOJ3529: [Sdoi2014]数表
  • 原文地址:https://www.cnblogs.com/drawnkid/p/6718323.html
Copyright © 2011-2022 走看看