文章目录
创建Dockerfile
Dockerfile的通用格式:
# 生产的新镜像的base image, 也可以认为是面向对象编程中的基础类, alping:是个mini的linux 系统
FROM alpine:latest
# 没有特殊含义, 只是说明作者或维护人是谁
MAINTAINER laoliu
# 运行命令 以及多种命令组合
CMD echo 'hello docker'
第一个Dockerfile
这是第一个Dockerfile的内容:
FROM alpine:latest
MAINTAINER laoliu
CMD echo 'hello docker'
第一个Dockerfile的创建,验证, 测试运行流程:
[root@laoliu home]# mkdir docker1
[root@laoliu home]# cd docker1/
[root@laoliu docker1]# vim Dockerfile
# 此处将第一个Dockerfile的内容输入进去并保存退出
# 请注意build命令最后的点,表示当前目录下的所有文件
[root@laoliu docker1]# docker build -t hello_docker:v1 .
Sending build context to Docker daemon 2.048 kB
Step 1/3 : FROM alpine:latest
Trying to pull repository docker.io/library/alpine ...
latest: Pulling from docker.io/library/alpine
bdf0201b3a05: Pull complete
Digest: sha256:28ef97b8686a0b5399129e9b763d5b7e5ff03576aa5580d6f4182a49c5fe1913
Status: Downloaded newer image for docker.io/alpine:latest
---> cdf98d1859c1
Step 2/3 : MAINTAINER loaliu
---> Running in 2f4c8881f105
---> e586be09dba6
Removing intermediate container 2f4c8881f105
Step 3/3 : CMD echo "hello Docker"
---> Running in d6209286134c
---> 388e1bb2a5f7
Removing intermediate container d6209286134c
Successfully built 388e1bb2a5f7
[root@laoliu docker1]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello_docker v1 388e1bb2a5f7 2 minutes ago 5.53 MB
docker.io/nginx latest 27a188018e18 2 weeks ago 109 MB
docker.io/alpine latest cdf98d1859c1 3 weeks ago 5.53 MB
[root@laoliu docker1]# docker run hello_docker:v1
hello Docker
[root@laoliu docker1]#
第二个Dockerfile
内容如下: # 为注释
FROM ubuntu
MAINTAINER laoliu
# 加速连接
RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
# 更新apt包, 建议经常配置
RUN apt-get update
# 下载nginx
RUN apt-get install -y nginx
# 复制index.html文件到image中
COPY index.html /var/www/html
# 容器入口
ENTRYPOINT ["/usr/sbin/nginx", "-g", "daemon off;"]
# 暴露端口
EXPOSE 80
流程如下:
[root@laoliu home]# mkdir docker2
[root@laoliu home]# cd docker2/
[root@laoliu docker2]# vim Dockerfile
# 输出上面的第二个Dockerfile 文件内容
[root@laoliu docker2]# vim index.html
# 将下面三行写入index.html
#<html>
#<h1>求工作...</html>
#</html>
[root@laoliu docker2]# docker build -t find_job:v1 .
Sending build context to Docker daemon 4.096 kB
Step 1/8 : FROM ubuntu
---> d131e0fa2585
Step 2/8 : MAINTAINER laoliu
---> Using cache
---> 4793b2149e3f
# (之后的过程省略, 直到出现下面的短语)
Successfully built edd93f6989c8
[root@laoliu docker2]# docker run -p 80:80 -d find_job:v1
a898c76111c0921b194865b44c07813cac43a286c3d5ad07f02bd32807a97c72
[root@laoliu docker2]# curl 192.168.31.160 # 这个是我的虚拟服务器地址
<html>
<h1>求工作...</html>
</html>
[root@laoliu docker2]#
验证方法:
- 打开浏览器:
服务器地址:80
检查是否可以打开自己的网页
Dockerfile常用命令
命令 | 用途 | 样例 |
---|---|---|
FROM | base image | FROM jupyter/base-notebook |
RUN | 执行命令 | RUN apt-get update -y && apt-get upgrade -y && apt-get install -y tzdata |
ADD | 添加文件 比copy更强大, 可以将远程的文件添加到image中 | |
COPY | 拷贝文件, 可以copy文件或目录 | COPY --chown=“${UID}:${GID} ” requirements.txt . |
CMD | 执行命令, 一般用来做容器入口 | |
EXPOSE | 暴露端口 | EXPOSE 8000 |
WORKDIR | 指定路径 | WORKDIR /home/${NB_USER} |
MAINTAINER | 维护者 | |
ENV | 设定环境变量 | |
ENTRYPOINT | 容器入口, 当与CMD同时使用时, CMD 后面的命令将作为entrypoint的参数 | ENTRYPOINT [“start_server.sh”] |
USER | 指定用户 | USER root |
VOLUME | mount point 指定容器所挂载的卷 |
Dockerfile 样例
centos7 中安装 python3.10
FROM centos:7
ENV PATH /usr/local/bin:$PATH
# 执行更新和安装软件包
RUN yum update -y && \
yum install -y \
wget \
curl \
vim \
gcc make autoconf \
&& yum clean all
WORKDIR /opt/apps
COPY ./requirements.txt ./requirements.txt
COPY ./Python-3.10.4.tgz ./Python-3.10.4.tgz # 先将Python-3.10.4.tgz下载至 pc 本地,或放到代码仓库中,都可以。
RUN tar xvzf Python-3.10.4.tgz
RUN cd Python-3.10.4 && ./configure --prefix=/usr/local/python3
RUN cd Python-3.10.4 && make
RUN cd Python-3.10.4 && make install
RUN ln -s /usr/local/python3/bin/python3 /usr/bin/python3
RUN echo python3 --version
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
EXPOSE 9111
说明: 本样例适用场景:一个非 python的服务需要运行在centos 中,需要使用 python脚本来支持一些辅助功能。
如果本身就是 python 的服务,则不用上面这么麻烦。
python3.10 服务
FROM python:3.10-slim
ENV PATH /usr/local/bin:$PATH
RUN apt-get update \
&& apt-get install -y vim \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* # RUN 这几行也可以不要。
WORKDIR /opt/apps
COPY ./ ./
COPY ./requirements.txt ./requirements.txt
# 部署包离线安装
# RUN python -m pip install --no-index --find-links=/opt/apps/pkg_whl -r requirements.txt
# 部署包为远程下载时应用
RUN /usr/local/bin/python -m pip install --upgrade pip
RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
# CMD ["python", "-O", "/opt/apps/main.py"]
EXPOSE 8800