前言
在看这一篇之前,可以先回顾一下使用现成的nginx镜像在搭建
https://blog.csdn.net/hl_java/article/details/86232900
可以发现,搭建成功后,服务是可以访问的(http://localhost:81/),这个时候你登录到容器(附登录到容器的命令示例:docker exec -it nginx81 /bin/bash )内部会发现 ll , vim , ps ,很多命令都是不能用的
那如果我还想使用上述命令该怎么办呢?
- 1.登录进去一个个地安装。
- 2.在nginx现有镜像的基础上构建一个自己的镜像。
接下来的篇幅将带你一起学习“在nginx现有镜像的基础上构建一个自己的镜像”
构建自己的nginx镜像
准备Dockerfile
vi ~/work/docker/dockerfiletest/Dockerfile
# base image
FROM nginx
# MAINTAINER
MAINTAINER lzc.java@icloud.com
RUN echo 'alias ll="ls -al"' >> ~/.bashrc
RUN echo 'alias grep="grep --color=auto"' >> ~/.bashrc
RUN echo 'alias psl="ps -eo user,pid,%cpu,%mem,vsz,rss,tty,stat,lstart,etime,command --sort=%cpu |grep java"' >> ~/.bashrc
生成镜像文件
docker build -t alioo/nginx:v1 ~/work/docker/dockerfiletest
Sending build context to Docker daemon 3.072kB
Step 1/5 : FROM nginx
---> 7042885a156a
Step 2/5 : MAINTAINER lzc.java@icloud.com
---> Using cache
---> 22a859b465ca
Step 3/5 : RUN echo 'alias ll="ls -al"' >> ~/.bashrc
---> Using cache
---> a1a8028e9658
Step 4/5 : RUN echo 'alias grep="grep --color=auto"' >> ~/.bashrc
---> Using cache
---> 749d2c8056e7
Step 5/5 : RUN echo 'alias psl="ps -eo user,pid,%cpu,%mem,vsz,rss,tty,stat,lstart,etime,command --sort=%cpu |grep java"' >> ~/.bashrc
---> Using cache
---> 3d417e7b21f5
Successfully built 3d417e7b21f5
进一步确认下镜像是否制件成功
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
alioo/nginx v1 3d417e7b21f5 31 minutes ago 109MB
hello-world latest fce289e99eb9 9 days ago 1.84kB
nginx latest 7042885a156a 12 days ago 109MB
好了,镜像都有了,我们接下就跟上一篇一样,直接创建容器吧
docker run \
--name nginx82 \
-d -p 82:80 \
-v /Users/alioo/work/docker/nginx/html:/usr/share/nginx/html \
-v /Users/alioo/work/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /Users/alioo/work/docker/nginx/log:/var/log/nginx/ \
-v /Users/alioo/work/docker/nginx/conf/conf.d:/etc/nginx/conf.d \
alioo/nginx:v1
进一步确认下容器是否启动成功
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3ffc102d65eb alioo/nginx:v1 "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:82->80/tcp nginx82
1238ca756d72 nginx "nginx -g 'daemon of…" 4 hours ago Up 4 hours 0.0.0.0:81->80/tcp nginx81
ba7819b6926a hello-world "/hello" 46 hours ago Exited (0) 46 hours ago infallible_volhard
curl http://localhost:82/
hello nginx
</br>
alioo come here!
</br>
i am success
ok,docker进程存活,网页也可以访问,到此这篇文章就结束了,有疑问可以留言哟。