**

linux服务器docker制作镜像,本地拉取

**

首先,服务器和本地下载docker

然后,在服务器启动docker

systemctl run docker

然后下载Tomcat或者Apache

docker pull tomcat

docker pull httpd

然后启动下载的Tomcat或者Apache并映射虚拟端口到服务器的真实端口
Tomcat:

docker run --name softtomcat -p 8080:8080 -v $PWD/test:/usr/local/tomcat/webapps/test -d tomcat

$PWD/test:/usr/local/tomcat/webapps/test:将主机中当前目录下的 test 挂载到容器的 /test。
Apache:

docker run -p 80:80 -v $PWD/www/:/var/www/ -v $PWD/conf/httpd.conf:/etc/httpd//conf/httpd.conf -v $PWD/logs/:/etc/httpd/logs/ -d httpd

$PWD/www/:/var/www/: 将主机中当前目录下的 www 目录挂载到容器的 /var/www/。
$PWD/conf/httpd.conf:/etc/httpd//conf/httpd.conf 将主机中当前目录下的 conf/httpd.conf 文件挂载到容器的 /etc/httpd//conf/httpd.conf。
$PWD/logs/:/etc/httpd/logs/: 将主机中当前目录下的 logs 目录挂载到容器的 /etc/httpd/logs/。(这些路劲根据自己实际情况更改)

然后输入服务器对应地址和端口验证是否成功运行(阿里云服务器记得开发对应端口)

接下来输入

docker ps

查看运行中的容器

设置拉取镜像的地址 linux拉取镜像_centos


以Apache为例,输入

docker exec -it cd2678738373  /bin/bash

设置拉取镜像的地址 linux拉取镜像_docker_02


进入容器内部,然后查看对应位置,例如:

设置拉取镜像的地址 linux拉取镜像_docker_03


找到Apache初始页面位置,然后换掉他,如果是Tomcat的话,就找到对应webapps文件位置,然后把war包拷贝进去,如下:

docker cp test.html 2678738373:/usr/local/apache2/htdocs

记得先退出容器在执行命令
上面的2678738373:/usr/local/apache2/htdocs便是对应容器的位置

创建私有仓库(记得服务器要开放对应5000的端口)

docker run -d -p5000:5000 --name="mydocker" --restart=always -v /root/docker/registry:/var/lib/registry registry

设置拉取镜像的地址 linux拉取镜像_tomcat_04

接下来,打包这个容器制作镜像

docker commit -a"xyh" -m "apache" 4f2e4b188236 xyhapache
docker tag xyhapache “服务器地址”:5000/xyhapache

输入docker images

设置拉取镜像的地址 linux拉取镜像_centos_05


查看是否打包成功,打包成功将其推上去

docker push `39.98.212.204:5000/xyhapache`

在这一步可能会出错

设置拉取镜像的地址 linux拉取镜像_linux_06


如果出现上面的错误,请进入docker的目录,然后往daemon.json添加内容,如果没这个文件请自行创建。

设置拉取镜像的地址 linux拉取镜像_linux_07


内容如下:

{ "insecure-registries":["服务器名:5000"] }

设置拉取镜像的地址 linux拉取镜像_linux_08


然后重启docker,再次推送就可以了。

接下来在本地docker进行拉取

docker pull 39.98.212.204:5000/xyhapache

拉取可能会报和上面一样的错误,依旧是改本地的daemon.json文件解决。
然后在本地运行镜像

docker run -d -p 80:80 --name=test 39.98.212.204:5000/xyhapache

然后localhost:80访问。

(结束)