1、编辑Dockerfile 文件
root@operator:~/yanlian# vi Dockerfile
FROM python:3.7-alpine as build #基础镜像,一切从这里开始
WORKDIR /opt #设定工作目录,之后的命令将在该目录下执行。
COPY . /opt #将宿主机上的文件复制到镜像内部的相应目录下。
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories &&
apk add mariadb-dev
g++
build-base libffi-dev python3-dev
libffi openssl ca-certificates
jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev
linux-headers pcre-dev
RUN pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
CMD [ "python3", "main.py" ] #定义容器启动时执行的默认命令
Dockerfile指令介绍:
2、打包成docker 镜像
root@operator:~/yanlian# docker build -f Dockerfile -t yanlian:v1.1 . #注意 . 不可以少
Sending build context to Docker daemon 32.26kB
Step 1/6 : FROM python:3.7-alpine as build
3.7-alpine: Pulling from library/python
59bf1c3509f3: Already exists
8786870f2876: Already exists
45d4696938d0: Already exists
ef84af58b2c5: Already exists
c3c9b71b9a69: Already exists
Digest: sha256:d64e0124674d64e78cc9d7378a1130499ced66a7a00db0521d0120a2e88ac9e4
Status: Downloaded newer image for python:3.7-alpine
---> a1034fd13493
Step 2/6 : WORKDIR /opt
---> Running in 2f6a4f4b5c50
Removing intermediate container 2f6a4f4b5c50
---> 928b9c70762f
Step 3/6 : COPY . /opt
---> 5fd79218075c
Step 4/6 : RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && apk add mariadb-dev g++ build-base libffi-dev python3-dev libffi openssl ca-certificates jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev linux-headers pcre-dev
---> Running in dcdaa4e7f95a
fetch https://mirrors.aliyun.com/alpine/v3.15/main/x86_64/APKINDEX.tar.gz
fetch https://mirrors.aliyun.com/alpine/v3.15/community/x86_64/APKINDEX.tar.gz
3、保存镜像为文件
root@operator:~# docker save -o yanlian1.tar yanlian:v1.1
4、拷贝镜像文件到目标机
root@operator:~# scp yanlian1.tar 172.16.100.100:/root
root@172.16.100.100's password:
yanlian1.tar 100% 433MB 162.8MB/s 00:02
5、加载镜像
root@worker:~# docker load -i yanlian1.tar
11844785c490: Loading layer [==================================================>] 34.3kB/34.3kB
f9c736507bd9: Loading layer [==================================================>] 374.9MB/374.9MB
ae6ab8bb6d45: Loading layer [==================================================>] 35.62MB/35.62MB
Loaded image: yanlian:v1.1
6、查看镜像信息
root@worker:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
yanlian v1.1 a08041713e40 21 minutes ago 444MB
7、启动服务
root@worker:~# docker run -d --name yanlian1 -p 6005:6004 yanlian:v1.1
428575938af414ccc5a1fb1b3b8dd43006cb9f9e731ef1430b2339e432ab2fc9
8、查看运行的容器
root@worker:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
428575938af4 yanlian:v1.1 "python3 main.py" 5 seconds ago Up 4 seconds 0.0.0.0:6005->6004/tcp, :::6005->6004/tcp yanlian1
原文始发于微信公众号(云网数端):启动一个Docker 服务全流程
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/259555.html