Docker-docker製做鏡像

1、下載鏡像

a、以daocloud爲mirror下載Nginx鏡像

配置daocloud爲mirror,下載Nginx鏡像:(daocloud官網登錄後,發現鏡像裏找到Nginx鏡像html

下載Nginx鏡像:linux

[root@localhost Desktop]# docker pull daocloud.io/library/nginx:1.13.0-alpine
1.13.0-alpine: Pulling from library/nginx
b2388ca7fa65: Pull complete
7947be538089: Pull complete
d16f692df913: Pull complete
0dbd6ee41762: Pull complete
Digest: sha256:5c36f962c506c379bd63884976489c9c5e700c1496a6e8ea13dc404b1d258f76
Status: Downloaded newer image for daocloud.io/library/nginx:1.13.0-alpine
[root@localhost Desktop]# nginx

b、查看全部已下載的鏡像

[root@localhost Desktop]# docker images
REPOSITORY                             TAG                      IMAGE ID              CREATED          SIZE
daocloud.io/library/nginx       1.13.0-alpine           f00ab1b3ac6d         6 months ago      15.5MB
[root@localhost Desktop]# git

c、下載的docker在本地的位置:/var/lib/docker/

d、docker啓動nginx:

[root@localhost docker]# docker run --name webserver -d -p 80:80 nginx:1.13.0-alpine
92d1de9fb008b7efc7d73390fa8b62ef1a1e45a2564e93b481dfb3ff5d68b7b8
[root@localhost docker]# github

驗證啓動結果:web

  

查看container(啓動鏡像(容器)):docker

[root@localhost docker]# docker ps
CONTAINER ID         IMAGE                            COMMAND                  CREATED              STATUS                    PORTS                                NAMES
92d1de9fb008      nginx:1.13.0-alpine      "nginx -g 'daemon ..."        2 minutes ago          Up 2 minutes          0.0.0.0:80->80/tcp                webserver
[root@localhost docker]# bash

e、若是要更改容器啓動的Nginx歡迎頁,能夠執行docker exec 命令進入容器,修改其內容。

 [root@localhost docker]# docker exec -it webserver bashtcp

oci runtime error: exec failed: container_linux.go:265: starting container process caused "exec: \"bash\": executable file not found in $PATH"ui

[root@localhost docker]#

報錯了,緣由是bash找不到,bash路徑是在/bin/sh下,因此咱們加上bash的路徑:

[root@localhost /]# docker exec -it webserver /bin/sh
/ # echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
/ # exit
[root@localhost /]#

刷新頁面:

2、基於鏡像定製本身的鏡像

a、使用 Dockerfile 定製鏡像(參考地址

Dockerfile 是一個文本文件,其內包含了一條條的指令(Instruction),每一條指令構建一層,所以每一條指令的內容,就是描述該層應當如何構建。

還以以前定製 nginx 鏡像爲例,此次咱們使用 Dockerfile 來定製。

在一個空白目錄中,創建一個文本文件,並命名爲 Dockerfile:(Dockerfile 全路徑 /usr/local/mynginx/Dockerfile)

[root@localhost local]# mkdir mynginx
[root@localhost local]# cd mynginx/
[root@localhost mynginx]# touch Dockerfile
[root@localhost mynginx]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 28 10:14 Dockerfile
[root@localhost mynginx]# vi Dockerfile

Dockerfile編輯內容:

FROM nginx:1.13.0-alpine
RUN echo '<h1>Hello, Docker! dockerfile</h1>' > /usr/share/nginx/html/index.html

FROM: FROM 就是指定基礎鏡像,所以一個 Dockerfile 中 FROM 是必備的指令,而且必須是第一條指令。

RUN:指令是用來執行命令行命令的。

b、構建鏡像

[root@localhost mynginx]# docker build -t nginx:v12036 .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM nginx:1.13.0-alpine
---> f00ab1b3ac6d
Step 2/2 : RUN echo '<h1>Hello, Docker! dockerfile</h1>' > /usr/share/nginx/html/index.html
---> Running in 6249d27a5db9
---> bc1dc66316f7
Removing intermediate container 6249d27a5db9
Successfully built bc1dc66316f7
Successfully tagged nginx:v12036
[root@localhost mynginx]#

c、查看鏡像

[root@localhost mynginx]# docker images
REPOSITORY                              TAG                  IMAGE ID               CREATED              SIZE
       nginx                                   v12036              bc1dc66316f7        44 seconds ago      15.5MB
daocloud.io/library/nginx         1.13.0-alpine        f00ab1b3ac6d         6 months ago        15.5MB
       nginx                                1.13.0-alpine        f00ab1b3ac6d         6 months ago        15.5MB
[root@localhost mynginx]#

TAG爲v12036 的是基於nginx  1.13.0-alpine 構建的自定義的鏡像

e、驗證自定義的鏡像

[root@localhost mynginx]# docker run --name nginxv12306 -d -p 8081:80 nginx:v12036
docker: Error response from daemon: Conflict. The container name "/nginxv12306" is already in use by container "917dbc7d5110bb63d4dbdc5855996c24cc92ec950f88d4f799ac647f2f213b3e". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
[root@localhost mynginx]#

容器(nginxv12306)已經在啓動,此時換個容器名或刪除此容器:

[root@localhost mynginx]# docker rm -f nginxv12306
nginxv12306
[root@localhost mynginx]#

d、啓動docker,綁定8081端口:

[root@localhost mynginx]# docker run --name nginxv12036 -d -p 8081:80 nginx:v12036
cda0e3bd719315da07466e79df076ee486b23a4ef7849918739c5e06ed7fa287
[root@localhost mynginx]#

e、訪問地址:http://192.168.118.128:8081

 

3、參考

鏡像構建上下文:Context 

git repo構建

相關文章
相關標籤/搜索