Docker getting started process (creating containers to building images)

Docker getting started process (creating containers to building images)

We often encounter code that runs on our own computer, but inexplicable problems occur when running on other people's computers. This is mostly due to the fact that the code is not adapted to the climate and the software versions and dependent libraries of different computers. Different, the use of Docker can solve this problem well. To put it simply, Docker can package the code and the environment in which the code runs to build a container, and you can run the code in this container. Also note that Docker can also be used for virtual machines.

1. Docker installation and operation commands

1. Docker introduction, installation and image accelerator

Docker is an open source application container engine. The environment is packaged and portable, and different containers are isolated from each other. The purpose is to solve the problem of cross-environment migration of software. There are many tutorials on the Internet for the installation of Docker. You can refer to this link. There are also some introductions in it. You may need to surf the Internet scientifically.

Docker has three basic concepts, warehouse, container and image. The specific details are easy to find and can be found in the above link. To put it simply, ① Image (Image), the image can be regarded as an independent minimum file system, such as Ubuntu: 18.04, the image name is Ubuntu, and the tag version number after the colon is. ②Container (Container), the container is built from a long-term image. A good metaphor is that the image is regarded as a defined class, and the container is regarded as based on < /span>. ③You can see many image repositories in the warehouse, including the official docker hub warehouse, as well as those established by companies and private individuals. Generally, when we pull the image without setting the image pull address, we will pull it from the official one by default, and install the software from the source. The same principle applies. Object instantiated by a class

Usually it is slow for us to pull official images, or even very slow, so we usually configure image accelerators. There are various accelerators such as NetEase, Alibaba, and the University of Science and Technology of China. You can also configure multiple ones. Take the Alibaba accelerator as an example and search < /span> and complete the configuration according to the prompts. . Image Accelerator, you will seeImage Tool, left When you open the Container Image Service, register and log in, search on the consoleAlibaba Cloud

2. Docker operation commands

①Docker service related commands:

Service-related commands will be used less frequently. If you have a little understanding, just set up docker to start automatically at boot.

systemctl  start docker  #启动docker
systemctl  stop docker   #停止docker
systemctl  restart docker  #重启docker
systemctl  status docker  #查看状态
systemctl  enable docker  #开机自启docker

②Docker image related commands

The most commonly used mirror commands are to view, pull and delete images.

docker images        #查看镜像
docker images -q     #查看镜像id
docker search  name   #网上搜索镜像,可以搜好再拉取
docker pull redis:5.0    #拉取镜像例子
docker rmi resdis:5.0 /image id  #删除镜像例子
docker rmi `docker images -q`   #删除所有镜像

③Docker container related commands

After pulling the image, you need to create a container based on the image. The container has two states, one is running up and the other is exiting. You need to know some parameter descriptions when creating the container.

Insert image description here

If it is created using -t, it will directly enter the container. If it is created by -d, docker ps ​​You can see that the container is running, and you need to docker exec -it​ and add /bin/bash​ into this container. After that, you can operate in the container or delete the container.

docker ps  #查看正在运行的容器
docker ps -a  # 查看所有容器,up 运行  exit退出
docker run -it --name=c1  centos:7 /bin/bash  # (bash 交互式的shell,默认也是这个)交互式的
docker run -id --name=c2 centos:7  #守护容器 后台运行需要
docker exec -it c2 /bin/bash  #进入这个容器 ,需要bash   守护容器进入exit不会关闭,需要stop
docker stop  c1     #停止一个容器
docker start c1     #开启一个容器
docker rm c1        #不能删除正在运行的容器
docker inspect c1   #查看容器信息

2. Data volume and mirror structure

1. Data volume configuration

Some experimental data results generated in Docker will disappear when the container is deleted. At this time, the data volume needs to be mounted.

A container can mount multiple data volumes, and the data volume is a directory of the host machine . The essence is: the container mounts an external data volume, the modified and generated data is placed in the data volume, and both parties will map it synchronously. In this case, the data generated by the container Data can be placed in the host directory, and data can also be exchanged between containers. The container is deleted, but the data volume contents are still there.

In addition, you can also configure the data volume container to let other containers map this data volume container, which is not used too much.

for example:

docker run -it --name=c1  -v ~/data:/root/data centos:7 /bin/bash #左边宿主机目录:右边容器目录
挂载多个就多个-v,容器加root,相当于~根目录,相当于一个映射文件夹,但是数据卷的内容不会随着容器的删除而消失

#配置数据卷容器
docker run -it --name=c3 -v /volume centos:7 
容器内部创建一个volume目录,对应宿主机目录下的一个文件,可以通过docker inspect 查看mounts
docker run -it --name=c1 --volumes-from c3 centos:7 
c1 下也会创建一个volume目录,都是相互映射的,对应宿主机目录下的一个文件

2. Mirror construction concept

Before building an image, understand some concepts. The file system of Linux is composed of bootfs and rootfs. In short, the bootfs of different Linux distributions are the same, the rootfs is different, and the bootfs of windows and Linux are different. The image can be regarded as a file system, which is stacked layer by layer. When building a container, the host's bootfs will be used. Therefore, windos and Linux Images are not interoperable. The bottom layer of the image is boofs, and the second layer is rootfs, such as ubuntu, centos, etc. You can continue to stack later. Note that the previous layer is superimposed on the next layer. An image can be placed on top of another image. The image below is called the parent image, and the bottom image becomes the base image. When starting a container from an image, Docker will > loads a read-write file system as a container, so the size of some images with high layers will be larger. The top level

Insert image description here

1.What is the essence of Docker image?

Answer: It is a hierarchical file system.
2. Why is a centos image in Docker only 200MB, while an iso file of the centos operating system is several G?
Answer: Centos’ iso image file contains bootfs and rootfs, while docker’s certos image reuses the operating system’s bootfs, with only rootfs and other image layers.
3. Why is a tomcat image in Docker 500MB, but a tomcat installation package is only more than 70 MB?
Answer: Since the image in docker is layered, although tomat is only more than 70 MB,Multi MBIt needs to depend on the parent image and the base image. The size of all externally exposed tomcat images is 500

3. Mirror construction method

①Build an image based on the container

The mainstream method is to create an image using a dockerfile. This uses the commit command to convert the container into an image, compress the image, save, transfer and load the image, and create a container based on the image.

docker commit contanier_id  mya:1.0  # 容器id  和自定义的镜像名称
docker images
docker save -o aa.tar mya:1.0  #将镜像保存为压缩包,可以给别人
docker load -i aa.tar            #别人拿到了load一下就可以导入镜像了
docker images
docker run -it --name=myaa mya:1.0 bash  #根据镜像创建容器

②Create a container based on dockerfile

Dockerfile is a text file that contains instructions one by one. Each instruction builds a layer based on the basic image, and finally builds a new image. Generally, when you encounter a specific command, just search it. There are many dockerfile keywords on the Internet. ReferenceLink 2, several key ones:

FROM  基础镜像
ADD   会解压缩
WORKDIR  进入容器停留的目录
CMD  启动容器的时候运行 (一般有的默认/bin/bash)
RUN   启动容器前运
之后在已有镜像上去下载 安装一些东西,build为一个自己的镜像
docker build -f dockerfile  -t myimg:1.0 . 会加一个.  跟目录有关

3. Docker compose and private warehouse

Let me briefly explain this. In the actual application process, there is more than one container. How to manage multiple containers and create containers based on images. At this time, docker compose is required to manage multiple containers in batches according to certain rules. Write docker-compos.yml to manage the construction of multiple containers. The startup command is docker-compose up. (Run docker-compose up under the docker-compose.yml file (add -d to run the background daemon)) to start multiple running containers.

You can put some of your own images in the private warehouse. There are many of them online. Here is a concept. To build a private warehouse, you can upload your own images and pull your own images. The general process is to download a private warehouse image and create a private warehouse container based on this image. In fact, it means adding an IP address and port number.

Insert image description here

Insert image description here

3. Build ROS container in Docker

This is related to your own research, so make a complete record.

1. Pull the image and build the container

sudo docker pull osrf/ros:melodic-desktop-full​, ReferenceConnection 3, Complete comparison.

If it contains a ros-core image, it will be about 400M. If it contains a complete set of destktop-full versions such as rviz, gazebo, etc., it will be about 5G, and the space occupied is still considerable.

After that, you can refer to the figure below to build the container, use -v to mount the data volume, and set the shared display to:The visual interface running such as rviz in the container will be on the host Show see.

Insert image description here

The host turns on xhost to enable the host to receive the display requirements of other clients. Run xhost +

When docker creates a container, just set the xserver mounting address as a parameter.

-e DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix

After running, the rviz displayed by the container can be displayed on the host. These are fixed commands.

-v /tmp/.X11-unix:/tmp/.X11-unix #Mount the local display service port into the container

-e DISPLAY=unix$DISPLAY # Modify environment variable DISPLAY

-e GDK_SCALE # Environment variables related to display effects

-e GDK_DPI_SCALE # Same as above

Generally mount a code directory and a data generation directory. After turning on the data volume and display settings, create and enter the container, you can modify the files and code on the host, automatically mapped into the container.

One thing to note is that the data generated in the container will be under the root user. At this time, you can use sudo chmod -R 777 文件目录​ to modify the permissions and use sudo chown -R 用户名 文件目录​ Go to modify owner

2. Possible errors and cautions

mistake:

no such file or directory): exec: “nvidia-container-runtime”:
executable file not found in $PATH: : unknown.

Install nvidia-docker

Strategy

curl https://get.docker.com | sh && sudo systemctl --now enable dockerdistribution= ( . / e t c / o s − r e l e a s e ; e c h o (. /etc/os-release;echo (./etcaxis/release ;echoID$VERSION_ID)

curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -

curl -s -L [https://nvidia.github.io/nvidia-docker/ d i s t r i b u t i o n / n v i d i a − d o c k e r . l i s t ] ( h t t p s : / / n v i d i a . g i t h u b . i o / n v i d i a − d o c k e r / distribution/nvidia-docker.list](https://nvidia.github.io/nvidia-docker/ distribution/nvidiadocker.list](https://nvidia.github.io/nvidiadocker/distribution/nvidia-docker.list) | sudo tee /etc/apt/sources.list.d/nvidia-docker.list

sudo -E apt-get update

sudo -E apt-get install -y nvidia-docker2

For the container created by the ros image, there is a ros_entrypoint.sh file. In order to ensure that the ros environment can run normally in the container, it needs to be executed in the root directory:

source ros_entrypoint.sh, essentially the source environment variable

After that it’s normal operation…

Guess you like

Origin blog.csdn.net/qq_43377917/article/details/131745891