這篇基礎文章是方便用戶在使用cSphere平臺以前,瞭解docker基礎知識。php
針對已經有必定的Linux基礎知識的用戶。nginx
Docker是一個改進的容器技術。具體的「改進」體如今,Docker爲容器引入了鏡像,使得容器能夠從預先定義好的模版(images)建立出來,而且這個模版仍是分層的。docker
Docker的安裝很是簡單,支持目前全部主流操做系統,從Mac到Windows到各類Linux發行版
具體參考: docker安裝shell
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的自動化和可移植性才成爲可能。json
不管是開發仍是運維,學會編寫Dockerfile幾乎是必備的,這有助於你理解整個容器的運行。ubuntu
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將做爲參數centos
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倉庫的地址:bash
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教程# 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