創(chuàng)建自己的docker鏡像(非Dockerfile構(gòu)建)

安裝docker

yum install docker -y

開啟docker

systemctl start docker

查看docker是否開啟

systemctl status docker
image.png

開啟docker開機自啟

systemctl enable docker

[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

創(chuàng)建自己的docker鏡像

1.從遠程倉庫拉取一個純凈的 centos 系統(tǒng)鏡像

查詢 centos 相關(guān)的鏡像

docker search centos

第一個是官方鏡像
[root@localhost ~]# docker search centos
INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/centos                             The official build of CentOS.                   6183      [OK]       
docker.io   docker.io/ansible/centos7-ansible            Ansible on Centos7                              132                  [OK]
docker.io   docker.io/consol/centos-xfce-vnc             Centos container with "headless" VNC sessi...   119                  [OK]
docker.io   docker.io/jdeathe/centos-ssh                 OpenSSH / Supervisor / EPEL/IUS/SCL Repos ...   115                  [OK]
docker.io   docker.io/centos/systemd                     systemd enabled base container.                 86                   [OK]

下載鏡像到本地

docker pull docker.io/centos

[root@localhost ~]# docker pull docker.io/centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ... 
latest: Pulling from docker.io/library/centos
3c72a8ed6814: Pull complete 
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for docker.io/centos:latest

查看本地鏡像

docker images

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    latest              0d120b6ccaa8        4 weeks ago         215 MB

2.創(chuàng)建并進入容器

創(chuàng)建容器
格式:docker run -dit --name=容器名 鏡像 id /bin/bash

docker run -dit --name=myFirstDocker 0d120b6ccaa8 /bin/bash

[root@localhost ~]# docker run -dit --name=myFirstDocker 0d120b6ccaa8 /bin/bash
ccba588539a82f12b5855cabf83c213e36bf7503f0706a6370b3cd316eab3a11

查看所有的容器

docker ps -a

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ccba588539a8        0d120b6ccaa8        "/bin/bash"         44 seconds ago      Up 43 seconds                           myFirstDocker

進入容器
格式:docker exec -it 容器名 /bin/bash

docker exec -it myFirstDocker /bin/bash

3.操作容器

在容器中安裝 環(huán)境 祝沸,我演示的是安裝 python3

yum install python3 -y

[root@ccba588539a8 /]# yum install python3
Failed to set locale, defaulting to C.UTF-8
CentOS-8 - AppStream                                                                                          49 kB/s | 5.8 MB     02:01    
CentOS-8 - Base                                                                                              653 kB/s | 2.2 MB     00:03    
CentOS-8 - Extras                                                                                            8.8 kB/s | 7.3 kB     00:00    
Dependencies resolved.
=============================================================================================================================================
 Package                            Architecture          Version                                             Repository                Size
=============================================================================================================================================
Installing:
 python36                           x86_64                3.6.8-2.module_el8.1.0+245+c39af44f                 AppStream                 19 k
Installing dependencies:
 platform-python-pip                noarch                9.0.3-16.el8                                        BaseOS                   1.8 M
 python3-pip                        noarch                9.0.3-16.el8                                        AppStream                 19 k
 python3-setuptools                 noarch                39.2.0-5.el8                                        BaseOS                   162 k
Enabling module streams:
 python36                                                 3.6                                                                               

Transaction Summary
=============================================================================================================================================
Install  4 Packages

測試已經(jīng)安裝好python3

python3 --version

[root@ccba588539a8 /]# python3 --version
Python 3.6.8

退出容器

exit

[root@ccba588539a8 /]# exit
exit
[root@localhost ~]#

在宿主機上創(chuàng)建個空文件夾

mkdir /docker_test

在文件夾內(nèi)創(chuàng)建編寫python腳本
app.py

from flask import Flask
import socket
import getpass

app = Flask(__name__)


@app.route("/")
def hello():
    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>"
    return html.format(name=getpass.getuser(), hostname=socket.gethostname())


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=80)

在文件夾內(nèi)創(chuàng)建編寫requirements.txt
requirements.txt

Flask

在文件夾內(nèi)創(chuàng)建編寫start_app.sh
start_app.sh

#!/usr/bin/env bash
nohup python3 /app/app.py &  #啟動服務(wù)
/bin/bash  #保留一個終端,防止容器自動退出

文件創(chuàng)建好了

.
├── app.py
├── requirements.txt
└── start_app.sh

將文件拷貝到docker容器內(nèi)
格式:docker cp 本地文件路徑 ID全稱:容器路徑

docker cp . ccba588539a8:/app

進入容器可以看到拷貝的文件,

[root@localhost docker_test]# docker exec -it myFirstDocker /bin/bash
[root@ccba588539a8 /]# cd app/
[root@ccba588539a8 app]# ls
app.py  requirements.txt  start_app.sh

將start_app.sh移動到根目錄硼补,給start_app.sh增加執(zhí)行權(quán)限

[root@ccba588539a8 app]# mv start_app.sh /
[root@ccba588539a8 app]# cd / 
[root@ccba588539a8 /]# chmod 777 start_app.sh 

在容器內(nèi)安裝依賴包

pip3 install -r requirements.txt

安裝完成
[root@ccba588539a8 app]# pip3 install -r requirements.txt
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting Flask (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/f2/28/2a03252dfb9ebf377f40fba6a7841b47083260bf8bd8e737b0c6952df83f/Flask-1.1.2-py2.py3-none-any.whl (94kB)
    100% |████████████████████████████████| 102kB 99kB/s 
Collecting Werkzeug>=0.15 (from Flask->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/cc/94/5f7079a0e00bd6863ef8f1da638721e9da21e5bacee597595b318f71d62e/Werkzeug-1.0.1-py2.py3-none-any.whl (298kB)
    100% |████████████████████████████████| 307kB 10kB/s 
Collecting itsdangerous>=0.24 (from Flask->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting click>=5.1 (from Flask->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/d2/3d/fa76db83bf75c4f8d338c2fd15c8d33fdd7ad23a9b5e57eb6c5de26b430e/click-7.1.2-py2.py3-none-any.whl (82kB)
    100% |████████████████████████████████| 92kB 10kB/s 
Collecting Jinja2>=2.10.1 (from Flask->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/30/9e/f663a2aa66a09d838042ae1a2c5659828bb9b41ea3a6efa20a20fd92b121/Jinja2-2.11.2-py2.py3-none-any.whl (125kB)
    100% |████████████████████████████████| 133kB 31kB/s 
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->Flask->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/b2/5f/23e0023be6bb885d00ffbefad2942bc51a620328ee910f64abe5a8d18dd1/MarkupSafe-1.1.1-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: Werkzeug, itsdangerous, click, MarkupSafe, Jinja2, Flask
Successfully installed Flask-1.1.2 Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 itsdangerous-1.1.0

4.將容器制作成鏡像

使用當前目錄的 Dockerfile 創(chuàng)建鏡像喉童,標簽為 v7hinc/my-first-docker:v1庭呜。
格式:docker commit -a '制作者' -m '鏡像描述' 容器名 鏡像名:tag

docker commit -a "V7hinc" -m "my first docker"  ccba588539a8  my-first-docker:v1 

[root@localhost docker_test]# docker commit -a "V7hinc" -m "my first docker"  ccba588539a8  my-first-docker:v1 
sha256:307ec9f1cd9a452c0473f0a1ca915c4fbef4c233017b0fd71f12405c58b09fd6

查看創(chuàng)建好的鏡像

[root@localhost docker_test]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my-first-docker     v1                  307ec9f1cd9a        28 seconds ago      259 MB
docker.io/centos    latest              0d120b6ccaa8        4 weeks ago         215 MB

將制作好的鏡像打成 tar 包

格式:docker save -o tar包的名字 鏡像名

[root@localhost docker_test]# docker save -o my-first-docker.tar my-first-docker 
[root@localhost docker_test]# ls
app.py  my-first-docker.tar  requirements.txt

鏡像在其他服務(wù)器還原

任何方式傳給別的服務(wù)器
我這邊用scp:scp my-first-docker.tar root@192.168.234.140:/develop/docker/
怎么使用tar包還原鏡像
格式:docker load < tar 包所在路徑

[root@localhost docker]# ls
my-first-docker.tar  nginx
[root@localhost docker]# docker load < my-first-docker.tar
99ddaea1f754: Loading layer [==================================================>] 44.64 MB/44.64 MB
Loaded image: my-first-docker:v1
[root@localhost docker]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
my-first-docker                v1                  307ec9f1cd9a        3 minutes ago       259 MB

啟動還原的docker鏡像
docker run命令:https://www.runoob.com/docker/docker-run-command.html
格式:docker run -dit --name "自定義容器名稱" -p 主機(宿主)端口:容器端口 鏡像id /bin/bash

docker run -dit --name "reduction-my-first-docker" -p 0.0.0.0:9980:80 307ec9f1cd9a /bin/bash

[root@localhost docker]# docker run -dit --name "reduction-my-first-docker" -p 0.0.0.0:9980:80 307ec9f1cd9a /start_app.sh
51ba7c3ae153a14014a21a7ab7cda073052bfce1a49baf0448c4270a184e997f

查看啟動的容器

[root@localhost docker]# docker ps -a
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                     PORTS                    NAMES
51ba7c3ae153        307ec9f1cd9a         "/start_app.sh"          8 seconds ago       Up 7 seconds               0.0.0.0:9980->80/tcp     reduction-my-first-docker

進入容器
docker attach 容器id

docker exec -it reduction-my-first-docker /bin/bash
或
docker attach 51ba7c3ae153

[root@51ba7c3ae153 /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 07:52 ?        00:00:00 bash /start_app.sh
root          6      1  0 07:52 ?        00:00:00 python3 /app/app.py
root          7      1  0 07:52 ?        00:00:00 /bin/bash
root         17      0  0 07:53 ?        00:00:00 /bin/bash
root         37     17  0 08:03 ?        00:00:00 ps -ef

用瀏覽器訪問


image.png

將鏡像上傳到公共倉庫

注冊賬號啥的我就不重復(fù)了暇赤,參考https://blog.csdn.net/weixin_42766128/article/details/98765822

登錄docker hub

[root@localhost docker_test]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: v7hinc
Password: 
Login Succeeded

設(shè)置鏡像tag
Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

[root@localhost docker_test]# docker tag my-first-docker:v1 v7hinc/my-first-docker:v1

[root@localhost docker_test]# docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
my-first-docker          v1                  307ec9f1cd9a        38 minutes ago      259 MB
v7hinc/my-first-docker   v1                  307ec9f1cd9a        38 minutes ago      259 MB
docker.io/centos         latest              0d120b6ccaa8        4 weeks ago         215 MB

push打好tag的鏡像

[root@localhost docker_test]# docker push v7hinc/my-first-docker:v1
The push refers to a repository [docker.io/v7hinc/my-first-docker]
99ddaea1f754: Pushed 
291f6e44771a: Mounted from library/centos 
v1: digest: sha256:07c6439d38550ff94343f517205af13f3f53906bbdc071630f2058716e5610de size: 741

Pull 拉取鏡像

docker pull v7hinc/my-first-docker

鏡像拉取到本地后的執(zhí)行命令:

docker run -dit --name "reduction-my-first-docker" -p 0.0.0.0:9980:80 v7hinc/my-first-docker /start_app.sh
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末冗栗,一起剝皮案震驚了整個濱河市演顾,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌隅居,老刑警劉巖钠至,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異胎源,居然都是意外死亡棉钧,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門涕蚤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宪卿,“玉大人,你說我怎么就攤上這事万栅∮蛹兀” “怎么了?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵烦粒,是天一觀的道長休溶。 經(jīng)常有香客問我,道長扰她,這世上最難降的妖魔是什么兽掰? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮徒役,結(jié)果婚禮上孽尽,老公的妹妹穿的比我還像新娘。我一直安慰自己忧勿,他們只是感情好杉女,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著鸳吸,像睡著了一般宠纯。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上层释,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天,我揣著相機與錄音快集,去河邊找鬼贡羔。 笑死廉白,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的乖寒。 我是一名探鬼主播猴蹂,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼楣嘁!你這毒婦竟也來了磅轻?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤逐虚,失蹤者是張志新(化名)和其女友劉穎聋溜,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體叭爱,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡撮躁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了买雾。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片把曼。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖漓穿,靈堂內(nèi)的尸體忽然破棺而出嗤军,到底是詐尸還是另有隱情,我是刑警寧澤晃危,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布叙赚,位于F島的核電站,受9級特大地震影響山害,放射性物質(zhì)發(fā)生泄漏纠俭。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一浪慌、第九天 我趴在偏房一處隱蔽的房頂上張望冤荆。 院中可真熱鬧,春花似錦权纤、人聲如沸钓简。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽外邓。三九已至,卻和暖如春古掏,著一層夾襖步出監(jiān)牢的瞬間损话,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留丧枪,地道東北人光涂。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像拧烦,于是被迫代替她去往敵國和親忘闻。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354