Docker 要求 Ubuntu 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的 Ubuntu 版本是否支持 Docker。php
经过 uname -r 命令查看你当前的内核版本html
经过 apt-get install docker.io 安装dockerjava
-a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;mysql
-d: 后台运行容器,并返回容器ID;nginx
-i: 以交互模式运行容器,一般与 -t 同时使用;git
-t: 为容器从新分配一个伪输入终端,一般与 -i 同时使用;github
--name="nginx-lb": 为容器指定一个名称;redis
-P: 容器的80端口映射到主机的随机端口sql
-p: 容器的端口映射到主机的对应端口,例如: -p 80:80 docker
-v: 主机的目录映射(挂载)到容器的目录,例如:-v /home/ubuntu/nginx/www:/www
-m 或 --memory:设置内存使用限额。例如 -m 200M、--memory 300M;
--memory-swap:设置内存+swap的使用限额,当-m 200M --memory-swap=300M时,表示容器可使用200M内存和100Mswap;
--vm:启动内存工做线程数。例如:--vm 1,启动1个内存工做线程;
--vm-bytes 280M:每一个工做线程分配280M内存;
-c 或 --cpu-shares:设置容器使用cpu权重;
--dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;
--dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致;
-h "mars": 指定容器的hostname;
-e username="ritchie": 设置环境变量;
--env-file=[]: 从指定文件读入环境变量;
--cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定CPU运行;
-m :设置容器使用内存最大值;
--net="bridge": 指定容器的网络链接类型,支持 bridge/host/none/container:<name|id> 四种类型;
--link=[]: 添加连接到另外一个容器;
--expose=[]: 开放一个端口或一组端口;
-a :显示全部的容器,包括未运行的。例如:docker ps -a
-f :根据条件过滤显示的内容。
--format :指定返回值的模板文件。
-l :显示最近建立的容器。
-n : 列出最近建立的n个容器。例如:docker ps -n 5
--no-trunc :不截断输出。
-q :静默模式,只显示容器编号。
-s :显示总的文件大小。
-f :指定返回值的模板文件。例如:docker inspect -f '{{.NetworkSettings.IPAddress}}' mycon (获取正在运行的容器mycon的 IP)
-s :显示总的文件大小。
--type :为指定类型返回JSON。
-f : 跟踪日志输出,例如:docker logs -f mycon(查看容器mycon的日志输出)
--since :显示某个开始时间的全部日志
-t : 显示时间戳
--tail :仅列出最新N条容器日志,例如:docker logs --since="2017-05-01" --tail=10 mycon(查看容器mycon从2017年5月1往后的最新10条日志。)
-f :根据条件过滤事件;例如:docker events -f "image"="mysql:5.6" --since="1466302400" (显示docker 镜像为mysql:5.6 这个时间戳对应的日期以后的相关事件。)
--since :从指定的时间戳后显示全部事件;例如:docker events --since="1466302400" (显示docker 在这个时间戳对应的日期以后的全部事件。)
--until :流水时间显示到指定的时间为止;
-L :保持源目标中的连接
docker cp /www/test mycon:/www/ #将主机/www/test目录拷贝到容器mycon的/www目录下
docker cp /www/test mycon:/www #将主机/www/test目录拷贝到容器mycon中,目录重命名为www
docker cp mycon:/www /tmp/test #将容器mycon中的/www目录拷贝到主机的/tmp/test目录中
-o :将输入内容写到文件。例如:docker export -o mysql-`date +%Y%m%d`.tar a404c6c174a2 #将id为a404c6c174a2的容器按日期保存为tar文件。
-d :分离模式: 在后台运行
-i :即便没有附加也保持STDIN 打开
-t :分配一个伪终端
docker exec -i -t mycon /bin/bash #在容器mycon中开启一个交互模式的终端
docker exec -it mycon /bin/sh /data/test.sh #在容器mycon中以交互模式执行容器内/data/test.sh脚本
-a :列出本地全部的镜像(含中间映像层,默认状况下,过滤掉中间映像层);
--digests :显示镜像的摘要信息;
-f :显示知足条件的镜像;
--format :指定返回值的模板文件;
--no-trunc :显示完整的镜像信息;
-q :只显示镜像ID。
-c :应用docker 指令建立镜像;
-m :提交时的说明文字;
docker import my_ubuntu_v3.tar my/ubuntu:v4 #从镜像归档文件my_ubuntu_v3.tar建立镜像,命名为my/ubuntu:v4
--build-arg=[] :设置镜像建立时的变量;
--cpu-shares :设置 cpu 使用权重;
--cpu-period :限制 CPU CFS周期;
--cpu-quota :限制 CPU CFS配额;
--cpuset-cpus :指定使用的CPU id;
--cpuset-mems :指定使用的内存 id;
--disable-content-trust :忽略校验,默认开启;
-f :指定要使用的Dockerfile路径;
--force-rm :设置镜像过程当中删除中间容器;
--isolation :使用容器隔离技术;
--label=[] :设置镜像使用的元数据;
-m :设置内存最大值;
--memory-swap :设置Swap的最大值为内存+swap,"-1"表示不限swap;
--no-cache :建立镜像的过程不使用缓存;
--pull :尝试去更新镜像的新版本;
-q :安静模式,成功后只输出镜像ID;
--rm :设置镜像成功后删除中间容器;
--shm-size :设置/dev/shm的大小,默认值是64M;
--ulimit :Ulimit配置。
docker build -t test/ubuntu:v1 #使用当前目录的Dockerfile建立镜像。
docker build github.com/creack/docker-firefox #使用URL github.com/creack/docker-firefox 的 Dockerfile 建立镜像。
-a :提交的镜像做者;
-c :使用Dockerfile指令来建立镜像;
-m :提交时的说明文字;
-p :在commit时,将容器暂停。
docker commit -a "xst" -m "it is test" a404c6c174a2 mymysql:v1 #将容器a404c6c174a2 保存为新的镜像mymysql:v1,并添加提交人信息和说明信息。(当在容器内作了改变后能够提交做为本身定制的镜像)
-f :强制删除;
--no-prune :不移除该镜像的过程镜像,默认移除;
docker rmi -f test/ubuntu:16.04 #强制删除本地镜像test/ubuntu:16.04。
-o :输出到的文件。
docker save -o ubuntu_16.tar test/ubuntu:v3 #将镜像test/ubuntu:16.04 生成ubuntu_16.tar文档
-H :以可读的格式打印镜像大小和日期,默认为true;
--no-trunc :显示完整的提交记录;
-q :仅列出提交记录ID。
docker history test/ubuntu:16.04 #查看本地镜像test/ubuntu:16.04的建立历史。
docker tag ubuntu:15.10 test/ubuntu:v15(将镜像ubuntu:15.10标记为 runoob/ubuntu:v15 镜像。)
--automated :只列出 automated build类型的镜像;
--no-trunc :显示完整的镜像描述;
-s :列出收藏数不小于指定值的镜像。
docker search php:5.6-fpm #从Docker Hub查找镜像名php5.6-fpm
docker search -s 10 java #从Docker Hub查找全部镜像名包含java,而且收藏数大于10的镜像
-a :拉取全部 tagged 镜像
--disable-content-trust :忽略镜像的校验,默认开启
docker pull php:5.6-fpm #从仓库下载php5.6-fpm镜像到本地
docker pull -a java #从仓库下载REPOSITORY为java的全部镜像。
--disable-content-trust :忽略镜像的校验,默认开启
docker push mycon:v1 #上传本地镜像mycon:v1到镜像仓库中。
获取Container IP地址(Container状态必须是Up)docker inspect id | grep IPAddress | cut -d '"' -f 4 #
docker inspect mycon | grep IPAddress #查看mycon容器的IP地址相关的字段
获取端口映射docker inspect -f '{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' id #
获取环境变量docker exec container_id env #
杀掉全部正在运行的容器docker kill $(docker ps -q) #
删除老的(一周前建立)容器docker ps -a | grep 'weeks ago' | awk '{print $1}' | xargs docker rm #
删除已经中止的容器docker rm `docker ps -a -q` #
删除全部镜像,当心docker rmi $(docker images -q) #
Dockerfile是docker构建镜像的基础,也是docker区别于其余容器的重要特征,正是有了Dockerfile,docker的自动化和可移植性才成为可能。
不管是开发仍是运维,学会编写Dockerfile几乎是必备的,这有助于你理解整个容器的运行。
FROM ubuntu
MAINTAINER William <wlj@nicescale.com>
ENV TEST 1
RUN apt-get -y update RUN apt-get -y install nginx
ADD http://nicescale.com/ /data/nicescale.tgz
WORKDIR /var/www
USER nginx
VOLUME [‘/data’]
EXPOSE 80 443
ENTRYPOINT ["/usr/sbin/nginx"]
CMD ["start"]
docker建立、启动container时执行的命令,若是设置了ENTRYPOINT,则CMD将做为参数
docker build csphere/nginx:1.7 .
镜像从Dockerfile build生成后,须要将镜像推送(push)到镜像仓库。企业内部都须要构建一个私有docker registry,这个registry能够看做二进制的scm,CI/CD也须要围绕registry进行。
mkdir /registry docker run -p 80:5000 -e STORAGE_PATH=/registry -v /registry:/registry registry:2.0
假设192.168.1.2是registry仓库的地址:
docker tag csphere/nginx:1.7 192.168.1.2/csphere/nginx:1.7 docker push 192.168.1.2/csphere/nginx:1.7
1.建立并拉取busybox
# docker run -it --name con01 busybox:latest / # ip addr #容器里执行 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever Segmentation fault (core dumped) / # ping www.csphere.cn PING www.csphere.cn (117.121.26.243): 56 data bytes 64 bytes from 117.121.26.243: seq=0 ttl=48 time=3.139 ms 64 bytes from 117.121.26.243: seq=1 ttl=48 time=3.027 ms ^C --- www.csphere.cn ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max = 3.027/3.083/3.139 ms exit #退出容器
2.建立测试容器
docker run -d --name con03 csphere/test:0.1 efc9bda4a2ff2f479b18e0fc4698e42c47c9583a24c93f5ce6b28a828a172709
3.登录到con03中
# docker exec -it con03 /bin/bash [root@efc9bda4a2ff /]# exit
4.中止con03
# docker stop con03 con03
5.开启con03
# docker start con03 con03
6.删除con03
# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES efc9bda4a2ff csphere/test:0.1 "/usr/local/bin/run 4 minutes ago Up 17 seconds con03 99aa6ee25adc busybox:latest "/bin/sh" 14 minutes ago Exited (0) 12 minutes ago con02 831c93de9b9f busybox:latest "/bin/sh" 2 hours ago Up 27 minutes con01 # docker rm con02 #容器中止的状态 # docker rm -f con03 #容器开启的状态
1.从docker hub官方镜像仓库拉取镜像
# docker pull busybox:latest atest: Pulling from busybox cf2616975b4a: Pull complete 6ce2e90b0bc7: Pull complete 8c2e06607696: Already exists busybox: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:38a203e1986cf79639cfb9b2e1d6e773de84002feea2d4eb006b52004ee8502d Status: Downloaded newer image for busybox:latest
2.从本地上传镜像到镜像仓库
docker push 192.168.1.2/csphere/nginx:1.7
3.查找镜像仓库的某个镜像
# docker search centos/nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED johnnyzheng/centos-nginx-php-wordpress 1 [OK] sergeyzh/centos6-nginx 1 [OK] hzhang/centos-nginx 1 [OK]
4.查看本地镜像列表
# docker images TAG IMAGE ID CREATED VIRTUAL SIZE docker.io/csphere/csphere 0.10.3 604c03bf0c9e 3 days ago 62.72 MB docker.io/csphere/csphere latest 604c03bf0c9e 3 days ago 62.72 MB csphere/csphere 0.10.3 604c03bf0c9e 3 days ago 62.72 MB registry 2.0 2971b6ce766c 7 days ago 548.1 MB busybox latest 8c2e06607696 3 weeks ago 2.43 MB
5.删除镜像
docker rmi busybox:latest #没有容器使用此镜像建立,若是有容器在使用此镜像会报错:Error response from daemon: Conflict, cannot delete 8c2e06607696 because the running container 831c93de9b9f is using it, stop it and use -f to force FATA[0000] Error: failed to remove one or more images docker rmi -f busybox:latest #容器使用此镜像建立,此容器状态为Exited
6.查看构建镜像所用过的命令
# docker history busybox:latest IMAGE CREATED CREATED BY SIZE 8c2e06607696 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0 B 6ce2e90b0bc7 3 weeks ago /bin/sh -c #(nop) ADD file:8cf517d90fe79547c4 2.43 MB cf2616975b4a 3 weeks ago /bin/sh -c #(nop) MAINTAINER Jérôme Petazzo 0 B
补充:
sudo groupadd docker #添加一个新的docker用户组
sudo gpasswd -a username docker #添加当前用户到docker用户组里
sudo service docker restart #重启Docker后台监护进程
docker version #重启以后,尝试一下,是否生效
sudo reboot #若还未生效,则系统重启,则生效
查看命令的详细使用 例如:docker run --helpdocker cmd --help #
ps:假设读者可以在ubuntu环境下搭建lnmp环境的状况。
一、下载相应镜像到本地
docker pull php:5.4-fpm
docker pull php:5.5-fpm
docker pull php:5.6-fpm
docker pull php:7.0-fpm
docker pull php:7.1-fpm
docker pull mysql:5.7
docker pull nginx:1.11
二、建立相应容器
#建立php:5.4-fpm的容器并命名php5.4,将容器的9000端口映射到主机的9000端口。把主机的/home/ubuntu/nginx/www目录挂载到容器的/www目录(这个目录用于存放php脚本文件)
docker run -d -p 9000:9000 --name php5.4 -v /home/ubuntu/nginx/www:/www php:5.4-fpm
#在建立这个容器以前要先把前面建立的php5.4中止,否则9000端口会出现冲突。
docker run -d -p 9000:9000 --name php5.5 -v /home/ubuntu/nginx/www:/www php:5.5-fpm
#在建立这个容器以前要把前面建立的两个都中止,不能同时运行多个php容器,要使用不一样版本时,必须先stop正则运行的php容器,在start另外一个版本的php容器
docker run -d -p 9000:9000 --name php5.6 -v /home/ubuntu/nginx/www:/www php:5.6-fpm
#建立nginx容器,把配置主机的/home/ubuntu/nginx/conf目录挂载到容器的/etc/nginx/conf.d目录,用于处理nginx的配置文件,文件根目录与PHP的指向同一个
docker run -d -p 80::80 --name nginx1.11 -v /home/ubuntu/nginx/conf:/etc/nginx/conf.d -v /home/ubuntu/nginx/www:/www nginx:1.11
#建立mysql容器,-e MYSQL_ROOT_PASSWORD=123456将mysql容器的root密码初始化为123456
docker run -d -p 3306:3306 --name mysql5.7 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
#建立infobright容器,与MySQL容器的建立方法相似
docker run -d -p 5029:5029 --name myinfobright -e MYSQL_ROOT_PASSWORD=root infobright:latest
#建立容器redis3.2, redis-server --apendonly yes 表示在容器执行redis-server启动命令,并打开redis持久化配置
docker run -d -p 6379:6379 --name redis3.2 -v /home/ubuntu/redis/data:/data redis:3.2 redis-server --appendonly yes
docker run -d -p 11211:11211 --name memcached memcached:latest #建立memcached容器。
三、相关配置
而后在/home/ubuntu/nginx/conf目录下新建文件default.conf文件,配置nginx虚拟机:
server { listen 80; root /www; index index.php index.html index.htm index.nginx-debian.html; server_name test.com; location / { index index.php index.html index.htm; if (!-e $request_filename) { rewrite ^/(.*)$ /index.php?$1 last; break; } } location ~ \.php$ { fastcgi_pass 172.17.0.3:9000; #这里的ip是对应PHP版本的容器的ip地址,查看容器ip地址的方法前面有介绍 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
注意:安装php的容器的时候,php的镜像里应该都是没有相关扩展的。须要本身安装相关扩展,例如gd,mysql,mysqli等等。
以上面建立的php5.6为例:
首先进入容器:docker exec -it php5.6 /bin/bash
而后就能够安装相关扩展:docker-php-ext-install mysqli (mysql,gd,curl,...)安装完扩展后可使用docker commit提交到镜像成为本身定制的docker镜像。
安装镜像中非自带的php扩展:例如 php-redis 和 php-memcached 扩展:
php-redis扩展下载地址:https://github.com/phpredis/phpredis (选择适合的版本下载)
php-memcached下载地址:https://github.com/php-memcached-dev/php-memcached (选择适合版本下载)
能够在php容器内使用wget下载,也能够在ubuntu中下载后拷贝到容器中,我这里ubuntu中已经下载过这两个扩展,就直接用docker cp 将这两个扩展的源文件拷贝到php的容器中。
若是在容器中下载不要下载.zip类型,由于容器中没有unzip命令。能够在ubuntu中解压后将目录拷贝进容器。
docker cp /data/phpredis3.1.2 php5.6:/usr/src/php/ext/redis #将ubuntu中的/data/phpredis3.1.2拷贝到容器的/usr/src/php/ext/目录下并重命名为redis 。
docker-php-ext-install redis #安装 php-redis 扩展
安装memcached也是同理。。。
而后重启该容器便可。
到这里,环境已经初步搭建完成,访问主机的IP,能够查看运行状况,打印phpinfo查看扩展安装状况。参考文章地址:https://blog.csphere.cn/archives/22