初识docker并安装

Docker简介

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。Docker 容器通过 Docker 镜像来创建。容器与镜像的关系类似于面向对象编程中的对象与类。官网地址:http://www.docker.com/

下面是docker官网提供的kvm与docker的图片

这里写图片描述

centos

1、系统版本

[root@jenkins ~]# cat /etc/redhat-release 

2、安装epel

[root@jenkins ~]# rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-10.noarch.rpm

3.安装docker

[root@jenkins ~]# yum -y install docker-io
[root@jenkins ~]# systemctl start docker 启动docker
[root@jenkins ~]# systemctl enable docker 开机自启

4、docker的一些操作

docker pull centos:latest #从docker.io中下载centos镜像到本地 /var/lib/docker/graph

docker images                                  #查看已下载的镜像

docker run -i -t centos /bin/bash              #启动一个容器

docker imr image_id                            #删除镜像

docker rmi $(docker images | grep none | awk '{print $3}' | sort -r)  #删除所有镜像

docker ps -a                                  #查看所有容器(包括正在运行和已停止的)

docker start container                        #开启一个容器(注意container_id和image_id是完全不一样de)

docker logs <容器名orID> 2>&1 | grep '^User: ' | tail -n1 #查看容器的root用户密码,因为docker容器启动时的root用户的密码是随机分配的。所以,通过这种方式就可以得到redmine容器的root用户的密码了

docker logs -f <容器名orID>                    #查看容器日志

docker rm $(docker ps -a -q)                    #删除所有容&删除单个容器docker rm <容器名orID>

docker run --name redmine -p 9003:80 -p 9023:22 -d -v /var/redmine/files:/redmine/files -v    /var/redmine/mysql:/var/lib/mysql sameersbn/redmine

#运行一个新容器,同时为它命名、端口映射、文件夹映射。以redmine镜像为例

docker run -i -t --name sonar -d -link mmysql:db  tpires/sonar-server

 # 一个容器连接到另一个容器&sonar容器连接到mmysql容器,并将mmysql容器重命名为db。这样,sonar容器就可以使用db的相关的环境变量了。

#当需要把一台机器上的镜像迁移到另一台机器的时候,需要保存镜像与加载镜像。

机器a

docker save busybox-1 > /home/save.tar

使用scp将save.tar拷到机器b上,然后:

docker load < /home/save.tar

docker build -t <镜像名> <Dockerfile路径>        #构建自己的镜像

ubuntu

1、查看版本

root@controller:~# uname -a
Linux controller 4.4.0-62-generic #83-Ubuntu SMP Wed Jan 18 14:10:15 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
root@controller:~# cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.2 LTS"

我所使用的是Ubuntu 16.04.2 64位版本和4.4的内核

2、安装docker

root@controller:~# apt install  -y docker.io
root@controller:~# systemctl start docker
root@controller:~# systemctl enable docker

3、docker版本

root@controller:~# docker version
Client:
 Version:      1.12.6
 API version:  1.24
 Go version:   go1.6.2
 Git commit:   78d1802
 Built:        Tue Jan 31 23:35:14 2017
 OS/Arch:      linux/amd64

Server:
 Version:      1.12.6
 API version:  1.24
 Go version:   go1.6.2
 Git commit:   78d1802
 Built:        Tue Jan 31 23:35:14 2017
 OS/Arch:      linux/amd64

现在,docker 已经安装在您的系统上。您可以从 Docker 库先下载 Docker Image 制作的容器

Docker 的基本用法

要创建一个新的容器,你应该选择一个基本 image 的操作系统,例如启动 Ubuntu 或者 CentOS 或其他系统。您可以搜索一个基本 image 使用 Docker 搜索命令:

1、docker search ubuntu

该命令将显示所有 ubuntu images,你可以自己尝试一下搜索 centos Images。

root@controller:~# docker search ubuntu
NAME                       DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                     Ubuntu is a Debian-based Linux operating s...   6356      [OK]       
rastasheep/ubuntu-sshd     Dockerized SSH service, built on top of of...   95                   [OK]
ubuntu-upstart             Upstart is an event-based replacement for ...   76        [OK]       
neurodebian                NeuroDebian provides neuroscience research...   37        [OK]       
ubuntu-debootstrap         debootstrap --variant=minbase --components...   30        [OK]       
32bit/ubuntu               Ubuntu for i386 (32bit)                         30                   
armhf/ubuntu               Ubuntu is a Debian-based Linux operating s...   27                   
nuagebec/ubuntu            Simple always updated Ubuntu docker images...   22                   [OK]
tutum/ubuntu               Simple Ubuntu docker images with SSH access     18                   
ppc64le/ubuntu             Ubuntu is a Debian-based Linux operating s...   10                   
aarch64/ubuntu             Ubuntu is a Debian-based Linux operating s...   9                    
sameersbn/ubuntu                                                           9                    [OK]
nimmis/ubuntu              This is a docker images different LTS vers...   7                    [OK]
i386/ubuntu                Ubuntu is a Debian-based Linux operating s...   7                    
darksheer/ubuntu           Base Ubuntu Image -- Updated hourly             3                    [OK]
libmesos/ubuntu                                                            3                    
scaleway/ubuntu            Ubuntu image on Scaleway                        2                    
s390x/ubuntu               Ubuntu is a Debian-based Linux operating s...   1                    
vcatechnology/ubuntu       A Ubuntu image that is updated daily            1                    [OK]
webhippie/ubuntu           Docker images for ubuntu                        1                    [OK]
konstruktoid/ubuntu        Ubuntu base image                               0                    [OK]
ossobv/ubuntu              Custom ubuntu image from scratch (based on...   0                    
smartentry/ubuntu          ubuntu with smartentry                          0                    [OK]
defensative/socat-ubuntu                                                   0                    [OK]
pivotaldata/ubuntu         A quick freshening-up of the base Ubuntu d...   0   

2、现在我们现在 base image到我们的服务中,使用命令:

root@controller:~# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
e0a742c2abfd: Pull complete 
486cb8339a27: Pull complete 
dc6f0d824617: Pull complete 
4f7a5649a30e: Pull complete 
672363445ad2: Pull complete 
Digest: sha256:84c334414e2bfdcae99509a6add166bbb4fa4041dc3fa6af08046a66fed3005f
Status: Downloaded newer image for ubuntu:latest

3、现在,您可以通过使用命令来查看所有已下载的images:

root@controller:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              14f60031763d        13 days ago         119.5 MB

4、Ubuntu 镜像从DockerHub/Docker Registry下载。下一步骤是创建从该镜像的容器。

要创建容器,可以使用docker create 或 docker run

docker create ubuntu

docker create 命令会创建一个新的容器,但不会启动它。所以现在你需要使用运行命令:

docker run -i -t ubuntu /bin/bash

此命令将创建并运行一个基于 Ubuntu 镜像的容器,容器内并运行一个命令/bin/bash,您将在容器内自动运行命令。

root@controller:~# docker create ubuntu
0cf6c594de0ccb0567d305d5f6ce5d4f1ad59778534e3d37ccab52e47af03b2f
root@controller:~# docker run -i -t ubuntu /bin/bash
root@2d57a682a56a:/# 
root@2d57a682a56a:/# exit
exit

当你输入 Exit 命令退出容器时,容器也是停止运行,如果你想容器在后台运行需要在命令后面添加 -d 参数。

docker run -i -t -d ubuntu /bin/sh -c “while true; do echo hello world; sleep 1; done”

/bin/sh -c “while true; do echo hello world; sleep 1; done” this is bash script to echo “hello word” forever.

现在你可以看到容器在后台运行通过命令:

docker ps

如果你想从 bash 命令看日志结果,使用命令:

docker logs NAMES/ContainerID

root@controller:~# docker run -i -t -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
7991f44c7117260c2eb34c64b2a4ce6d5896ea951685b674c2e1fe48fe22fc01
root@controller:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
7991f44c7117        ubuntu              "/bin/sh -c 'while tr"   10 seconds ago      Up 10 seconds                           serene_liskov
root@controller:~# docker logs serene_liskov
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world

怎样在后台访问容器 shell?这个命令将会连接你的容器 shell:

docker exec -i -t NAMES/ContainerID

root@controller:~# docker exec -i -t serene_liskov /bin/bash
root@7991f44c7117:/# 

你可以看到主机名和容器ID是相等的,这意味着你在容器shell内。当你在shell 上键入’exit`,会离开的shell,但容器仍在运行。

你会经常使用的另一个命令是:

docker stop NAME/ContainerID

这将停止容器而不将其删除,这样你就可以用命令重新启动它:

docker start NAME/ContainerID

如果你想删除的容器,先停止它,然后用命令将其删除:

docker rm NAME/ContainerID

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值