手動製作docker鏡像(單個服務)的流程
1、啓動一個純淨的容器 ,在容器中安裝服務
2、把安裝好服務的容器提交爲鏡像
3、測試鏡像
製作一個基於centos6系統的nginx鏡像(單服務)
1:啓動一個純淨的centos:6.9容器,安裝nginx
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum install nginx -y
2:把安裝好服務的容器,提交爲鏡像
docker container commit eb109f194821 centos6.9_nginx:v1
3:測試鏡像的功能
docker run -d -p 82:80 centos6.9_nginx:v1 nginx -g 'daemon off;'
curl -I 10.0.0.11:82
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Wed, 27 May 2020 02:26:26 GMT
Content-Type: text/html
Content-Length: 3698
Last-Modified: Tue, 07 May 2019 06:09:40 GMT
Connection: keep-alive
ETag: "5cd12124-e72"
Accept-Ranges: bytes
手動做的鏡像不能修改初始命令 自動的可以修改
手動製作鏡像(雙服務)
啓動一個已經安裝nginx的容器 安裝ssh
1、啓動一個nginx基礎容器
docker run -it -p 88:80 -p 1022:22 centos6.9_nginx:v1 /bin/bash
yum install -y openssh-server
service sshd restart
echo '123456'|passwd --stdin root
指定進程 不會秒死
docker run -d -it -p 89:80 -p 1022:22 centos6.9_nginx_ssh:v2
進入容器之後
啓動雙服務
[root@b464d8664f20 /]# /etc/init.d/sshd start
Starting sshd: [ OK ]
[root@b464d8664f20 /]# nginx
在製作鏡像之前
寫一個腳本
[root@1b8dfe535159 ~]# cat /init.sh
#!/bin/bash
/etc/init.d/sshd start
nginx -g 'daemon off;'
並授權可執行
chmod +x /init.sh
執行嘗試一下
沒有問題 退出 製作鏡像
[root@docker01 ~]# docker container commit d063154e7962 lsy_test:v1
sha256:715c718400b2b33abaf2b03fe853aad6856c87e6d9edafb6da2d53270afab108
[root@docker01 ~]# docker rm -f `docker ps -a -q`
d063154e7962
運行鏡像 啓動容器
[root@docker01 ~]# docker run -d -p 90:80 -p 1024:22 lsy_test:v1 /init.sh
1b8dfe535159c05abac6551b21a4ed0e9512c511874b87af191fbd63175f1d31
[root@docker01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1b8dfe535159 lsy_test:v1 "/init.sh" 6 seconds ago Up 4 seconds 0.0.0.0:1024->22/tcp, 0.0.0.0:90->80/tcp hungry_lamarr
[root@docker01 ~]# ssh [email protected] -p 1024 測試
[email protected]'s password:
Last login: Wed May 27 03:17:32 2020 from 10.0.0.11
[root@1b8dfe535159 ~]# exit
logout
Connection to 10.0.0.11 closed.
測試
[root@docker01 ~]# curl -I 10.0.0.11:90
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Wed, 27 May 2020 03:22:29 GMT
Content-Type: text/html
Content-Length: 3698
Last-Modified: Tue, 07 May 2019 06:09:40 GMT
Connection: keep-alive
ETag: "5cd12124-e72"
Accept-Ranges: bytes
docker 共用宿主機內核 直接啓動自己的服務 不走開機啓動流程
容器的初始命令只能執行一條
部署有道雲盤並製作鏡像
註釋:有道雲網盤不需要後端數據庫服務
製作一個kodexplorer鏡像(nginx+php)
1:啓動一個基礎環境的容器,在容器中安裝服務
docker run -it -p 80:80 centos6.9_nginx:v1 /bin/bash
yum install php-fpm php-gd php-mbstring -y
修改PHP配置文件
vi /etc/php-fpm.d/www.conf
user = nginx
group = nginx
寫入nginx配置文件
vi /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
root /code;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
創建站點目錄
mkdir /code
宿主機操作:將本地的有道代碼文件放入到容器中
docker cp kodexplorer4.40.zip f12048d09d53:/code
解壓並授權
yum install unzip -y
unzip kodexplorer4.40.zip
chown -R nginx:ngin
編寫一個自動啓動腳本
vi /init.sh
#!/bin/bash
service php-fpm start
nginx -g 'daemon off;'
2:把安裝好服務的容器提交爲鏡像
docker commit f12048d09d53 kod:v1
3:測試docker run
docker run -d -p 81:80 kod:v1 /bin/bash /init.sh