这篇基础文章是方便用户在使用cSphere平台之前,了解docker基础知识。
针对已经有一定的Linux基础知识的用户。
Docker是一个改进的容器技术。具体的“改进”体现在,Docker为容器引入了镜像,使得容器可以从预先定义好的模版(images)创建出来,并且这个模版还是分层的。
Docker的安装非常简单,支持目前所有主流操作系统,从Mac到Windows到各种Linux发行版
具体参考: docker安装
docker inspect id | grep IPAddress | cut -d '"' -f 4
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 <[email protected]>
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