怎么安装docker+HelloWorld?

安装Docker
(提示:对Docker感兴趣的朋友可以加我的微信ghostcloud2016,然后我把你加到我们的一个Docker爱好者社群里面。)

Docker最核心的程序是Docker Engine,根据参数,它既可以作为后台daemon运行,也可以作为命令行工具使用。

1 Linux上安装
Docker本身是基于linux内核的,因此只要内核版本足够新,一般大于3.10左右,就可以运行。各种系统的安装方式基本相同,只是针对各系统的进程管理或者目录结构略有不同。下面是docker在linux系统下的物理逻辑结构: alt text

2 Ubuntu上安装Docker
在所有系统中,Docker对ubuntu的支持是最好的,因此如果使用docker,我推荐都适用ubuntu系统。Ubuntu系统的可视化、安装以及内核的更新,我认为都是最好的。Docker支持的Ubuntu系统:

Ubuntu Wily 15.10
Ubuntu Vivid 15.04
Ubuntu Trusty 14.04 (LTS)
Ubuntu Precise 12.04 (LTS) 环境要求:
64位系统
3.10以上linux内核,具体可以根据uname –r进行查看
具体安装步骤:

更新apt源

添加gpg key

sudo apt-key adv –keyserver hkp://p80.pool.sks-keyservers.net:80 –recv-keys 58118E89F3A912897C070ADBF76221572C52609D

增加/etc/apt/sources.list.d/docker.list

增加下面一行(选择你的具体版本)

Ubuntu precise 12.04: deb https://apt.dockerproject.org/repo ubuntu-precise main
Ubuntu trustry 14.04: deb https://apt.dockerproject.org/repo ubuntu-trusty main
Ubuntu trustry 15.04: deb https://apt.dockerproject.org/repo ubuntu-vivid main
Ubuntu trustry 15.10: deb https://apt.dockerproject.org/repo ubuntu-wily main

执行apt-get update,更新源

删除旧版本Docker
apt-get purge lxc-docker
检查Docker的apt源是否正常
apt-cache policy docker-engine
不同版本的处理

Ubuntu Wily 15.10, Ubuntu Vivid 15.04, Ubuntu Trusty 14.04 (LTS)

s u d o a p t g e t i n s t a l l l i n u x i m a g e e x t r a (uname -r)
linux-image-extra内核包安装后,可以使用aufs

Ubuntu Precise 12.04(LTS)

对于这个版本的ubuntu,需要3.13内核及一些相关的包,执行下面的命令进行安装:
s u d o a p t g e t i n s t a l l l i n u x i m a g e g e n e r i c l t s t r u s t y ( ) r e b o o t n f d o c k e r e n g i n e sudo apt-get update
s u d o a p t g e t i n s t a l l d o c k e r e n g i n e sudo service docker start
$ sudo docker run hello-world #验证是否安装成功
其他配置

创建docker用户

docker daemon默认绑定在unix socket上,而不是tcp的端口。而unix socket默认是属于root用户,所以其他用户要访问该 socket需要通过sudo命令。如果想避免使用sudo,你需要将当前用户添加到docker 用户组。具体步骤:

$ sudo usermod -aG docker ubuntu

退出当前用户,并登入当前用户

$ docker run hello-world#验证是否成功

如果失败会报错:

Cannot connect to the Docker daemon. Is ‘docker daemon’ running on this host?
调整Memory和Swap的配额

有时候你在启动时会出现:

WARNING: Your kernel does not support cgroup swap limit. WARNING: Your
kernel does not support swap limit capabilities. Limitation discarded.
这个提示说明,你当前的内核不支持cgroup对swap的控制。如果需要支持,会使用大约1%的系统总内存,同时会有10%左右的性能损失。具体步骤如下:

以root用户编辑/etc/default/grub

添加GRUB_CMDLINE_LINUX=”cgroup_enable=memory swapaccount=1”

保存文件,并执行sudo update-grub

sync

reboot系统

同UFW一起使用

如果你使用了UFW(Uncomplicated Firewall),你需要做额外的配置,因为UFW默认会屏蔽所有的Forwarding流量,所以你需要zuo如下配置:

$ sudo ufw status

$ sudo vi /etc/default/ufw
DEFAULT_FORWARD_POLICY=”ACCEPT”

s u d o u f w r e l o a d sudo ufw allow 2375/tcp
配置docker的DNS

Ubuntu系统默认使用127.0.0.1作为DNS服务器,具体配置文件为/etc/resolv.conf。同时,还使用dnsmasq作为DNS的缓存。当你启动容器的时候,可能会遇到下面的警告:

WARNING: Local (127.0.0.1) DNS resolver found in resolv.conf and containers
can’t use it. Using default external servers : [8.8.8.8 8.8.4.4]
这个警告说明Docker容器不能使用主机的本地DNS,因此使用外部的默认DNS(8.8.8.8,8.8.4.4)这两个地址都是google的免费DNS地址。如果想自己指定 DNS,具体步骤如下:

s u d o v i / e t c / d e f a u l t / d o c k e r D O C K E R O P T S = d n s 8.8.8.8 192.168.1.1 sudo restart docker

或者修改dnsmasq

sudo vi /etc/NetworkManager/NetworkManager.conf

注释掉这一行

dns=dnsmasq

重启网络

$ sudo restart network-manager

重启docker

$ sudo restart docker
让Docker随系统启动

首先需要了解你所使用的系统用的什么启动管理工具,在ubuntu 15.04及其以上版本使用的是systemd,在14.10及其以下使用的是upstart。在15.04及以上版本需要执行:

$ sudo systemctl enable docker
在14.10以下,安装程序会自动将docker加入到upstart中。

升级docker
$ apt-get upgrade docker-engine
卸载docker

s u d o a p t g e t p u r g e d o c k e r e n g i n e sudo apt-get autoremove –purge docker-engine

删除 docker的镜像及工作目录

$ rm -rf /var/lib/docker
3 CentOS上安装 docker
3.1 环境要求
CentOS 7.X及其以后版本
3.10以后版本
3.2 通过脚本安装
更新yum源:

$ sudo yum update
下载脚本并安装:

$ curl -sSL https://get.docker.com/ | sh
启动服务:

$ sudo service docker start
验证docker:

$ sudo docker run hello-world
3.3 通过yum安装
更新源:

$ sudo yum update
添加源及gpg key:

$ sudo tee /etc/yum.repos.d/docker.repo
<<-‘EOF’
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF
安装docker-engine:

$ sudo yum install docker-engine
启动docker daemon:

$ sudo service docker start
验证docker:

s u d o d o c k e r r u n h e l l o w o r l d 3.4 d o c k e r u b u n t u sudo usermod -aG docker your_username
3.5 随系统启动
$ sudo chkconfig docker on`
3.6 卸载

检查是否安装

$ 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

删除包

$ sudo yum -y remove docker-engine.x86_64

删除docker镜像及中间文件

$ rm -rf /var/lib/docker
4 Mac上安装
背景

Mac OSX下Docker通过Docker Toolbox来进行安装,Docker Toolbox是Docker公司的一个工具包,里面包括:

Docker Machine: 运行docker-machine程序
Docker Engine: Docker可执行程序
Docker Compose: docker-compose程序
Kitematic: Docker GUI
包含预定义的shell脚本,用于命令行运行环境
Oracle VM VirtualBox
由于Docker Daemon 使用了linux kernel的众多特性,我们不能在OS X本地运行Docker,所以我们必须使用docker-machine来创建并关联到一个虚拟机。这个虚拟机就是Docker的实际运行环境。下面是docker在mac或windows下的物理逻辑结构 alt text

环境要求: OS X 10.8及其以上版本

安装Toolbox

地址是https://www.docker.com/toolbox, 下载完成后根据提示进行安装。默认情况,Docker Toolbox安装在/usr/local/bin,每个用户都可以使用这些可执行程序,同时会安装带有Docker运行环境的Virtual Box。

启动Docker Machine 通过Launchpad启动程序:
执行程序后,会自动执行一段shell脚本启动虚拟机并进入一个shell终端。

Hello World, Docker

我们先来执行一个最简单的docker容器:

docker run hello-world
这个命令包含三部分:

docker - 所有docker命令的可执行程序,默认在/usr/local/bin/docker
run - docker 的子命令,该命令首先查看本地是否有相应的镜像,如果没有将默认从Docker Hub进行拉取,之后再运行该镜像
hello-world - 镜像名称,这个镜像就是echo一句hello world
容器本身是一个简化的经过裁减的linux系统,容器可以运行一个简单命令,就像上例一样;也可以运行比较复杂的应用程序,比如:数据库,web后台等。那hello-world镜像是由谁生成的呢?是Docker官方生成的,通过docker容器,你可以在任何系统容器中运行镜像。
查找Image

你可以在docker hub主页,或者本机通过命令行进行搜索,一般我喜欢通过命令行进行搜索。例如,搜索ubuntu:

root@gctest:~# docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating s… 2761 [OK]
ubuntu-upstart Upstart is an event-based replacement for … 48 [OK]
dorowu/ubuntu-desktop-lxde-vnc Ubuntu with openssh-server and NoVNC on po… 28 [OK]
torusware/speedus-ubuntu Always updated official Ubuntu docker imag… 25 [OK]
sequenceiq/hadoop-ubuntu An easy way to try Hadoop on Ubuntu 24 [OK]
ubuntu-debootstrap debootstrap –variant=minbase –components… 20 [OK]
tleyden5iwx/ubuntu-cuda Ubuntu 14.04 with CUDA drivers pre-installed 18 [OK]
neurodebian NeuroDebian provides neuroscience research… 15 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of… 15 [OK]
consol/ubuntu-xfce-vnc Ubuntu container with “headless” VNC sessi… 7 [OK]
n3ziniuka5/ubuntu-oracle-jdk Ubuntu with Oracle JDK. Check tags for ver… 5 [OK]
nuagebec/ubuntu Simple always updated Ubuntu docker images… 4 [OK]
ioft/armhf-ubuntu [ABR] Ubuntu Docker images for the ARMv7(a… 4 [OK]
nimmis/ubuntu This is a docker images different LTS vers… 3 [OK]
maxexcloo/ubuntu Docker base image built on Ubuntu with Sup… 2 [OK]
sylvainlasnier/ubuntu Ubuntu 15.04 root docker images with commo… 1 [OK]
densuke/ubuntu-jp-remix Ubuntu Linuxの日本語remix風味です 1 [OK]
seetheprogress/ubuntu Ubuntu image provided by seetheprogress us… 1 [OK]
rallias/ubuntu Ubuntu with the needful 0 [OK]
tvaughan/ubuntu https://github.com/tvaughan/docker-ubuntu 0 [OK]
zoni/ubuntu 0 [OK]
partlab/ubuntu Simple Ubuntu docker images. 0 [OK]
teamrock/ubuntu TeamRock’s Ubuntu image configured with AW… 0 [OK]
konstruktoid/ubuntu Ubuntu base image 0 [OK]
esycat/ubuntu Ubuntu LTS 0 [OK]
在搜索结果中,你可以看到有的是通过”/”划分的,有的没有。凡是没有”/”分级的就是Docker Hub自带的镜像;否则该镜像就出自于某个Docker Hub用户,从这里你也可以看到,Docker Hub是一个Public仓库,你的任何镜像,别人都可以搜索到。因此,一定要注意安全,不要把敏感信息放上去了。

拉取镜像

我们尝试拉取一个centos镜像:

root@gctest:~# docker pull centos
Using default tag: latest
Pulling repository docker.io/library/centos
ce20c473cd8a: Pulling dependent layers
47d44cb6f252: Pulling metadata
ce20c473cd8a: Download complete
168a69b62202: Download complete
812e9d9d677f: Download complete
4234bfdd88f8: Pulling fs layer
4234bfdd88f8: Download complete
Status: Downloaded newer image for centos:latest
root@gctest:~# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ghostcloud.cn:5000/gcali 1.5 6ff13e5ab907 29 hours ago 213.9 MB
centos latest ce20c473cd8a 7 weeks ago 172.3 MB
编译Image 除了拉取官方的Image,也可以自己编译Image,编译的方法和Makefile类似。
创建Dockerfile
root@gctest:~# mkdir myimage
root@gctest:~# cd myimage/
root@gctest:~/myimage# vi Dockerfile
root@gctest:~/myimage# cat Dockerfile
FROM centos:latest
CMD echo “Hello myimage”
编译Image
root@gctest:~/myimage# docker build -t myimage .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM centos:latest
—> ce20c473cd8a
Step 2 : CMD echo “Hello myimage”
—> Running in 1421c66fa1a6
—> 2408b71c40c0
Removing intermediate container 1421c66fa1a6
Successfully built 2408b71c40c0
root@gctest:~/myimage# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
myimage latest 2408b71c40c0 6 seconds ago 172.3 MB
ghostcloud.cn:5000/gcali 1.5 6ff13e5ab907 30 hours ago 213.9 MB
centos latest ce20c473cd8a 7 weeks ago 172.3 MB
运行自己的Image
root@gctest:~/myimage# docker run myimage
Hello myimage

猜你喜欢

转载自blog.csdn.net/gydtep/article/details/81736185