当前位置:   article > 正文

Docker保姆级教程:用Dockerfile文件构建专属于你的镜像_创建dockerfile文件成功的图片

创建dockerfile文件成功的图片

初学者想要详细的了解docker可以去Docker菜鸟教程仔细学习,本文只展示使用docker部署代码的全部过程。操作系统是ubuntu:18.04    (tip:一定要了解docker是什么,为什么要用docker,什么是镜像,什么是容器,镜像和容器的关系)

一、安装docker

打开终端,复制curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun就可以实现一键安装docker环境。 

二、构建镜像

构建镜像背景:我要将AlexNet图像分类网络用docker部署到另一台主机上,需要的环境有python3.7.15,torch,torchvision,以及若干包如absl-py、tqdm等等

写dockerfile文件,dockerfile文件内容指令可以参考:dockerfile文件内容指令

1.建立dockerfile文件

 2.开始编辑dockerfile文件(tip:可以先写一条指令保存退出,在文件夹里打开进行编辑)

 

下面是我编写的dockerfile文件,可以参考:

#基础镜像

FROM nvidia/cuda:10.2-cudnn8-runtime-ubuntu18.04

#基础镜像

FROM ubuntu:18.04

#这条指令通常都要加上

RUN apt update && \
    apt install -y \
        wget build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev \
        libreadline-dev libffi-dev libsqlite3-dev libbz2-dev liblzma-dev && \
    apt clean && \
    rm -rf /var/lib/apt/lists/*

#转到目录temp

WORKDIR /temp

#下载python3.7.15压缩包和解压3.7.15压缩包

RUN wget https://www.python.org/ftp/python/3.7.15/Python-3.7.15.tgz && \
    tar -xvf Python-3.7.15.tgz

#安装python3.7.15,此段指令可以参考指令

RUN cd Python-3.7.15 && \
    ./configure --enable-optimizations && \
    make && \
    make install

#工作目录

WORKDIR /workspace

#删除刚才下载的python3.7.15包

RUN rm -r /temp && \
    ln -s /usr/local/bin/python3 /usr/local/bin/python && \
    ln -s /usr/local/bin/pip3 /usr/local/bin/pip

#使用pip(pip是一款非常方便的python包管理工具)安装torch和torchvision

RUN pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116 && \
    rm -r /root/.cache/pip#

#以下五条指令是为了解决在运行最后一条指令RUN pip install -i https://pypi.douban.com/simple/ absl-py  && 。。。时报错:WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv)NG:以“root”用户运行pip可能会导致权限中断,以及与系统包管理器的行为冲突.
#我是pip了太多包,所以出错。

RUN pip install --upgrade pip --user
RUN find / -name pip-*
RUN cd /usr/local/lib/python3.7
RUN python3 -m venv tutorial-env
RUN /bin/bash -c "source tutorial-env/bin/activate"

#安装若干包如absl-py、tqdm等等

RUN pip install -i https://pypi.douban.com/simple/ absl-py  && \
    pip install -i https://pypi.douban.com/simple/ addict && \
    pip install -i https://pypi.douban.com/simple/ alfred-py && \
    pip install -i https://pypi.douban.com/simple/ antlr4-python3-runtime && \
    pip install -i https://pypi.douban.com/simple/ anyio && \
    pip install -i https://pypi.douban.com/simple/ argon2-cffi && \
    pip install -i https://pypi.douban.com/simple/ argon2-cffi-bindings && \

    pip install -i https://pypi.douban.com/simple/ Babel && \
    pip install -i https://pypi.douban.com/simple/ backcall && \
    pip install -i https://pypi.douban.com/simple/ beautifulsoup4 && \
    pip install -i https://pypi.douban.com/simple/ black && \
    pip install -i https://pypi.douban.com/simple/ bleach && \
    pip install -i https://pypi.douban.com/simple/ Bottleneck && \
    pip install -i https://pypi.douban.com/simple/ brotlipy && \
    pip install -i https://pypi.douban.com/simple/ cachetools && \
    pip install -i https://pypi.douban.com/simple/ cffi && \
    pip install -i https://pypi.douban.com/simple/ charset-normalizer && \
    pip install -i https://pypi.douban.com/simple/ click && \
    pip install -i https://pypi.douban.com/simple/ cloudpickle && \
    pip install -i https://pypi.douban.com/simple/ colorama && \
    pip install -i https://pypi.douban.com/simple/ commonmark && \
    pip install -i https://pypi.douban.com/simple/ cryptography && \

    pip install -i https://pypi.douban.com/simple/ Cython && \
    pip install -i https://pypi.douban.com/simple/  debugpy && \
    pip install -i https://pypi.douban.com/simple/  decorator && \

    pip install -i https://pypi.douban.com/simple/ Deprecated && \
    pip install -i https://pypi.douban.com/simple/ deprecation && \
    
    pip install -i https://pypi.douban.com/simple/ dill && \
    pip install -i https://pypi.douban.com/simple/ einops && \
    pip install -i https://pypi.douban.com/simple/ entrypoints && \
    pip install -i https://pypi.douban.com/simple/ fairscale && \
    pip install -i https://pypi.douban.com/simple/ fastjsonschema && \
    pip install -i https://pypi.douban.com/simple/ filelock && \
    pip install -i https://pypi.douban.com/simple/ funcy && \
    pip install -i https://pypi.douban.com/simple/ future && \
    pip install -i https://pypi.douban.com/simple/ fvcore && \
    pip install -i https://pypi.douban.com/simple/ google-auth && \
    pip install -i https://pypi.douban.com/simple/ google-auth-oauthlib && \
    pip install -i https://pypi.douban.com/simple/ grpcio && \
    pip install -i https://pypi.douban.com/simple/ gtrending && \
    pip install -i https://pypi.douban.com/simple/ huggingface-hub && \
    pip install -i https://pypi.douban.com/simple/ hydra-core && \
    pip install -i https://pypi.douban.com/simple/ idna && \
    pip install -i https://pypi.douban.com/simple/ importlib-metadata && \
    pip install -i https://pypi.douban.com/simple/ importlib-resources && \
    pip install -i https://pypi.douban.com/simple/ iopath && \
    pip install -i https://pypi.douban.com/simple/ ipykernel && \
    pip install -i https://pypi.douban.com/simple/ ipython && \
    pip install -i https://pypi.douban.com/simple/ ipython-genutils && \
    pip install -i https://pypi.douban.com/simple/ ipywidgets && \
    pip install -i https://pypi.douban.com/simple/ jedi && \
    pip install -i https://pypi.douban.com/simple/ Jinja2 && \
    pip install -i https://pypi.douban.com/simple/ joblib && \
    pip install -i https://pypi.douban.com/simple/ json5 && \
    pip install -i https://pypi.douban.com/simple/ jsonschema && \
   
    pip install -i https://pypi.douban.com/simple/ jupyter_core && \
    pip install -i https://pypi.douban.com/simple/ jupyter_packaging && \
    pip install -i https://pypi.douban.com/simple/ jupyter-server && \
    pip install -i https://pypi.douban.com/simple/ jupyterlab && \
    pip install -i https://pypi.douban.com/simple/ jupyterlab-pygments && \
      
    pip install -i https://pypi.douban.com/simple/ jupyterlab-widgets && \
    pip install -i https://pypi.douban.com/simple/ kiwisolver  && \
    pip --default-timeout=100 install Markdown  && \
    pip install -i https://pypi.douban.com/simple/ matplotlib && \
    pip install -i https://pypi.douban.com/simple/ mistune && \
    pip install -i https://pypi.douban.com/simple/ mkl-fft && \
    pip install -i https://pypi.douban.com/simple/ mkl-random && \
    pip install -i https://pypi.douban.com/simple/ mkl-service && \
    pip install -i https://pypi.douban.com/simple/ mmpycocotools && \
    pip install -i https://pypi.douban.com/simple/ natsort && \
    pip install -i https://pypi.douban.com/simple/ nbclassic && \
    pip install -i https://pypi.douban.com/simple/ nbclient && \
    pip install -i https://pypi.douban.com/simple/ nbconvert && \
    pip install -i https://pypi.douban.com/simple/ nbformat && \
    pip install -i https://pypi.douban.com/simple/ nbnb && \
    pip install -i https://pypi.douban.com/simple/ nest-asyncio && \
    pip install -i https://pypi.douban.com/simple/ notebook && \
    pip install -i https://pypi.douban.com/simple/ notebook_shim && \
    pip install -i https://pypi.douban.com/simple/ oauthlib && \
    pip install -i https://pypi.douban.com/simple/ omegaconf && \
    pip install -i https://pypi.douban.com/simple/ open3d && \
    pip install -i https://pypi.douban.com/simple/ opencv-python && \
    pip install -i https://pypi.douban.com/simple/ packaging && \
    pip install -i https://pypi.douban.com/simple/ pandas && \
    pip install -i https://pypi.douban.com/simple/ pandocfilters && \
    pip install -i https://pypi.douban.com/simple/ parso && \
    pip install -i https://pypi.douban.com/simple/ pascal-voc-writer && \
    pip install -i https://pypi.douban.com/simple/ pexpect && \
    pip install -i https://pypi.douban.com/simple/ pickleshare && \
    pip install -i https://pypi.douban.com/simple/ Pillow && \
    pip install -i https://pypi.douban.com/simple/ pkgutil_resolve_name && \
    pip install -i https://pypi.douban.com/simple/ platformdirs && \
    pip install -i https://pypi.douban.com/simple/ plyfile && \
    pip install -i https://pypi.douban.com/simple/ portalocker && \
    pip install -i https://pypi.douban.com/simple/ prometheus-client && \
    pip install -i https://pypi.douban.com/simple/ prompt-toolkit && \
    pip install -i https://pypi.douban.com/simple/ protobuf && \
    pip install -i https://pypi.douban.com/simple/ psutil && \
    pip install -i https://pypi.douban.com/simple/ ptyprocess && \
    pip install -i https://pypi.douban.com/simple/ pyasn1 && \
    pip install -i https://pypi.douban.com/simple/ pyasn1-modules && \
    pip install -i https://pypi.douban.com/simple/ pycocotools && \
    pip install -i https://pypi.douban.com/simple/ pycparser && \
    pip install -i https://pypi.douban.com/simple/ Pygments && \
    pip install -i https://pypi.douban.com/simple/ pyOpenSSL && \
    pip install -i https://pypi.douban.com/simple/ pyparsing && \
    pip install -i https://pypi.douban.com/simple/ pyquaternion && \
    pip install -i https://pypi.douban.com/simple/ pyrsistent && \
    pip install -i https://pypi.douban.com/simple/ PySocks && \
    pip install -i https://pypi.douban.com/simple/ python-dateutil && \
    pip install -i https://pypi.douban.com/simple/ pytz && \
    pip install -i https://pypi.douban.com/simple/ PyYAML && \
    pip install -i https://pypi.douban.com/simple/ pyzmq && \
    pip install -i https://pypi.douban.com/simple/ regex && \
    pip install -i https://pypi.douban.com/simple/ requests && \
    pip install -i https://pypi.douban.com/simple/ requests-oauthlib && \
    pip install -i https://pypi.douban.com/simple/ rsa && \
    pip install -i https://pypi.douban.com/simple/ scikit-learn && \
    pip install -i https://pypi.douban.com/simple/ scipy && \
    pip install -i https://pypi.douban.com/simple/ seaborn && \
    pip install -i https://pypi.douban.com/simple/ Send2Trash && \

    pip install -i https://pypi.douban.com/simple/ sniffio && \
    pip install -i https://pypi.douban.com/simple/ soupsieve && \
    pip install -i https://pypi.douban.com/simple/ tabulate && \
    pip install -i https://pypi.douban.com/simple/ tensorboard && \
    pip install -i https://pypi.douban.com/simple/ tensorboard-data-server && \
    pip install -i https://pypi.douban.com/simple/ tensorboard-plugin-wit && \
    pip install -i https://pypi.douban.com/simple/ tensorboardX && \
    pip install -i https://pypi.douban.com/simple/ termcolor && \
    pip install -i https://pypi.douban.com/simple/ terminado && \
    pip install -i https://pypi.douban.com/simple/ thop && \
    pip install -i https://pypi.douban.com/simple/ threadpoolctl && \
    pip install -i https://pypi.douban.com/simple/ timm && \
    pip install -i https://pypi.douban.com/simple/ tinycss2 && \
    pip install -i https://pypi.douban.com/simple/ tomli && \
    pip install -i https://pypi.douban.com/simple/ tomlkit && \
    pip install -i https://pypi.douban.com/simple/ torchaudio && \
    pip install -i https://pypi.douban.com/simple/ tornado && \
    pip install -i https://pypi.douban.com/simple/ tqdm && \
    pip install -i https://pypi.douban.com/simple/ trimesh && \
    pip install -i https://pypi.douban.com/simple/ typed-ast && \
    pip install -i https://pypi.douban.com/simple/ typing_extensions && \
    pip install -i https://pypi.douban.com/simple/ typish && \
    pip install -i https://pypi.douban.com/simple/ urllib3 && \
    pip install -i https://pypi.douban.com/simple/ wcwidth && \
    pip install -i https://pypi.douban.com/simple/ webencodings && \
    pip install -i https://pypi.douban.com/simple/ websocket-client && \
    pip install -i https://pypi.douban.com/simple/ wheel && \
    pip install -i https://pypi.douban.com/simple/ widgetsnbextension && \
    pip install -i https://pypi.douban.com/simple/ wrapt && \
    pip install -i https://pypi.douban.com/simple/ xdg && \
    pip install -i https://pypi.douban.com/simple/ yacs && \
    pip install -i https://pypi.douban.com/simple/ zipp

3.用写好的dockerfile文件构建镜像,指令:docker -t vms:sylu .  (vms是镜像名称,sylu是镜像标签)

 4.查看构建好的镜像vms,指令:docker images (tip:如果docker一些指令忘了,可以在终端执行指令:docker)

看到有vms,镜像构建成功

三、建立容器

1.以后台方式启动容器(后台启动的原因是方便等下将代码复制到容器中)

docker run -itd --name alexnet vms:sylu /bin/bash(alexnet是要建立的容器名称,vms:sylu是镜像名称)

  

 出现了一串数字和字母组合就说明构建成功了(子母和数字组合是容器的id)

2.查看容器,指令:docker ps

 记住容器id,下一步要用

3.将代码拷贝到容器中

指令:docker cp alexnet 2515307089e5:/workspace

docker cp的详细使用可以看docker cp

4.进入容器

指令:docker exec -it 2515307089e5 /bin/bash

(tip:想要进入容器,可以通过以下指令进入:

            (1)docker attach

            (2)docker exec:推荐大家使用 docker exec 命令,因为此命令会退出容器终端,但不会导致容器的停止。)


 5.进入alexnet文件,运行代码

四、导出镜像和容器可以使用指令

1.导出镜像:

docker save 镜像名:版本号 -o /路径/保存的包名.tar 

2.导出容器:

docker export 1e560fca3906 > alexnet.tar

导出容器 1e560fca3906 快照到本地文件 alexnet..tar

五、部署到另一台主机可以通过dockerfile文件生成镜像,再生成容器,也可以将导出的镜像和容器导入到另一台主机上。

具体过程全部结束!!!

   

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/380087
推荐阅读
相关标签
  

闽ICP备14008679号