创建自己的docker镜像(非Dockerfile构建)
安装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.
创建自己的docker镜像
1.从远程仓库拉取一个纯净的 centos 系统镜像
查询 centos 相关的镜像
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.创建并进入容器
创建容器
格式: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.操作容器
在容器中安装 环境 ,我演示的是安装 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
测试已经安装好python3
python3 --version
[root@ccba588539a8 /]# python3 --version
Python 3.6.8
退出容器
exit
[root@ccba588539a8 /]# exit
exit
[root@localhost ~]#
在宿主机上创建个空文件夹
mkdir /docker_test
在文件夹内创建编写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)
在文件夹内创建编写requirements.txt
requirements.txt
Flask
在文件夹内创建编写start_app.sh
start_app.sh
#!/usr/bin/env bash
nohup python3 /app/app.py & #启动服务
/bin/bash #保留一个终端,防止容器自动退出
文件创建好了
.
├── app.py
├── requirements.txt
└── start_app.sh
将文件拷贝到docker容器内
格式: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增加执行权限
[root@ccba588539a8 app]# mv start_app.sh /
[root@ccba588539a8 app]# cd /
[root@ccba588539a8 /]# chmod 777 start_app.sh
在容器内安装依赖包
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 创建镜像,标签为 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
查看创建好的镜像
[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
镜像在其他服务器还原
任何方式传给别的服务器
我这边用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
将镜像上传到公共仓库
注册账号啥的我就不重复了,参考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
设置镜像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
镜像拉取到本地后的执行命令:
docker run -dit --name "reduction-my-first-docker" -p 0.0.0.0:9980:80 v7hinc/my-first-docker /start_app.sh