Building Container Images
文章源自Katacoda Docker Playground的学习
Step 1 - Base Images
# Creating a Dockerfile
$ vim Dockerfile
FROM nginx:1.11-alpine
Step 2 - Running Commands
RUN <command>
allows you to execute any command
COPY <src> <dest>
copy local file to container
# Creating a Dockerfile
$ vim Dockerfile
FROM nginx:1.11-alpine
COPY index.html /usr/share/nginx/html/index.html
Step 3 - Exposing Ports(暴露端口)
EXPOSE <port>
是声明运行时容器提供服务端口
# Creating a Dockerfile
$ vim Dockerfile
FROM nginx:1.11-alpine
COPY index.html /usr/share/nginx/html/index.html
EXPOSE 80
Step 4 - Default Commands
文中的意思是CMD是在容器启动后开始执行,为什么要用default command没理解,下次看官方文档学
还有一个跟CMD类似的命令ENTRYPOINT
# Creating a Dockerfile
$ vim Dockerfile
FROM nginx:1.11-alpine
COPY index.html /usr/share/nginx/html/index.html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Step 5 - Building Containers
$ docker build -t my-nginx-image:latest .
Sending build context to Docker daemon 3.072kB
Step 1/4 : FROM nginx:1.11-alpine
---> bedece1f06cc
Step 2/4 : COPY index.html /usr/share/nginx/html/index.html
---> 5e7ec77f11f5
Step 3/4 : EXPOSE 80
---> Running in bbba47274f20
Removing intermediate container bbba47274f20
---> 2affd960d187
Step 4/4 : CMD ["nginx", "-g", "daemon off;"]
---> Running in 1763dbb56315
Removing intermediate container 1763dbb56315
---> 7d389eab8621
Successfully built 7d389eab8621
Successfully tagged my-nginx-image:latest
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.11-alpine bedece1f06cc 3 years ago 54.3MB
Step 6 - Launching New Image
$ docker run -d -p 80:80 my-nginx-image:latest
379c8748a3415a1ddd019f119cb1d5a201419ea8e403200a802bbd6030c86c6e
$ curl -i http://docker
HTTP/1.1 200 OK
Server: nginx/1.11.13
Date: Thu, 30 Apr 2020 15:48:19 GMT
Content-Type: text/html
Content-Length: 21
Last-Modified: Thu, 30 Apr 2020 15:45:11 GMT
Connection: keep-alive
ETag: "5eaaf287-15"
Accept-Ranges: bytes
<h1>Hello World</h1>
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
379c8748a341 my-nginx-image:latest "nginx -g 'daemon of…" 40 seconds ago Up 38 seconds 0.0.0.0:80->80/tcp, 443/tcp hungry_payne