Docker容器内部署Ngin服务
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 //执行编译配置

[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主机IP:192.168.45.129
docker2主机IP: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 //导出镜像为tar文件 [root@Docker ~]# scp myweb.tar 192.168.45.134:/root //将tar包传输给docker2主机 The authenticity of host '192.168.45.134 (192.168.45.134)' can't 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.134's password: //输入docker2主机的密码 myweb.tar 100% 353MB 176.4MB/s 00:02
2)操作docker2主机
[root@Docker2 ~]# docker load < myweb.tar //加载接收到的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主机上。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。