小知识:Docker容器简单部署nginx过程解析

1、容器中部署nginx服务

centos:7镜像运行一个容器,并且,在这个容器内部署nginx服务。

[root@docker ~]# docker pull centos:7 //下载镜像

[root@docker ~]# docker run -itd –name webapp –restart=always centos:7 //运行一个容器名为:webapp

[root@docker ~]# docker cp nginx-1.16.0.tar.gz webapp:/root //将本地nginx包导入webapp容器内

[root@docker ~]# docker exec -it webapp /bin/bash //进入容器

[root@85099880dabe ~]# tar zxf nginx-1.16.0.tar.gz

[root@85099880dabe ~]# cd nginx-1.16.0

[root@85099880dabe nginx-1.16.0]# yum install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel //下载相关依赖包

[root@85099880dabe nginx-1.16.0]# useradd -m -s /sbin/nologin nginx //添加一个没有登录权限的nginx用户

[root@85099880dabe nginx-1.16.0]# ./configure –prefix=/usr/local/nginx –user=nginx –group=nginx //编译安装

%小知识:Docker容器简单部署nginx过程解析-猿站网-插图

[root@85099880dabe nginx-1.16.0]# make && make install //编译安装

[root@85099880dabe nginx-1.16.0]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ //创建软链接

[root@85099880dabe nginx-1.16.0]# nginx

[root@85099880dabe nginx-1.16.0]# nginx //确认nginx服务开启

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: address already in use)

[root@85099880dabe nginx-1.16.0]# cd /usr/local/nginx/html/

[root@85099880dabe html]# echo “test web” > index.html

[root@85099880dabe html]# curl 127.0.0.1

test web

2、迁移镜像

所需环境:

docker1主机:192.168.45.129

docker2主机:192.168.45.134

1)docker1主机

将docker1的刚刚创建的容器并制作成镜像导入docker2主机上

[root@docker ~]# docker commit webapp myweb //将webapp容器制作成镜像名为myweb

sha256:b035b8e8a36140e1bdbda9cf3a736b139ea8a48db7871a10f509b8f34d4c0f82

[root@docker ~]# docker save > myweb.tar myweb:latest //将镜像导出

[root@docker ~]# scp myweb.tar 192.168.45.134:/root //将镜像导出的tar包cp给docker2主机

the authenticity of host 192.168.45.134 (192.168.45.134) cant be established.

ecdsa key fingerprint is d7:77:71:90:34:25:c0:ec:e0:b6:5c:cc:6b:44:93:7b.

are you sure you want to continue connecting (yes/no)? yes

warning: permanently added 192.168.45.134 (ecdsa) to the list of known hosts.

root@192.168.45.134s password: //密码为docker2主机的密码

myweb.tar 100% 353mb 176.4mb/s 00:02

2)docker2主机

[root@docker2 ~]# docker load < myweb.tar //将刚刚cp过来的tar包制作成镜像

[root@docker2 ~]# docker run -itd –name newweb myweb:latest //启动一个容器

[root@docker2 ~]# docker exec -it newweb /bin/bash //进入容器

[root@4e419b580248 /]# nginx

[root@4e419b580248 /]# nginx //确定已启动nginx服务

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: address already in use)

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: address already in use)

nginx: [emerg] still could not bind()

[root@4e419b580248 /]# curl 127.0.0.1

test web

注:这里我们可以看见在docker1主机容器中搭建的简单nginx环境并制作成镜像成功的迁移到了docker2主机上了

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.51cto.com/14306186/2513103

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:Linux下如何使用minicom USB串口

2023-3-29 19:44:12

建站知识

小知识:详解Nginx 工作原理

2023-3-29 19:50:24

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索