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/