首页 技术 正文
技术 2022年11月21日
0 收藏 760 点赞 3,423 浏览 2753 个字

如果我们需要在Docker环境下部署tomcat、redis、mysql、nginx、php等应用服务环境,有下面三种方法:

1,根据系统镜像创建Docker容器,这时容器就相当于是一个虚拟机,进入容器内部署应用环境。然后将这种应用容器提交为新的镜像
2,直接通过docker pull拉取别人提交好的tomcat、nginx等应用程序镜像,然后基于这些镜像去创建容器。
3,通过dockerfile制作自己的应用程序镜像。

 这里首先用系统镜像创建自己所需的docker容器,根据系统镜像部署容器,简单的步奏如下:

1,启动centos系统镜像,并进入系统镜像

2,在系统镜像中部署应用服务nginx,mysql,php等

3,将部署好的系统镜像提交为新的系统镜像,并使用新的镜像创建容器

4,将这个新的镜像打包,可以导入到别的docker服务器上使用

  下面使用下载好的centos系统镜像创建一个nginx应用服务

[root@localhost ~]# docker run  –privileged  –name  host  -d  -i  -t  centos  /sbin/init
820e8b0be033f65b512d92c2d2281d5140c90bcc734b780168cd3c0118f7d104

 –【最后执行的指令是 /sbin/init,如果这里仍是/bin/bash进入容器执行systemctl指令还是有同样的问题】

【centos7容器里面出现的一个BUG,即centos7镜像创建的容器里面安装服务后,不能用systemctl/service启动服务,centos6的容器里没有这个坑。
可以通过使用 --privileged 选项来避免这个错误。】

  进入centos容器安装nginx应用

[root@localhost ~]# docker exec -it host bash

  安装nginx服务

[root@820e8b0be033 /]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Retrieving http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
warning: /var/tmp/rpm-tmp.JHI5he: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:nginx-release-centos-7-0.el7.ngx ################################# [100%][root@820e8b0be033 /]# yum install nginx -y

  启动检查服务是否正常

[root@820e8b0be033 /]# systemctl start nginx
[root@820e8b0be033 /]# ps -ef |grep nginx
root 187 1 0 08:17 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 188 187 0 08:17 ? 00:00:00 nginx: worker process
root 190 77 0 08:17 ? 00:00:00 grep --color=auto nginx
[root@820e8b0be033 /]#

 将这个装了nginx服务的容器提交为新的镜像

[root@localhost ~]# docker stop 820
820
[root@localhost ~]# docker commit 820 nginx-server
sha256:a0f9f0a113a57bd90790b197096df3205c3f65ab5798e3364b31185617d84fe4
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-server latest a0f9f0a113a5 27 seconds ago 302 MB
docker.io/nginx latest 73acd1f0cfad 24 hours ago 109 MB
docker.io/centos latest 2d194b392dd1 9 days ago 195 MB
[root@localhost ~]#

 利用新提交的镜像创建一个新额容器 mynginx

[root@localhost ~]# docker run -d -i -t --name mynginx -p 80:80 -v /usr/share/nginx/html:/usr/share/nginx/html nginx-server
52ee2d59bbad033de9d0989682346d2cbc5e3bd34919ad738f66c64d73f98f28
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52ee2d59bbad nginx-server "/sbin/init" 7 seconds ago Up 6 seconds 0.0.0.0:80->80/tcp mynginx
[root@localhost ~]#

  启动新容器中nginx

 

[root@localhost ~]# docker exec -it 52e bash
[root@52ee2d59bbad /]# ps -ef | grep nginx
root 39 26 0 08:32 ? 00:00:00 grep --color=auto nginx
[root@52ee2d59bbad /]# systemctl start nginx
[root@52ee2d59bbad /]# ps -ef | grep nginx
root 45 1 0 08:33 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 46 45 0 08:33 ? 00:00:00 nginx: worker process
root 48 26 0 08:33 ? 00:00:00 grep --color=auto nginx
[root@52ee2d59bbad /]# exit
exit

  访问测试页面

Docker学习笔记–2 镜像的创建

后面学习使用Dockerfile的方式创建镜像~

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,997
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,356
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,139
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848