Write a Dockerfile
In detail:
-
FROM(指定基礎image)
構建指令,必須指定且需要在Dockerfile其他指令的前面。后續的指令都依賴於該指令指定的image。FROM指令指定的基礎image可以是官方遠程倉庫中的,也可以位於本地倉庫。FROM <image> FROM <image>:<tag>
-
MAINTAINER(用來指定鏡像創建者信息)構建指令,用於將image的制作者相關的信息寫入到image中。當我們對該image執行docker inspect命令時,輸出中有相應的字段記錄該信息。
MAINTAINER <name>
-
RUN(安裝軟件用)構建指令,RUN可以運行任何被基礎image支持的命令。如基礎image選擇了ubuntu,那么軟件管理部分只能使用ubuntu的命令。
RUN <command> (the command is run in a shell - `/bin/sh -c`) RUN ["executable", "param1", "param2" ... ] (exec form)
-
CMD(設置container啟動時執行的操作)設置指令,用於container啟動時指定的操作。該操作可以是執行自定義腳本,也可以是執行系統命令。該指令只能在文件中存在一次,如果有多個,則只執行最后一條。
CMD ["executable","param1","param2"] (like an exec, this is the preferred form) CMD command param1 param2 (as a shell)
-
ENTRYPOINT(設置container啟動時執行的操作)設置指令,指定容器啟動時執行的命令,可以多次設置,但是只有最后一個有效。
ENTRYPOINT ["executable", "param1", "param2"] (like an exec, the preferred form) ENTRYPOINT command param1 param2 (as a shell)
-
USER(設置container容器的用戶)設置指令,設置啟動容器的用戶,默認是root用戶。
-
EXPOSE(指定容器需要映射到宿主機器的端口)
EXPOSE <port> [<port>...] # 映射多個端口 EXPOSE port1 port2 port3
-
ENV(用於設置環境變量)
ENV <key> <value> ENV JAVA_HOME /path/to/java/dirent
-
ADD(從src復制文件到container的dest路徑)
ADD <src> <dest>
For example, create a dockerfile in /home/pyt/test/:
FROM ubuntu:14.04.4
MAINTAINER puyangsky "puyangsky@163.com"
RUN apt-get update
RUN apt-get install -y nginx
RUN echo "hi , i am in your container" > /usr/share/nginx/html/index.html
expose 80
Build the image:
# docker build -t="puyangsky/test:v0.1" /home/pyt/test/
the result is like:
we see all images:
# docker images
Run the image
the second one is that we just created. And we run it up.
# docker run -d -p 80:80 --name test puyangsky/test:v0.1 nginx -g "daemon off;"
4f6306d2ff200ce37cd3c1ddffee97d29725aeb1871122051ca9a96e0ee852a0
we open the browser and type "localhost:80" and we get:
Push the image to Dockerhub
root@pyt:/home/pyt/test# docker push puyangsky/test:v0.1
The push refers to a repository [puyangsky/test] (len: 1)
229fc9eec22a: Image already exists
737e1830ecac: Image successfully pushed
12094b42ed11: Image successfully pushed
2ff6c8fffe7e: Image successfully pushed
874509123f48: Image successfully pushed
87d8afb341b5: Image successfully pushed
46e772baf32a: Image successfully pushed
0a7a3b768106: Image successfully pushed
e094bcb9d0fe: Image successfully pushed
a16f6804bd85: Image successfully pushed
Digest: sha256:174cec58394568c6af9a8a98c822a352f535811b2c30171e0e2e217bd60436be
And the image is uploaded to hub.docker.com, https://hub.docker.com/r/puyangsky/test/
Automated from github
Created a github repo: https://github.com/puyangsky/nginx_demo, and create from dockerhub.
So the image is automated by the github repo at https://hub.docker.com/r/puyangsky/nginx_demo/