通过dockerfile创建⼀个在启动容器时,就可以启动httpd服务的镜
像
步骤
-
创建⼀个⽬录,⽤于存储Docker file所使⽤的⽂件
-
在此⽬录中创建Docker file⽂件,以及镜像制作所使⽤的⽂件
-
使⽤docker build创建镜像
-
使⽤创建的镜像启动容器(验证)
思考 -
基础镜像
-
yum -y install httpd
-
使⽤脚本启动服务(httpd -k start)
-
应该将httpd服务放在前端执⾏
-
暴露端⼝ 80/tcp
-
添加⼀个测试⽂件,⽤于验证软件是否可⽤
过程1.创建⽬录
[root@docker001 ~]# mkdir test
2.跳转到⽬录中
[root@docker001 ~]# cd test/
3.编辑启动脚本
[root@docker001 test]# vim start.sh
[root@docker001 test]# cat start.sh
######################################
#!/bin/bah
rm -rf /run/httpd/*
exec /sbin/httpd -D FOREGROUND
######################################4.编辑index.html⽂件
[root@docker001 test]# vim index.html
5.编辑Dockerfile
[root@docker001 test]# vim Dockerfile
####################################
FROM centos_yum:v0
MAINTAINER "httpd server start
15773141955@163.com"
RUN yum clean all
RUN yum makecache
RUN yum -y install httpd
ADD start.sh /start.sh
RUN chmod -v +x /start.sh
Add index.html /var/www/html/
EXPOSE 80
WORKDIR /
CMD ["/bin/bash","/start.sh"]
####################################6.创建镜像
[root@docker001 test]# docker build -t
centos:httpd .
[+] Building 51.6s (12/12) FINISHED
docker:default
=> [internal] load build definition from
Dockerfile
0.0s
=> => transferring dockerfile: 309B
0.0s
=> [internal] load metadata for
docker.io/library/centos_yum:v0
0.0s
=> [internal] load .dockerignore
0.0s
=> => transferring context: 2B
0.0s
=> [1/8] FROM docker.io/library/centos_yum:v0
0.0s
=> [internal] load build context
0.0s
=> => transferring context: 180B
0.0s
=> [2/8] RUN YUM clean all
2.3s
=> [3/8] RUN YUM makecache
42.7s
=> [4/8] RUN YUM -y install httpd
5.8s
=> [5/8] ADD START.SH /START.SH
0.0s
=> [6/8] RUN CHMOD -v +x /start.sh
0.2s
=> [7/8] ADD INDEX.HTML /VAR/WWW/HTML/
0.0s
=> exporting to image
0.5s
=> => exporting layers
0.4s
=> => writing image
sha256:a488bc9f4cfb58a91baa57c2ab9faf615d457ff8714
e4e958e7220a253469ed 0.0s
=> => naming to docker.io/library/centos:httpd7.查看镜像
[root@docker001 test]# docker image ls
REPOSITORY TAG IMAGE ID CREATED
SIZE
centos httpd a488bc9f4cfb About a
minute ago 352MB
centos nginx 12fea1b890de 41 hours ago
366MB
centos java17 b6bf774de7bb 41 hours ago
550MB
centos_yum v0 191d9c84f461 2 days ago
260MB
mysql latest a82a8f162e18 4 weeks ago
586MB9.创建容器测试
[root@docker001 test]# docker run -d --name c0
centos:httpd
c245d9f9f3e0d6526d80e117ebd7b4c2870a1e12558192f1da
1e681cabdc4e87
[root@docker001 test]# docker ps --all
CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS
NAMES
c245d9f9f3e0 centos:httpd "/bin/bash
/start.sh" 21 seconds ago Up 20 seconds
80/tcp c010.查看ip地址
[root@docker001 test]# docker inspect c0
访问测试
[root@docker001 test]# curl 172.17.0.2
p
pp
ppp
pppp
ppppp
pppppp
ppppppp
pppppppp
测试挂载其他项⽬
[root@docker001 test]# docker run -d -v
/opt:/var/www/html --name c1 centos:httpd
3c97af2d11a56b9c2f2ea4eb6bb0a7ff18d6f26d0f777f68a4
eb4a20b9c61835
[root@docker001 test]# eche
"adsfadfafwqrdfqdsafdgdfsgda" > /opt/index.html
-bash: eche: 未找到命令
[root@docker001 test]# echo
"adsfadfafwqrdfqdsafdgdfsgda" > /opt/index.html
[root@docker001 test]# curl 172.17.0.3
adsfadfafwqrdfqdsafdgdfsgda
yum -y install yum-plugin-ovl
练习 配置nginx应⽤镜像
FROM centos:latest
MAINTAINER "NGINX"
RUN rm -rf /etc/yum.repos.d/*
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo
https://mirrors.aliyun.com/repo/Centos-vault-
8.5.2111.repo
RUN yum clean all && yum makecache
RUN yum -y install epel-release && yum -y
install nginx
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
WORKDIR /
CMD /usr/sbin/nginx
[root@docker001 ~]# docker run -d --name c1 -
p80:80 -v /opt/:/usr/share/nginx/html/
centos:nginx
948f20e1ed4758e78759de8f9ce9b34a7f44c431bc8ead75b6
70e1a5b488654c
创建私有仓库,在企业中分享项⽬
# 安装仓库镜像
[root@docker001 ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
930bdd4d222e: Pull complete
a15309931e05: Pull complete
6263fb9c821f: Pull complete
86c1d3af3872: Pull complete
a37b1bf6a96f: Pull complete
Digest:
sha256:12120425f07de11a1b899e418d4b0ea174c8d4d572d
45bdb640f93bc7ca06a3d
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
#查看新安装的镜像
[root@docker001 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED
SIZE
registry latest cfb4d9904335 10 months
ago 25.4MB
centos latest 5d0da3dc9764 2 years ago
231MB
# 创建挂载的⽬录
[root@docker001 ~]# mkdir /opt/dockeregistry
# 创建容器,映射端⼝,挂载⽂件
[root@docker001 ~]# docker run -d --name c1 -
p5000:5000 -v
/opt/dockeregistry/:/var/lib/registry
registry:latest
7ec28de22490aee02bd542ef22076f8ba746f7513cf96bf501
d817bc07b38f27
[root@docker001 ~]# docker ps
CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS
NAMES
7ec28de22490 registry:latest "/entrypoint.sh
/etc..." 13 seconds ago Up 12 seconds
0.0.0.0:5000->5000/tcp, :::5000->5000/tcp c1
# 访问⽬录⻚
[root@docker001 ~]# curl
http://10.1.1.50:5000/v2/_catalog
{"repositories":[]}
# 创建仓库
[root@docker001 ~]# vim /etc/docker/daemon.json
[root@docker001 ~]# cat /etc/docker/daemon.json
{
"insecure-registries":[
"http://10.1.1.50:5000"
],
"registry-mirrors": [
"https://do.nark.eu.org",
"https://dc.j8.work",
"https://docker.m.daocloud.io",
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn"
],
"hosts": [
"tcp://0.0.0.0:2375",
"unix:///var/run/docker.sock"
]
}
# 重启docker服务
[root@docker001 ~]# systemctl restart
docker.service
# 查看镜像
[root@docker001 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED
SIZE
registry latest cfb4d9904335 10 months
ago 25.4MB
centos latest 5d0da3dc9764 2 years ago
231MB
# 为要上传的镜像添加标记
[root@docker001 ~]# docker tag centos:latest
10.1.1.50:5000/centos:v0
[root@docker001 ~]# docker images
REPOSITORY TAG IMAGE ID
CREATED SIZE
registry latest cfb4d9904335
10 months ago 25.4MB
10.1.1.50:5000/centos v0 5d0da3dc9764 2
years ago 231MB
centos latest 5d0da3dc9764 2
years ago 231MB
10.1.1.50/centos v0 5d0da3dc9764 2
years ago 231MB
[root@docker001 ~]# docker push
10.1.1.50:5000/centos:v0
The push refers to repository
[10.1.1.50:5000/centos]
Get "http://10.1.1.50:5000/v2/": dial tcp
10.1.1.50:5000: connect: connection refused
[root@docker001 ~]# docker start c1
c1
[root@docker001 ~]# docker ps
CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS
NAMES
7ec28de22490 registry:latest "/entrypoint.sh
/etc..." 16 minutes ago Up 4 seconds
0.0.0.0:5000->5000/tcp, :::5000->5000/tcp c1
[root@docker001 ~]# docker push
10.1.1.50:5000/centos:v0
The push refers to repository
[10.1.1.50:5000/centos]
74ddd0ec08fa: Pushed
v0: digest:
sha256:a1801b843b1bfaf77c501e7a6d3f709401a1e0c8386
3037fa3aab063a7fdb9dc size: 529
[root@docker001 ~]# ls /opt/
1.png assets dist
index.html
2.png containerd dockeregistry
nohup.out
application.properties cvloume favicon.ico
Project_ExamSystem-V1.0.0.war
[root@docker001 ~]# ls /opt/dockeregistry/
docker
[root@docker001 ~]# ls /opt/dockeregistry/docker/
registry
[root@docker001 ~]# ls
/opt/dockeregistry/docker/registry/
v2
[root@docker001 ~]# ls
/opt/dockeregistry/docker/registry/v2/
blobs repositories
[root@docker001 ~]# ls
/opt/dockeregistry/docker/registry/v2/repositories
/
centos
[root@docker001 ~]#
1.拉取registry
docker pull registry
2.创建挂载⽬录
mkdir /regist
3.启动容器,映射端⼝,挂载⽬录
docker run -d --name c0 -v
/regist/:/var/lib/rigistry/ -p5000:5000
registry:latest
4.访问仓库
curl http://192.168.71.50:5000/v2/_catelog
5.配置pull和push,修改daemon.json
vim /etc/docker/daemon.json
{
.....,
"insecure-registries":[
"http://192.168.71.50:5000"
]
}
:wq
6.修改了配置⽂件,重启docker服务
systemctl restart docker
7.启动registry容器
docker start r1
curl localhost:5000/v2/_catalog