一個通用的私有倉庫,能夠提高效率 Docker鏡像構建分爲兩種,一種是手動構建,一種是Dockerfile(自動構建)html
docker run --name testdocker -it centos yum install wget -y wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum install nginx -y
nginx配置文件中須要運行在前臺,vi /etc/nginx/nginx.conf 中加入 daemon off;python
root@jenkins:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 627b8bcc624c centos "/bin/bash" 29 minutes ago Exited (0) 15 seconds ago dockerfornginx root@jenkins:~# docker commit -m "test nginx" 627b8bcc624c dockerfornginx/dockerfornginx:v1 sha256:8ddfd5a400d738d5d69d7f1efae955fb8ec55c2a8741efff8f6863892cf3c0e1 root@jenkins:~# docker images REPOSITORY TAG IMAGE ID CREATED SIZE dockerfornginx/dockerfornginx v1 8ddfd5a400d7 8 seconds ago 371 MB #註釋 -m 描述 容器ID 第一個dockerfornginx是倉庫的名稱 第二個dockerfornginx是鏡像的名稱 v1 標籤,若是是最後一個版本咱們能夠打latest
root@jenkins:~# docker run --name docker01 -d -p 8888:80 dockerfornginx/dockerfornginx:v1 nginx 3193e76232eb8c32e8cb08fcf3d609619219e2641b111e7a0f00a8d51bfafb3a 咱們要寫鏡像全稱,帶上標籤 提示:後面的nginx不是鏡像的nginx,而是服務的名稱 root@jenkins:~# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3193e76232eb dockerfornginx/dockerfornginx:v1 "nginx" 6 seconds ago Up 5 seconds 0.0.0.0:8888->80/tcp docker01 root@jenkins:~# sh /data/docker/docker_in.sh docker01 [root@3193e76232eb /]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1/nginx: master pro tcp6 0 0 :::80 :::* LISTEN 1/nginx: master pro
注意:D須要大寫,當咱們構建dockerfile的時候,docker默認會在咱們當前目錄讀取一個名爲Dockerfile的文件。這時候的D必須大寫nginx
root@jenkins:/data/docker/dockerfile/nginx# pwd /data/docker/dockerfile/nginx root@jenkins:/data/docker/dockerfile/nginx# cat Dockerfile # This Dockerfile # Base image FROM centos # Maintainer MAINTAINER docker01 xxx@gmail.com #Commands RUN rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm RUN yum install -y nginx && yum clean all RUN echo "daemon off;" >>/etc/nginx/nginx.conf ADD index.html /usr/share/nginx/html/index.html EXPOSE 80 CMD ["nginx"] #井號表明註釋 #Base image 除了註釋的第一行,必須是FROM,意思就是咱們須要告訴dockerfile基礎鏡像是什麼 #Maintainer 維護信息 #Commands 命令 #ADD index.html 這個文件須要咱們在當前目錄下有才能夠,咱們配置咱們能夠準備好,而後使用ADD命令進行添加或修改 #EXPOSE 對外端口號 #CMD [「nginx」] 它要啓動的命令是nginx (就算是nginx服務)
Dockerfile中的index.html在同級目錄中新建 echo www.testdocker.com >index.htmldocker
root@jenkins:/data/docker/dockerfile/nginx# ll total 8 -rw-r--r-- 1 root root 775 Dec 9 02:52 Dockerfile -rw-r--r-- 1 root root 19 Dec 9 02:54 index.html root@jenkins:/data/docker/dockerfile/nginx# docker build -t sup:v2 .
提示:.表明構建的位置,咱們是當前目錄,咱們使用docker build進行構建centos
root@jenkins:/data/docker/dockerfile/nginx# docker run --name supv1 -d -p 8288:80 sup:v2 f44218f249da4427fe60bd2a21f78b16582e88900a3c4456010be144eda9ec0d root@jenkins:/data/docker/dockerfile/nginx# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f44218f249da sup:v2 "nginx" 3 seconds ago Up 2 seconds 0.0.0.0:8288->80/tcp supv1 3193e76232eb dockerfornginx/dockerfornginx:v1 "nginx" 21 minutes ago Up 21 minutes 0.0.0.0:8888->80/tcp docker01
提示:Dockerfile格式參考,http://www.javashuo.com/article/p-bmzxmzij-dk.htmlbash