初识Docker

来源:互联网 发布:笔记本跑分软件 编辑:程序博客网 时间:2024/06/07 01:51

安装Docker

  1. 安装https源
    sudo apt-get install -y apt-transport-https
  2. 添加https源的gpg秘钥
    sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv-keys 58118E89F3A912897C070ADBF76221572C52609D
  3. 获取操作系统的代号
    lsb_release -c
    4.添加文件(注意保持操作系统代号一直,Ubuntu14.04====>>>>trusty)
    sudo cat < /etc/apt/sources.list.d/docker.listdeb https://apt.dockerproject.org/repo ubuntu-trusty main
    EOF
    5.更新软件包
    sudo apt-get update
    6.开始安装docker
    sudo apt-get install -y docker-engine
    6.1 利用官方脚本安装docker
    sudo curl -sSL https://get.docker.com/ |sh
  4. 启动docker
    sudo service docker start
  5. 配置docker

    为了避免每次使用docker命令都需要使用root权限,我们赋予普通用户加入docker用户组中.

    sudo usermod -aG docker UER_NAME

    设置之后,退出重新登录.并重启==docker sudo service docker start==

认识Images

获取镜像

docker pull NAME[:TAG]
name镜像名称,tag镜像标签,通常为版本信息
例如:下载一个Ubuntu镜像

root@ubuntu:~# docker pull ubuntu:14.0414.04: Pulling from library/ubuntu1d8592394ba1: Pull complete 01aa7f61ccd1: Pull complete 5dd2552a960e: Pull complete 7cbe941c5e3e: Pull complete 2549ecfb14c6: Pull complete Digest: sha256:30204139c6ab96ebd75d72f34db390f28c4decd5e563488b4e485bf979397b67Status: Downloaded newer image for ubuntu:14.04

-a, –all-tags=true|false 获取仓库全部地址(默认false)

注意 : 如果下载非官方镜像 需要在name前加上仓库地址

查看镜像

  1. 查看镜像列表
    docker images
    “`
    root@ubuntu:~# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    ubuntu 14.04 132b7427a3b4 3 days ago 188MB

子选项-a, –all=true|false 列出所有镜像,默认false
–digests=true|false 列出镜像的数字摘要文件,默认false
-f, –filter=[] 过滤镜像,如 dangling=true 只显示没有使用的镜像
–no-trunc=true|false 对输出内容太长部分进行截断,默认是

root@ubuntu:~# docker tag ubuntu:14.04 myubuntu:14root@ubuntu:~# docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEmyubuntu            14                  132b7427a3b4        3 days ago          188MBubuntu              14.04               132b7427a3b4        3 days ago          188MB```
  1. 用inspect查看镜像详细信息
 docker inspect ubuntu:14.04
  1. 查看镜像历史信息
docker history ubuntu:14.04
  1. 搜索镜像
docker search --automated -s 80 redis

注意
–automated=true|false 仅显示自动创建的镜像,默认否

–no-trunc=true|false 输出信息是否截断显示,默认否

-s, –stars=x 输出指定星级以上的镜像,默认0

  1. 删除镜像

    1. 使用标签删除镜像
    docker rmi myubuntu:14

    如果成功返回:Untagged: myubuntu:14

    注意:利用标签删除镜像,如果本地存在镜像id相同的镜像,不影响其他标签镜像

    1. 利用镜像ID删除镜像
      注意: – 删除一个正在被容器使用的镜像
root@ubuntu:~# docker run ubuntu:14.04 echo 'hell, i am here!'hell, i am here!root@ubuntu:~# docker rmi 132b7427a3b4Error response from daemon: conflict: unable to delete 132b7427a3b4 (must be forced) - image is being used by stopped container 5102bf6a1010root@ubuntu:~# docker ps -a CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                          PORTS               NAMES5102bf6a1010        ubuntu:14.04        "echo 'hell, i am ..."   About a minute ago   Exited (0) About a minute ago                       hungry_thompsonroot@ubuntu:~# docker rm 5102bf6a10105102bf6a1010root@ubuntu:~# docker ps -a CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

从上可以看出,通常情况下,我们不能直接删除一个被容器正在使用的镜像,我们需要先把这个容器删除掉,然后在清除对应镜像.
当然我们可以通过 ==docker rmi -f xxx== 强制删除镜像
6. 创建镜像
1. 基于已有镜像的容器创建

    ```    stormfast@ubuntu:~$ docker run -it ubuntu:14.04 /bin/bash    root@99cd2ed8fe20:/# touch test    root@99cd2ed8fe20:/# exit    exit    stormfast@ubuntu:~$ docker commit -m "added a new file" -a "Docker zx" 99cd2ed8fe20 test:0.1    sha256:6a3a026cd43a44ee811afed96361eb368f2dff832314367c2cfc965ef3b6d54f    stormfast@ubuntu:~$ docker images    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE    test                0.1                 6a3a026cd43a        8 seconds ago       188MB    ubuntu              14.04               132b7427a3b4        3 days ago          188MB    ```-it (为容器分配一个伪输入终端)子选项:     -a, --author="" 作者信息    -c, --change=[] 提交的时候执行Dockerfile指令    -m, --message="" 提交信息    -p, --pause=true 提交时暂停容器运行2. 基于本地模板导入3. 基于Dockerfile创建

7. 存入和载入镜像
1. 存出镜像

```docker save -o ubuntu_14.04.tar ubuntu:14.04```2. 载入镜像docker load --input ubuntu_14.04.tar或者docker load < ubuntu_14.04.tar

8. 上传镜像
我们可以上传自己的镜像到远程仓库,方便之后使用

```docker push NAME[:TAG] | [REGISTRY_HOST[REGISTRY_PORT]/]NAME[:TAG]``````stormfast@ubuntu:~$ docker tag test:0.1 zxfendoubuzhi/test:lateststormfast@ubuntu:~$ docker push zxfendoubuzhi/test:latest The push refers to a repository [docker.io/zxfendoubuzhi/test]ada26b46e50d: Pushed 7bf04ad1d006: Pushed 023b7696bd58: Pushed ad37eb38337a: Pushed 350bf4dddc59: Pushed```*注意*:保持==zxfendoubuzhi==/test和你远程仓库的用户名相同
原创粉丝点击