Docker | Use Docker to create custom images to encapsulate artificial intelligence projects and environments

-------------------------- Docker Series-------------------------- ----------

Docker | Just read this article for docker in deep learning_Deep Learning Docker-CSDN Blog

Docker | Use the remote server Linux to create a pytorch container, download the file on GitHub and run the Layout2img project (Step-by-step)_linux download the github file-CSDN Blog​​​​​

Docker | Detailed steps to install portainer with docker-CSDN Blog 

Docker | Push your own docker image to docker hub [picture and text details]-CSDN Blog

Docker is a tool for packaging, deploying, and running applications in lightweight containers . If you want to learn the basics of Docker, see the blog above.

1.Dockerfile writing rules

In the Dockerfile, everything on the left is  INSTRUCTION, and the right is the ARGUMENT of these instructions  .

Every instruction in the Dockerfile builds a layer, RUN and this is no exception. The behavior of each one  RUN is the same as the process of manually establishing the mirror just now: create a new layer, execute these commands on it, and after the execution is completed, commit the modification of this layer constitutes a new mirror.

Dockerfile instructions explain
FROM Specify base images that can be pulled from container registries (Docker hub, GCR, Quay, ECR, etc.)
RUN Execute the command during the image build process.
ENV Set environment variables in the image. It will be available during the build as well as in the running container. If you only want to set build-time variables, use the ARG directive.
COPY Copy local files and directories to image
EXPOSE Specify the port to be exposed for the Docker container.
ADD It is a more feature-rich version of the COPY instruction. It also allows copying from a URL as source and  automatically extracting tar files into images. However, it is recommended to use the COPY command instead of ADD. If you want to download a remote file, use curl or get it using RUN.
WORKDIR Set the current working directory. You can reuse this directive in your Dockerfile to set different working directories. If WORKDIR is set, execute the , , , or instructions in the directory.RUNCMDADDCOPYENTRYPOINT
VOLUME It is used to create volumes or mount volumes into Docker containers
USER Set the username and UID when running the container. You can use this directive to set a non-root user for the container.
LABEL Metadata information used to specify Docker images
ARG Used to set build-time variables with keys and values. The ARG variable will not be available while the container is running. If you want to persist variables on a running container, use ENV.
SHELL This directive sets shell options and the default shell for the RUN, CMD, and ENTRYPOINT directives that follow it.
CMD It is used to execute commands in a running container. There can only be one CMD, if there are multiple CMDs, only the last one applies. It can be overridden from the Docker CLI  .
ENTRYPOINT Specifies the command that will be executed when the Docker container starts. If no ENTRYPOINT is specified, it defaults to ENTRYPOINT. You can also use the CLI to override ENTRYPOINT using flags . For more information, see  CMD vs. ENTRYPOINT ./bin/sh -c--entrypoint

   

 Docker images are mounted sequentially (layer by layer) using federated mounting, and the image is built in the container.

2. Customize docker image

2.1. Method 1: Create an image through Dockerfile [most commonly used]

2.1.1.Writing Dockerfile

Create a new Dockerfile and write the environment

FROM nvcr.io/nvidia/pytorch:22.12-py3 

RUN apt-get update \
 && apt-get install -y espeak

WORKDIR /app

COPY MB-iSTFT-VITS2 /app/MB-iSTFT-VITS2

WORKDIR /app/MB-iSTFT-VITS2

RUN /usr/bin/python -m pip install --upgrade pip \
 && pip install -r requirements.txt 


EXPOSE 6843

ENV FLASK_APP=app.py

#CMD ["python", "/app/app.py"]
CMD ["flask", "run", "--host=0.0.0.0"]

2.2.2.Build the image

Build a Docker image using Dockerfile

The basic syntax is:

docker build [OPTIONS] PATH | URL | -

 docker build --no-cache  -t tts:v0.1 -f ./Dockerfile .

  

2.3.3 Use of mirrors

View the build image

  

2.3.3.1. Use mirror directly

If the image is configured with flask or other network frameworks when encapsulating it, target recognition, speech synthesis, etc. can be directly implemented through customization. This article uses artificial intelligence algorithm model + flask framework!

 docker run -p 5000:5000 tts:v0.1 python app.py --text 

or

 docker run -p 5000:5000 tts:v0.1 python app.py --text 

If an error occurs, refer to [PS1]

2.3.3.2. Generate containers using encapsulated images

 docker run -it --gpus all --ipc host --net host --name tts-test insanena414/tts_v1:latest  /bin/bash

 

2.2. Method 2: Create through Git repo

 docker build -t hello-world https://github.com/docker-library/hello-world.git#master:amd64/hello-world

The command specifies the Git repo required for the build, and specifies the branch as and  masterthe build directory as  /amd64/hello-world/. Then Docker will go to  git clone the project, switch to the specified branch, and enter the specified directory to start building.

2.3. Method 3: Create through tar compressed package

docker build my.tar.gz

Problems encountered during the process and their solutions [PS]

【PS1】docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error running hook #1: error running hook: exit status 1, stdout: , stderr: Auto-detected mode as 'legacy'

  Error analysis: It may be that the current cuda device version of the computer does not match the torch version.

references

【1】Use Dockerfile to customize the image · Docker -- from entry to practice (docker-practice.github.io)

【2】How To Build Docker Image [Comprehensive Beginners Guide] (devopscube.com) 

【3】Quickly deploy frequently changing Docker images (feat. image size) | Miscellaneous blog (kimeuichan.github.io)  【4】Top 20 Dockerfile best practices for security – Sysdig

Guess you like

Origin blog.csdn.net/weixin_44649780/article/details/135100119