docker安装和使用

一、安装docker

1.1 centos7.2安装docker

环境:centos7.2

安装方法:https://docs.docker.com/engine/installation/linux/centos/

1)Make sure your existing yum packages are up-to-date.

CentOS使用yum update更新时不升级内核

  1. cp /etc/yum.conf /etc/yum.confbak
  2. 方法一、修改yum的配置文件
  3. vi /etc/yum.conf 在[main]的最后添加 exclude=kernel*
  4. 方法二、直接在yum的命令后面加上如下的参数:
  5. yum --exclude=kernel* update
  6. 查看系统版本 cat /etc/issue
  7. 查看内核版本 uname -a

下面的命令,生产环境不要随便使用!

  1. sudo yum --exclude=kernel* update

2)Add the yum repo.

  1. sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
  2. [dockerrepo]
  3. name=Docker Repository
  4. baseurl=https://yum.dockerproject.org/repo/main/centos/7/
  5. enabled=1
  6. gpgcheck=1
  7. gpgkey=https://yum.dockerproject.org/gpg
  8. EOF

3)Install the Docker package.

  1. sudo yum -y install docker-engine

4)Start the Docker daemon.

  1. sudo service docker start

5)Verify docker is installed correctly by running a test image in a container.

  1. $ sudo docker run hello-world
  2. Unable to find image 'hello-world:latest' locally
  3. latest: Pulling from hello-world
  4. a8219747be10: Pull complete
  5. 91c95931e552: Already exists
  6. hello-world:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
  7. Digest: sha256:aa03e5d0d5553b4c3473e89c8619cf79df368babd1.7.1cf5daeb82aab55838d
  8. Status: Downloaded newer image for hello-world:latest
  9. Hello from Docker.
  10. This message shows that your installation appears to be working correctly.
  11. 。。。。。。

6)Run docker ps -a to show all containers on the system.

  1. [root@bogon ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 30973426a4de hello-world "/hello" 3 minutes ago Exited (0) 3 minutes ago adoring_mestorf

1.2 创建docker用户组

docker用root权限运行,所以普通用户总是加sudo来执行命令,所以我们创建docker用户组,有root的权限,然后把普通用户添加到docker组里,这样普通用户执行docker命令就不用加sudo 了。

1)Create the docker group.

  1. sudo groupadd docker

2)Add your user to docker group.

  1. sudo usermod -aG docker your_username

3)Log out and log back in.

This ensures your user is running with the correct permissions.

4)Verify your work by running docker without sudo.

  1. $ docker run hello-world

1.3 uninstall

1)List the package you have installed.

  1. $ yum list installed | grep docker
  2. yum list installed | grep docker
  3. docker-engine.x86_64 1.7.1-1.el7 @/docker-engine-1.7.1-1.el7.x86_64.rpm

2)Remove the package.

  1. $ sudo yum -y remove docker-engine.x86_64
  2. This command does not remove images, containers, volumes, or user-created configuration files on your host.

3)To delete all images, containers, and volumes, run the following command:

  1. $ rm -rf /var/lib/docker

4)Locate and delete any user-created configuration files.

1.4 Learn about images & containers

An image is a filesystem and parameters to use at runtime. It doesn’t have state and never changes.

A container is a running instance of an image.

When you ran the command, Docker Engine:

  • checked to see if you had the hello-world software image
  • downloaded the image from the Docker Hub (more about the hub later)
  • loaded the image into the container and “ran” it

二、Find and run the whalesay image

Step 1: Locate the whalesay image

浏览器打开:https://hub.docker.com

搜索:whalesay,回车

进入界面,就可以看到用法了。

https://hub.docker.com/r/mendlik/docker-whalesay

Usage:

# Print random fortune cookie message

  1. $ docker run mendlik/docker-whalesay

# Print custom message

  1. $ docker run mendlik/docker-whalesay "Your message"

# Let's see what's inside the container

  1. $ docker run -it --entrypoint /bin/bash mendlik/docker-whalesay

Step 2: Run the whalesay image

我们在容器里,运行whalesay镜像:

  1. docker run mendlik/docker-whalesay cowsay boo

查看本地所有的镜像:

  1. [root@bogon ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. hello-world latest c54a2cc56cbb 7 weeks ago 1.848 kB
  4. mendlik/docker-whalesay latest 552104437e78 5 months ago 172.3 MB

Take a moment to play with the whalesay container a bit.

Try running the whalesay image again with a word or phrase. And you type a lot to get whalesay to talk..

  1. [root@bogon ~]# docker run mendlik/docker-whalesay boo-boo
  2. _________
  3. < boo-boo >
  4. ---------
  5. \
  6. \
  7. \
  8. ## .
  9. ## ## ## ==
  10. ## ## ## ## ===
  11. /""""""""""""""""___/ ===
  12. ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
  13. \______ o __/
  14. \ \ __/
  15. \____\______/

三、Build your own image

Step 1: Write a Dockerfile

1)创建目录,这个目录包含所有你要自建的镜像

  1. mkdir mydockerbuild
  2. cd mydockerbuild

2)编辑文件内容

  1. vi Dockerfile
  2. 添加内容:
  3. FROM mendlik/docker-whalesay:latest

FROM关键字表示你的镜像是基于哪个镜像来做的。

现在我们把fortunes程序加入到镜像里,fortunes 程序会有一个命令,让whale说话。所以我们要安装它。

这行安装软件到镜像里。

Dockerfile添加内容:

  1. RUN apt-get -y update && apt-get install -y fortunes

一旦镜像有需要的软件,你就指定:当镜像加载的时候,运行这个软件。

这一行表示fortune程序,把一个漂亮的quote传递给cowsay程序

  1. CMD /usr/games/fortune -a | cowsay

所以,你的文件全部内容为:

  1. $ cat Dockerfile
  2. FROM mendlik/docker-whalesay:latest
  3. RUN apt-get -y update && apt-get install -y fortunes
  4. CMD /usr/games/fortune -a | cowsay

Step 2: Build an image from your Dockerfile

注意,一定要加点“.”

  1. docker build -t docker-whalesay .

这个命令使用当前目录下的Dockerfile文件。

然后在你本机创建一个叫"docker-whalesay "的镜像

我们看看命令执行输出:

  1. [root@bogon mydockerbuild]# docker build -t docker-whalesay .
  2. Sending build context to Docker daemon 2.048 kB # docker检查他需要创建的东西。
  3. Step : FROM mendlik/docker-whalesay:latest # docker下载mendlik/docker-whalesay镜像,因为之前已经下载,就不用下载了。
  4. ---> 552104437e78
  5. Step : RUN apt-get -y update && apt-get install -y fortunes # 先更新apt-get包
  6. ---> Running in ecb33156fc42
  7. Get: http://security.debian.org jessie/updates InRelease [63.1 kB]
  8. Get: http://security.debian.org jessie/updates/main amd64 Packages [385 kB]
  9. Ign http://httpredir.debian.org jessie InRelease
  10. Get: http://httpredir.debian.org jessie-updates InRelease [142 kB]
  11. Get: http://httpredir.debian.org jessie Release.gpg [2373 B]
  12. Get: http://httpredir.debian.org jessie-updates/main amd64 Packages [17.6 kB]
  13. Get: http://httpredir.debian.org jessie Release [148 kB]
  14. Get: http://httpredir.debian.org jessie/main amd64 Packages [9032 kB]
  15. Fetched kB in 4min 47s (34.0 kB/s)
  16. Reading package lists...
  17. W: Size of file /var/lib/apt/lists/httpredir.debian.org_debian_dists_jessie_main_binary-amd64_Packages.gz is not what the server reported
  18. Reading package lists...
  19. Building dependency tree...
  20. Reading state information...
  21. fortunes is already the newest version.
  22. upgraded, newly installed, to remove and not upgraded.
  23. ---> e78b54d7f981
  24. Removing intermediate container ecb33156fc42 # docker再安装fortunes程序
  25. Step : CMD /usr/games/fortune -a | cowsay # docker 完成创建镜像,并打印输出。
  26. ---> Running in 5ca72e7209e3
  27. ---> 7c8d26884c76
  28. Removing intermediate container 5ca72e7209e3
  29. Successfully built 7c8d26884c76

docker build -t docker-whalesay .

Step 3: Run your new docker-whale

查看本地有哪些镜像:

  1. [root@bogon ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. docker-whalesay latest 7c8d26884c76 About an hour ago 182.1 MB
  4. hello-world latest c54a2cc56cbb 7 weeks ago 1.848 kB
  5. mendlik/docker-whalesay latest 552104437e78 5 months ago 172.3 MB

运行自建的镜像,可以看到docker不再下载任何东西了,因为可以用你本地的东西。

  1. [root@bogon ~]# docker run docker-whalesay
  2. ____________________________________
  3. / /bin/sh -c /usr/games/fortune -a | \
  4. \ cowsay /
  5. ------------------------------------
  6. \
  7. \
  8. \
  9. ## .
  10. ## ## ## ==
  11. ## ## ## ## ===
  12. /""""""""""""""""___/ ===
  13. ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
  14. \______ o __/
  15. \ \ __/
  16. \____\______/

docker run docker-whalesay

四、Create a Docker Hub account & repository

浏览器打开:https://hub.docker.com/?utm_source=getting_started_guide&utm_medium=embedded_MacOSX&utm_campaign=create_docker_hub_account

点注册就好了

用户名/密码:w***/***

验证完邮箱之后,点击Create repository

五、Tag, push, and pull your image

你打tag,推送docker-whalesay 镜像到你新建的repository。

当你做完之后,你从repository中pull你的新镜像来测试。

Step 1: Tag and push the image

1)查看所有的镜像:

  1. [root@bogon ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. docker-whalesay latest 7c8d26884c76 About an hour ago 182.1 MB
  4. hello-world latest c54a2cc56cbb 7 weeks ago 1.848 kB
  5. mendlik/docker-whalesay latest 552104437e78 5 months ago 172.3 MB

2)找到你自己的docker-whalesay的镜像id

这里的id是:

7c8d26884c76

注意:REPOSITORY展示的repo名字是docker-whalesay,不是命名空间,如果你想包含Docker Hub账户里的命名空间的话。

这个命名空间和你账户的名字是一样的。你需要重命名镜像为YOUR_DOCKERHUB_NAME/docker-whalesay

3)使用镜像ID和docker tag 命令来对你的docker-whalesay打标签。

改成自己的账户和信息:

docker tag 7c8d26884c76 w**/docker-whalesay:latest

4)查看所有镜像,可以看到你最新的打过tag的镜像:

  1. [root@bogon ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. w**/docker-whalesay latest 7c8d26884c76 2 hours ago 182.1 MB
  4. docker-whalesay latest 7c8d26884c76 2 hours ago 182.1 MB
  5. hello-world latest c54a2cc56cbb 7 weeks ago 1.848 kB
  6. mendlik/docker-whalesay latest 552104437e78 5 months ago 172.3 MB

5)在命令行使用docker login命令登录Docker Hub

  1. docker login --username=w** --email=***@qq.com
  2.  
  3. 输入密码,提示登陆成功:
  4.  
  5. Flag --email has been deprecated, will be removed in 1.13.
  6.  
  7. Password:
  8.  
  9. Login Succeeded

  

6)使用docker push命令把你的镜像push到你的新的repository上。

use docker push to push my images to docker hub:

  1. [root@bogon ~]# docker push w**/docker-whalesay
  2. The push refers to a repository [docker.io/w**/docker-whalesay]
  3. 4e41c767b863: Layer already exists
  4. c156acae0765: Layer already exists
  5. 51c7a9fb7da1: Layer already exists
  6. 887b1e17f589: Layer already exists
  7. 284cb67cd312: Layer already exists
  8. 5f70bf18a086: Layer already exists
  9. 917c0fc99b35: Layer already exists
  10. latest: digest: sha256:53bb6ecb63346c7916e7f68d942d754964fc176732ce93322cea2ef25a7b28af size: 1779

7)回到Docker Hub上然后看看你的新镜像

https://hub.docker.com/r/w**/

Step 2: Pull your new image

  1. [root@bogon ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. docker-whalesay latest 7c8d26884c76 18 hours ago 182.1 MB
  4. w**/docker-whalesay latest 7c8d26884c76 18 hours ago 182.1 MB
  5. hello-world latest c54a2cc56cbb 7 weeks ago 1.848 kB
  6. mendlik/docker-whalesay latest 552104437e78 5 months ago 172.3 MB

为了做测试,我们从本地删除w**/docker-whalesay和docker-whalesay 镜像,然后从你的repository上docker pull镜像。

  1. [root@bogon ~]# docker rmi -f 7c8d26884c76
  2. [root@bogon ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. hello-world latest c54a2cc56cbb 7 weeks ago 1.848 kB
  5. mendlik/docker-whalesay latest 552104437e78 5 months ago 172.3 MB

现在你本地没有这个镜像了,我们下载它

[root@bogon ~]# docker run w**/docker-whalesay

  1. Unable to find image 'w**/docker-whalesay:latest' locally
  2. latest: Pulling from w**/docker-whalesay
  3.  
  4. fdd5d7827f33: Already exists
  5. a3ed95caeb02: Already exists
  6. 7aff21647840: Already exists
  7. 6e3940ed972c: Already exists
  8. d50bea27de75: Already exists
  9. 47ce71aadbde: Already exists
  10. Digest: sha256:53bb6ecb63346c7916e7f68d942d754964fc176732ce93322cea2ef25a7b28af
  11. Status: Downloaded newer image for w**/docker-whalesay:latest
  12. ____________________________________
  13. / /bin/sh -c /usr/games/fortune -a | \
  14. \ cowsay /
  15. ------------------------------------
  16. \
  17. \
  18. \
  19. ## .
  20. ## ## ## ==
  21. ## ## ## ## ===
  22. /""""""""""""""""___/ ===
  23. ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
  24. \______ o __/
  25. \ \ __/
  26. \____\______/

六、docker常用命令

6.1 镜像

 

命令

描述

inspect 镜像ID/标签

查看,镜像信息。

pull 标签

获取,镜像。

images

查看,所有镜像。

search 标签

搜索,镜像。

默认搜索Docker Hub官网里的镜像

--automated=false仅显示自动创建的镜像(automated资源则允许用户验证镜像的来源和内容)

--no-trunc=false 输出信息不截断显示

--filter=stars=0   指定仅显示评价为星级以上的镜像。

rmi 镜像ID/标签

删除,镜像。

rmi 标签

当同一个镜像有多个标签的时候,docker rmi标签名:只是删除了该镜像多个标签中的指定标签而已,

并不影响镜像文件。

但是当镜像只剩下一个标签的时候就要小心了,此时再使用“docker rmi 标签”会彻底删除该镜像

rmi 镜像ID

会删除镜像本身

注意:当该镜像创建的容器存在时,镜像文件默认无法被删除。

不推荐用-f,因被强制删除的镜像会换了新的ID继续存在系统中。

正确的做法:先删除依赖该镜像的所有容器,再来删除镜像。

docker rm e81  # 删除容器e81266565

docker rmi 8sdgw23sdwe   # 删除镜像

docker images  # 查看镜像

commit -m "Added a new file" -a "wangqiaomei" 容器ID 镜像标签名

创建新镜像:基于已有镜像的容器创建一个新的镜像。

-a, --author="" 作者信息。

-m, --message="" 提交信息。

-p, --pause=true 提交时暂停容器运行

1)首先,启动一个镜像,并在其中进行修改操作,例如创建一个test文件,之后推出:

$ docker run -it --entrypoint /bin/bash mendlik/docker-whalesa

root@146ec4ca5971:/# touch test.txt

root@146ec4ca5971:/# exit

记住容器的ID为:146ec4ca5971

2)此时该容器跟原mendlik/docker-whalesa镜像相比,已经发生了改变,可以使用docker commit命令来提交为一个新的镜像。

提交时可以使用ID或名称来指定容器

[root@bogon ~]# docker commit -m "Added a new file" -a "wangqiaomei" 146ec4ca5971 test.txt

返回新镜像的ID:

sha256:6968da90af9a141e1fc326535a3f9f47efa0ca7a27232da96372609bed15e279

查看新的镜像已经存在了:

[root@bogon ~]# docker images

REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE

test.txt                  latest              6968da90af9a        39 seconds ago      172.3 MB

import

创建新镜像,基于本地模板导入

推荐使用OpenVZ提供的模板来创建。OPENVZ模板下载地址:

说明:ubuntu:14.04 注意:ubuntu是repository名,14.04是tag名

sudo cat ubuntu-14.04-x86-minimal.tar.gz|docker import - ubuntu:14.04

返回镜像ID:

sha256:5fcb7c50afe7f3985f1d94c8c2b30a1c89ba00b52fce3a295bc4905e27d41d0e

[root@bogon tools]# docker images

REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE

ubuntu                    14.04               5fcb7c50afe7        5 minutes ago       205.2 MB

save

存出镜像,为一个tar文件。

存出本地的ubuntu:14.04镜像为文件 ubuntu_14.04.tar

docker images

REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE

ubuntu                    14.04               5fcb7c50afe7        5 minutes ago       205.2 MB

docker save -o ubuntu_14.04.tar ubuntu:14.04

load

载入镜像

从本地文件ubuntu_14.04.tar导入到本地镜像库

docker load --input ubuntu_14.04.tar

docker load < ubuntu_14.04.tar

这将导入镜像和元数据信息。

docker images查看

push

上传镜像

docker push NAME[:TAG]

默认上传到DockerHub官方仓库

docker tag 7c8d26884c76 wqm71/docker-whalesay:latest

docker push wqm71/docker-whalesay

然后提示你输入:

Username:

Password:

Email

docker1-安装和使用的更多相关文章

  1. docker flannel网络部署和路由走向分析

    1.flannel介绍 flannel是coreos开发的容器网络解决方案.flannel为每个host分配一个subnet,容器从此subnet中分配ip.这些ip可以在host间路由,容器间无需n ...

  2. Centos7的安装、Docker1.12.3的安装,以及Docker Swarm集群的简单实例

    目录 [TOC] 1.环境准备 ​ 本文中的案例会有四台机器,他们的Host和IP地址如下 c1 -> 10.0.0.31 c2 -> 10.0.0.32 c3 -> 10.0.0. ...

  3. Centos6.7安装docker1.7.1

    Docker当前发布的最新版本已经到了1.11,其官网上针对Centos的的安装需求如下: Docker requires a -bit installation regardless of your ...

  4. docker1.12 安装pxc(Percona XtraDB Cluster )测试

    docker1.12 安装pxc(Percona XtraDB Cluster )测试

  5. docker1.12 安装redis第三方集群方案 codis

    docker1.12 安装redis第三方集群方案 codis

  6. docker1.13.1的安装与卸载及mysql5.5安装实例

    docker中国官方地址:https://www.docker-cn.com/ 您可以使用以下命令直接从该镜像加速地址进行拉取: $ docker pull registry.docker-cn.co ...

  7. 【k8s】centos上安装kubernetes,报错Error:docker-ce-cli conflicts with 2:docker-1.13.1-94.gitb2f74b2.el7.centos.x86_64

    使用命令: yum install kubernetes 报错: Error: docker-ce-cli conflicts with :docker--.git07f3374.el7.centos ...

  8. CentOS7.x安装Docker1.11.1

    原文发表于cu:2016-05-30 本文属于重发,当前Docker已经分为EE与CE版本,CE版本是17.06.0-ce,最新的CE版本安装方式已略有不同:但可以指定安装版本,如1.11.1,1.1 ...

  9. [转载] Centos7的安装、Docker1.12.3的安装,以及Docker Swarm集群的简单实例

    1.环境准备 ​ 本文中的案例会有四台机器,他们的Host和IP地址如下 c1 -> 10.0.0.31 c2 -> 10.0.0.32 c3 -> 10.0.0.33 c4 -&g ...

  10. Ubuntu16.04安装Docker1.12+开发实例+hello world+web应用容器

    本次主要是详细记录Docker1.12在Ubuntu16.04上的安装过程,创建Docker组(避免每次敲命令都需要sudo),Docker常用的基本命令的总结,在容器中运行Hello world,以 ...

随机推荐

  1. [javascript]IIFE立即执行的函数表达式

    近况:最近一直忙着找实习没有更新,不过学习还是在继续的.最近在写Node.js又稍带把javascript的角落知识捡了一遍,过半个月打算去看看python和一些CSS深层的书和博客.工作找的还好,拿 ...

  2. ipad协议7.0,与大佬们分享几套新老版本的协议源码及算法,交流心得。

  3. 网易云安全DDoS高防全新上线 ,游戏防护实力领先

    本文由  网易云发布.       10月24日,网易云安全(易盾)正式上线DDoS高防解决方案[点击查看].基于网易20年网络安全防护经验,网易云安全(易盾)DDoS高防可提供1T超大防护带宽,拥有 ...

  4. 网络流——二分图最优匹配KM算法

    前言 其实这个东西只是为了把网络流的内容凑齐而写的(反正我是没有看到过这样子的题不知道田忌赛马算不算) 算法过程 我们令左边的点(其实二分图没有什么左右)为女生,右边的点为男生,那么: 为每一个女生定 ...

  5. git 常用命令(不定期更新)

    过程写写吧,总是忘记.1,在一个文件夹下 键入 git init ,使之成为Git可以管理的仓库.2,编写一个文件readme.txt.3,把文件添加到仓库 git add readme.txt4,把 ...

  6. Flask从入门到精通之Jinja2模板引擎

    我们使用一个简单的例子切入到Jinja2模板引擎,形式最简单的Jinja2模板引擎就是一个包含响应文本的文件,实例如下: <h1>Hello World!</h1> 最简单的包 ...

  7. 一步一步教你使用 LSMW 批量处理数据

    保存退出 输入完后,保存退出

  8. 多线程学习:Volatile与Synchronized的区别、什么是重排序

    java线程的内存模型 java的线程内存模型中定义了每个线程都有一份自己的共享变量副本(本地内存),里面存放自己私有的数据,其他线程不能直接访问,而一些共享变量则存在主内存中,供所有线程访问. 上图 ...

  9. 【BZOJ3143】【HNOI2013】游走 高斯消元

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3143 我们令$P_i$表示从第i号点出发的期望次数.则$P_n$显然为$0$. 对于$P ...

  10. POJ 1154

    #include<iostream> #include<stdio.h> #define MAXN 20 using namespace std; int DFS(int i, ...