Hello Docker

什么是Docker

What is Docker?
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.

上面是opensource.com对docker的解释.总结一下:docker是一个完整的应用运行容器,而且用起来很方便.

安装

以mac os为例,直接到docker官网下载安装包即可.对于初学者我不太推荐Kitematic(一个管理docker的可视化工具).就好比学习git,再强大的可视化工具也干不过原生的命令行.所以最好就是直接使用docker提供的原生命令行工具.
安装完成后,最重要的事情就是设置一下镜像,否则速度真是没法忍受.我自己使用的是阿里云的docker加速器.注册完成后,会有一个属于个人的加速器地址https://xxx.mirror.aliyuncs.com,把这个地址加入到docker的mirror列表中就行了.

试试看

我们先装个nginx试试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 安装nginx
docker pull nginx

# 查看已经安装的image
docker images

# 启动,名称是hello-nginx,将80端口映射到宿主机的8080
docker run --name hello-nginx -d -p 8000:80 nginx

# 进入命令行, 02697b7bf225为container id,可以用docker ps查看
docker exec -it 02697b7bf225 bash


# 如果是ubuntu的image
docker run --name myubuntu -it -d ubuntu

# 和宿主机的文件拷贝
docker cp <CONTAINER_ID>:SRC_PATH DEST_PATH
docker cp SRC_PATH <CONTAINER_ID>:DEST_PATH

# 挂载宿主机的目录到容器中
docker run -itd --name myubuntu -v /Users/charles/Desktop/mydata:/mydata ubuntu

启动后就能在宿主机的浏览器中直接访问http://localhost:8000就能看到nginx的欢迎页了.接着进入容器的命令行之后就跟普通的linux没什么区别了,可以看到系统是debian的8.0版本.

1
2
3
cat /etc/issue

Debian GNU/Linux 8 \n \l

接着我们在容器里装个vim试试,把apt的源换成阿里云的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 更换成aliyun的源
echo -e "\
deb http://mirrors.aliyun.com/debian/ jessie main non-free contrib\n\
deb http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib\n\
deb-src http://mirrors.aliyun.com/debian/ jessie main non-free contrib\n\
deb-src http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib\
" >/etc/apt/sources.list

# 当然也可以直接从宿主机cp过来
docker cp sources.list [containerID]:/etc/apt/sources.list

# 安装vim
apt-get update
apt-get install vim

是不是很爽,一切命令都不用sudo,直接root执行,而且不怕搞出问题,因为随时都可以再run一个起来.

Dockerfile

Dockerfile用来描述一个image,写过java的人可以把Dockerfile类比成maven工程的pom.xml文件.我们写个最简单的Dockerfile试试

1
2
3
4
5
6
7
8
9
10
11
FROM nginx
MAINTAINER charles
RUN echo "\
deb http://mirrors.aliyun.com/debian/ jessie main non-free contrib\n\
deb http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib\n\
deb-src http://mirrors.aliyun.com/debian/ jessie main non-free contrib\n\
deb-src http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib\
" >/etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y vim
RUN echo "alias ll='ls -lh'" >> /root/.bashrc

写法很简单,基本上跟上面的例子差不多,有点小区别就是在Dockerfile里面的echo不需要加-e了.接着用docker build编译一个image出来

1
docker build -t "charles/mynginx:v1" ./

接着就能用docker images查看到刚刚打出来的镜像了.还能把刚才打出来的镜像直接push到阿里云的私人仓库中.基本的使用差不多就这样了,更多的玩法就你自己去探索吧!
ps: 我现在的电脑里是不装mysql,mongo这些东西了,直接用docker代替了,挺好.还有你看到这个blog也是运行在docker中的,😄