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更新时不升级内核

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

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

sudo yum --exclude=kernel* update

2)Add the yum repo.

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

3)Install the Docker package.

sudo yum -y install docker-engine

4)Start the Docker daemon.

sudo service docker start

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

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from hello-world
a8219747be10: Pull complete
91c95931e552: Already exists
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.
Digest: sha256:aa03e5d0d5553b4c3473e89c8619cf79df368babd1.7.1cf5daeb82aab55838d
Status: Downloaded newer image for hello-world:latest
Hello from Docker.
This message shows that your installation appears to be working correctly.
。。。。。。

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

[root@bogon ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
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.

sudo groupadd docker

2)Add your user to docker group.

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.

$ docker run hello-world

1.3 uninstall

1)List the package you have installed.

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

2)Remove the package.

$ sudo yum -y remove docker-engine.x86_64
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:

$ 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

$ docker run mendlik/docker-whalesay

# Print custom message

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

# Let's see what's inside the container

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

Step 2: Run the whalesay image

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

docker run mendlik/docker-whalesay cowsay boo

查看本地所有的镜像:

[root@bogon ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest c54a2cc56cbb 7 weeks ago 1.848 kB
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..

[root@bogon ~]# docker run mendlik/docker-whalesay boo-boo
_________
< boo-boo >
---------
\
\
\
## .
## ## ## ==
## ## ## ## ===
/""""""""""""""""___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\______/

三、Build your own image

Step 1: Write a Dockerfile

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

mkdir mydockerbuild
cd mydockerbuild

2)编辑文件内容

vi Dockerfile
添加内容:
FROM mendlik/docker-whalesay:latest

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

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

这行安装软件到镜像里。

Dockerfile添加内容:

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

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

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

CMD /usr/games/fortune -a | cowsay

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

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

Step 2: Build an image from your Dockerfile

注意,一定要加点“.”

docker build -t docker-whalesay .

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

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

我们看看命令执行输出:

 [root@bogon mydockerbuild]# docker build -t docker-whalesay .
Sending build context to Docker daemon 2.048 kB # docker检查他需要创建的东西。
Step : FROM mendlik/docker-whalesay:latest # docker下载mendlik/docker-whalesay镜像,因为之前已经下载,就不用下载了。
---> 552104437e78
Step : RUN apt-get -y update && apt-get install -y fortunes # 先更新apt-get包
---> Running in ecb33156fc42
Get: http://security.debian.org jessie/updates InRelease [63.1 kB]
Get: http://security.debian.org jessie/updates/main amd64 Packages [385 kB]
Ign http://httpredir.debian.org jessie InRelease
Get: http://httpredir.debian.org jessie-updates InRelease [142 kB]
Get: http://httpredir.debian.org jessie Release.gpg [2373 B]
Get: http://httpredir.debian.org jessie-updates/main amd64 Packages [17.6 kB]
Get: http://httpredir.debian.org jessie Release [148 kB]
Get: http://httpredir.debian.org jessie/main amd64 Packages [9032 kB]
Fetched kB in 4min 47s (34.0 kB/s)
Reading package lists...
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
Reading package lists...
Building dependency tree...
Reading state information...
fortunes is already the newest version.
upgraded, newly installed, to remove and not upgraded.
---> e78b54d7f981
Removing intermediate container ecb33156fc42 # docker再安装fortunes程序
Step : CMD /usr/games/fortune -a | cowsay # docker 完成创建镜像,并打印输出。
---> Running in 5ca72e7209e3
---> 7c8d26884c76
Removing intermediate container 5ca72e7209e3
Successfully built 7c8d26884c76

docker build -t docker-whalesay .

Step 3: Run your new docker-whale

查看本地有哪些镜像:

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

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

 [root@bogon ~]# docker run docker-whalesay
____________________________________
/ /bin/sh -c /usr/games/fortune -a | \
\ cowsay /
------------------------------------
\
\
\
## .
## ## ## ==
## ## ## ## ===
/""""""""""""""""___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\______/

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)查看所有的镜像:

[root@bogon ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-whalesay latest 7c8d26884c76 About an hour ago 182.1 MB
hello-world latest c54a2cc56cbb 7 weeks ago 1.848 kB
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的镜像:

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

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

docker login --username=w** --email=***@qq.com

输入密码,提示登陆成功:

Flag --email has been deprecated, will be removed in 1.13.

Password:

Login Succeeded

  

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

use docker push to push my images to docker hub:

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

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

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

Step 2: Pull your new image

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

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

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

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

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

Unable to find image 'w**/docker-whalesay:latest' locally
latest: Pulling from w**/docker-whalesay fdd5d7827f33: Already exists
a3ed95caeb02: Already exists
7aff21647840: Already exists
6e3940ed972c: Already exists
d50bea27de75: Already exists
47ce71aadbde: Already exists
Digest: sha256:53bb6ecb63346c7916e7f68d942d754964fc176732ce93322cea2ef25a7b28af
Status: Downloaded newer image for w**/docker-whalesay:latest
____________________________________
/ /bin/sh -c /usr/games/fortune -a | \
\ cowsay /
------------------------------------
\
\
\
## .
## ## ## ==
## ## ## ## ===
/""""""""""""""""___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\______/

六、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. C#多线程编程系列(五)- 使用任务并行库

    目录 1.1 简介 1.2 创建任务 1.3 使用任务执行基本的操作 1.4 组合任务 1.5 将APM模式转换为任务 1.6 将EAP模式转换为任务 1.7 实现取消选项 1.8 处理任务中的异常 ...

  2. Docker容器的原理与实践 (下)

    欢迎访问网易云社区,了解更多网易技术产品运营经验. Docker原理分析 Docker架构 镜像原理 镜像是一个只读的容器模板,含有启动docker容器所需的文件系统结构及内容Docker以镜像和在镜 ...

  3. 【转】Lucene工作原理——反向索引

    原文链接:  http://my.oschina.net/wangfree/blog/77045 倒排索引 倒排索引(反向索引) 倒排索引源于实际应用中需要根据属性的值来查找记录.这种索引表中的每一项 ...

  4. Mysql 练习题一

    库操作: 1. 创建 数据库  create database db1; 2. 使用数据库 use db1 3. 查看表  show tables; 4. 删除  drop database db1  ...

  5. python web开发——c2 flask框架和flask_script

    重定向/error 通过flask中的redirect方法和自定义的newpath函数.redirect_demo函数实现重定向: #coding:utf-8 from flask import Fl ...

  6. A Tool To Plot Mathematical Function

    Plot.cs using Microsoft.ClearScript; using Microsoft.ClearScript.V8; using Microsoft.Win32; using Sy ...

  7. Dynamic Programming-650. 2 Keys Keyboard

    Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...

  8. linux下oracle数据库字符集修改

    linux下oracle数据库字符集修改 0.RHEL6.7.oracle11gr2 1.登录oracle.在安装oracle的用户下进入数据库. $ sqlplus / as sysdba 2.查询 ...

  9. Java并发(一)-了解线程安全

    线程不安全性 先来举例说明线程不安全是什么情况下发生的:例如一个变量可以被多个线程进行访问,那么在大量线程并发访问这个变量的情况下,线程执行的顺序会给最后的结果带来不可预估的错误. 先定义一个单例类S ...

  10. Storm系列二: Storm拓扑设计

    Storm系列二: Storm拓扑设计 在本篇中,我们就来根据一个案例,看看如何去设计一个拓扑, 如何分解问题以适应Storm架构,同时对Storm拓扑内部的并行机制会有一个基本的了解. 本章代码都在 ...