Project Scenario and Problem Description
This article is a simple instruction guide for using Docker, which can quickly realize the complete process of building a Docker image, starting, running, saving, exporting, and then importing into a new environment. Each command has a lot of parameters and knowledge. You can check for more detailed explanations by yourself. This article can be used by Xiaobai to quickly build and use images.
Automatically build images using Dockerfile
First, prepare a Dockerfile that contains information about the base image you want to use. Take the following Dockerfile as an example, the basic image is based on the ubuntu20.04 operating system, the cuda version is 11.2.0, the nvidia driver version is 450.80.02, and wget, git, vim, python3, python3-pip, ffmpeg and other commonly used tool, and create a working directory /app. For more references, see the reference link at the end of this article. Usually, some projects on GitHub will also provide the Dockerfile of the author's environment.
Dockerfile
FROM nvidia/cuda:11.2.0-devel-ubuntu20.04
RUN export DEBIAN_FRONTEND=noninteractive;apt-get update && \
apt-get install -y --no-install-recommends wget git vim python3 python3-pip ffmpeg
WORKDIR /app
After the Dockerfile is ready, execute the following command in the directory where the Dockerfile is located to build the image.
docker build -t MyUbuntu .
(Don't miss the last "." oh~)
The parameter -t is followed by the tag name (tag) of the image. By default, docker will find the Dockerfile from the build context, and you can also specify the location of the Dockerfile through the -f parameter.
After the image is successfully created, you can use docker images
the command to view the created image, including REPOSITORY, TAG, IMAGE ID and other information.
Take the above picture as an example. The newly built image REPOSITORY is ebxxx06, TAG is latest, and IMAGE ID is 4baxxxe34 (xxx is a mosaic).
docker run start image
Simple start command:
docker run -it <镜像的REPOSITORY:TAG>
like:
docker run -it MyUbuntu:latest
A more complicated startup command:
docker run -it -v /home/app/VeraCode:/app/VeraCode/ -p 8060:8060 --entrypoint="/bin/bash" --gpus all --name MyUbuntu2023 MyUbuntu:latest
Among them,
the parameter -v is followed by the local directory: the directory in docker, and the two directories are shared. The
parameter -p is followed by port mapping.
The parameter --gpus all is to enable the GPU
parameter --name is followed by the container name. Take
the last MyUbuntu :latest is the REPOSITORY:TAG of the image you want to start
After the container is started, it will directly enter the container, and the command exit
can exit the container.
docker exec to run the container
It is relatively simple to enter the running command again for the started container, but it is necessary to confirm whether the container is running.
Instructions docker ps
to view running containers
Instructions docker ps -a
to view all containers in history
Usually after the first run command is used to start the container and exit, the status of the container will be Exited Check the CONTAINER ID of the container to be started through the command, usually only the first three digits are needed. First, make sure the status of the container is Up, if
not docker ps -a
, you can use the command docker start <CONTAINER ID> 或 <CONTAINER ID的前三位>
to make the container in the running state, and then use the following command to run the container:
docker exec -it <CONTAINER ID> 或 <CONTAINER ID的前三位> /bin/bash
Take the above picture as an example, the command is:
docker exec -it 074 /bin/bash
docker commit save container
You can use docker commit
the command to package a container into an image, so that the environment you have installed when you run the container, such as the Python library installed by pip, etc., can be preserved, but the size of the exported image will be much larger than the initial base image. The command is as follows:
docker commit <CONTAINER ID> <镜像的REPOSITORY:TAG>
like:
docker commit 074 MyUbuntu:latest
docker save image export
The following two commands are available for local image export:
docker save -o MyUbuntu.tar <IMAGE ID>
or
docker save <IMAGE ID> > MyUbuntu.tar
like:
docker save -o MyUbuntu.tar 4baxxxe34
或
docker save 4baxxxe34 > MyUbuntu.tar
The above IMAGE_ID can also be replaced with <mirror REPOSITORY:TAG>
such as:
docker save -o MyUbuntu.tar MyUbuntu:latest
或
docker save MyUbuntu:latest > MyUbuntu.tar
docker load image import
You can transfer the exported image MyUbuntu.tar package to the docker host that needs to use the image package. The following two commands can be used to import the image:
docker load -i MyUbuntu.tar
or
docker load < MyUbuntu.tar
After importing, use docker images
the command to view the image imported into the new environment. To start and run, use the instructions introduced above.
Description:
1. The difference between docker save and docker export:
docker save saves all the information of the image - including historical information, use
docker export with docker load to export only the current information, use it with docker import
2. If you use the IMAGE ID when exporting the image, When importing a new environment, the REPOSITORY and TAG of the image may be none, and the following command can be used to rename the image:
docker tag <IMAGE ID> <新镜像名称>:<新镜像标签>
References, Supplementary Materials
Docker Dockerfile
There are several ways to create a Docker image
Docker image construction, import and export of Docker images and containers