Docker 使用 Google 公司推出的 Go 语言 进行开发实现,基于 Linux 内核的 cgroup,namespace,以及 AUFS 类的 Union FS 等技术,对进程进行封装隔离,属于操作系统层面的虚拟化技术。由于隔离的进程独立于宿主和其它的隔离的进程,因此也称其为容器。最初实现是基于 LXC,从 0.7 以后开始去除 LXC,转而使用自行开发的 libcontainer,从 1.11 开始,则进一步演进为使用 runC 和 containerd。
Docker 在容器的基础上,进行了进一步的封装,从文件系统、网络互联到进程隔离等等,极大的简化了容器的创建和维护。使得 Docker 技术比虚拟机技术更为轻便、快捷。
传统虚拟机技术是虚拟出一套硬件后,在其上运行一个完整操作系统,在该系统上再运行所需应用进程;而容器内的应用进程直接运行于宿主的内核,容器内没有自己的内核,而且也没有进行硬件虚拟。因此容器要比传统虚拟机更为轻便。
docker daocloud加速器:
- linux:
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://311e425f.m.daocloud.io,该脚本可以将 --registry-mirror 加入到你的 Docker 配置文件 /etc/docker/daemon.json 中。适用于 Ubuntu14.04、Debian、CentOS6 、CentOS7、Fedora、Arch Linux、openSUSE Leap 42.1,其他版本可能有细微不同- Mac:右键点击桌面顶栏的 docker 图标,选择 Preferences ,在 Daemon 标签(Docker 17.03 之前版本为 Advanced 标签)下的 Registry mirrors 列表中加入下面的镜像地址:
http://311e425f.m.daocloud.io,点击 Apply & Restart 按钮使设置生效。- Windows: 在桌面右下角状态栏中右键 docker 图标,修改在 Docker Daemon 标签页中的 json ,把下面的地址:
http://311e425f.m.daocloud.io,加到"registry-mirrors"的数组里。点击 Apply 。
官方mac的安装步骤
下载docker.dmg文件,然后点击,一步步操作
检测Docker Engine, Docker Compose, 和Docker Machine的版本:
docker version Client: Version: 17.03.1-ce API version: 1.27 Go version: go1.7.5 Git commit: c6d412e Built: Tue Mar 28 00:40:02 2017 OS/Arch: darwin/amd64 Server: Version: 17.03.1-ce API version: 1.27 (minimum version 1.12) Go version: go1.7.5 Git commit: c6d412e Built: Fri Mar 24 00:00:50 2017 OS/Arch: linux/amd64 Experimental: true docker-compose --version docker-compose version 1.11.2, build dfed245 docker-machine --version docker-machine version 0.10.0, build 76ed2a6 拉取hello-world镜像
docker pull hello-world 查看hello-world镜像信息:
docker images hello-world REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 1815c82652c0 5 days ago 1.84 kB 根据镜像生成对应容器
docker run hello-world ps不加参数只会把当前运行的Community打印出来,查看当前所有的Community,加上-a参数。
docker ps -a 当然也可以根据镜像生成一个具体名称的镜像,先删除当前容器
docker rm 60 当然如果当前容器正在运行,要删除这个容器,则使用命令
docker rm -f 60 使用镜像生成具体名称的容器:
docker run --name miaozhihao hello-world 于更多的docker run的命令可以使用来查看
docker run --help 第二个examples,启动docker的web服务
docker pull nginx docker run -d -p 80:80 --name webserver nginx -p参数是使用宿主机的80映射容器的80端口
curl localhost:80 Welcome to nginx! Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
交互式终端方式进入 webserver容器,
docker exec -it webserver bash 修改nginx的显示页面:
root@41b6804c716e:/# echo 'Hello, Docker!
' > /usr/share/nginx/html/index.html root@41b6804c716e:/# exit exit 修改了容器的文件,也就是改动了容器的存储层。我们可以通过 docker diff
docker diff webserver C /root A /root/.bash_history C /run A /run/nginx.pid C /usr/share/nginx/html/index.html C /var/cache/nginx A /var/cache/nginx/client_temp A /var/cache/nginx/fastcgi_temp A /var/cache/nginx/proxy_temp A /var/cache/nginx/scgi_temp A /var/cache/nginx/uwsgi_temp 使用docker commit生成镜像
docker commit --author "zhihao.miao <1026145686@qq.com>" --message "修改了默认网页" webserver nginx:v2 sha256:0a34c054b8a826d85dddf4d1dbdd3028ab890feff4c8a0844e9b98dd146c2e07 –autho 指定作者 --message表示容器的一些信息
查看当前nginx镜像:
docker images nginx REPOSITORY TAG IMAGE ID CREATED SIZE nginx v2 0a34c054b8a8 11 seconds ago 109 MB nginx latest 958a7ae9e569 4 weeks ago 109 MB 查看当前所有的容器,包括运行的和停止的
docker ps -a \CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 41b6804c716e nginx "nginx -g 'daemon ..." 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp webserver 85612b405cda miaozhihao001dockerhub/commit_test1 "nginx -g 'daemon ..." 20 hours ago Exited (0) 3 hours ago nginx_web 9ef1fb35d7aa ubuntu:14.04 "/bin/bash" 20 hours ago Exited (0) 20 hours ago commit_test bf8320b9e445 ubuntu:14.04 "/bin/bash" 24 hours ago Exited (0) 24 hours ago sharp_curie 9f9767eb8aaf hello-world "/hello" 10 days ago Exited (0) 10 days ago miaozhihao 启动新的容器
docker run --name newwebserver -d -p 80:80 nginx:v2 3619b34ed347cf1ae2ee3ab32c419140871f3084b9a1325ab5d8c6155d43bf06 ➜ curl localhost:80 Hello, Docker!
编辑Dockerfile文件,填入以下内容:
FROM docker/whalesay:latest RUN apt-get -y update && apt-get install -y fortunes CMD /usr/games/fortune -a | cowsay 运行以下命令创建名为docker-whale的镜像:
docker build -t docker-whale . -t: 给tag命令.: Dockerfile文件所在路径docker tag 8e15421920b1 xulingfeng/docker-whale:latest 上传操作
docker push xulingfeng/docker-whale 下载操作
docker pull centos docker run -t -i ubuntu /bin/bash -i 允许你和容器进行交互操作/bin/bash 启动容器中的Bash shelldocker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done" docker logs -f 容器名 -f 类似与 tail -fdocker run -d -P training/webapp python app.py -d:代表后台运行该容器-P:映射容器中的web应用端口号到你的主机上32768-61000中的某一个端口。这样你可以访问该容器中的web应用training/webapp: 一个已经构建好的镜像,包含一个简单的python flask框架web应用python app.py:这个命令用来启动容器中的web成功运行以上命令后,运行:docker ps
查看到容器的5000端口号映射到了本地的32768,浏览器访问http://127.0.0.1:32768 看到helloworld 成功提示
docker run -d -p 80:5000 training/webapp python app.py http://127.0.0.1 即可docker top 容器名 docker inspect 容器名 docker search 内容 centos镜像,分解步骤如下
yum install -y epel-releaseyum install -y gcc automake autoconf libtool make gcc-c++yum install -y wgetyum install -y openssl-develwget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz -P /software/tar -zxvf /software/Python-3.5.2.tgz -C /software/与./configure && make -j2&& make install -j2pip install --upgrade pip与pip install --upgrade setuptoolspip install Django Flask requests PyMySQL ldap3 jira celery simplejsonDockerfile:
FROM centos:latest RUN yum install -y epel-release RUN yum install -y gcc automake autoconf libtool make gcc-c++ RUN yum install -y wget RUN yum install -y openssl-devel RUN wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz -P /software/ RUN tar -zxvf /software/Python-3.5.2.tgz -C /software/ RUN cd /software/Python-3.5.2/ && ./configure python3 && make -j2&& make install -j2 RUN pip install --upgrade pip RUN pip install --upgrade setuptools RUN pip install Django Flask requests PyMySQL ldap3 jira celery simplejson 在Dockerfile目录中执行:docker build -t 名字:版本 .
brew install socat socat TCP-LISTEN:2376,reuseaddr,fork,bind=127.0.0.1 UNIX-CLIENT:/var/run/docker.sock 下一篇:基于深度学习的适应硬件的神经网络