Docker-docker制作镜像
一、下载镜像
a、以daocloud为mirror下载Nginx镜像
配置daocloud为mirror,下载Nginx镜像:(daocloud官网登陆后,发现镜像里找到Nginx镜像)
下载Nginx镜像:
[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]#
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]#
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]#
验证启动结果:
查看container(启动镜像(容器)):
[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]#
e、如果要更改容器启动的Nginx欢迎页,可以执行docker exec
命令进入容器,修改其内容。
[root@localhost docker]# docker exec -it webserver bash
oci runtime error: exec failed: container_linux.go:265: starting container process caused "exec: \"bash\": executable file not found in $PATH"
[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 /]#
刷新页面:
二、基于镜像定制自己的镜像
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
三、参考
镜像构建上下文:Context