Docker-Docker basic operations

1. Basic operations of image (image)

1. Mirror related commands

2. Case - pull and view images

Requirement: Pull an nginx image from DockerHub and view it

1) First go to the image warehouse to search for the nginx image

Dockericon-default.png?t=N7T8https://hub-stage.docker.com/_/nginx

2) According to the viewed image name, pull the image you need through the command: docker pull nginx

docker pull nginx

3) View the pulled image through the command: docker images

docker images

3. Case - save and import images

Requirements:Use docker save to export the nginx image to disk, and then load it back through load

1) Use the docker xx --help command to view the syntax of docker save and docker load

For example, to view the save command usage, you can enter the command:

docker save --help

        Command format:

docker save -o [保存的目标文件名称] [镜像名称]

2) Use docker save to export the image to disk and run the command:

docker save -o nginx.tar nginx:latest

3) Use docker load to load the image

        First delete the local nginx image:

docker rmi nginx:latest

        ​​​​Then run the command to load the local file:

docker load -i nginx.tar

2. Basic operations of containers

Containers protect three states:

  • Running: The process is running normally

  • Pause: The process is suspended, the CPU is no longer running, and the memory is not released.

  • Stop: The process is terminated and the memory, CPU and other resources occupied by the process are recycled.

 in:

- docker run:创建并运行一个容器,处于运行状态
- docker pause:让一个运行的容器暂停
- docker unpause:让一个容器从暂停状态恢复运行
- docker stop:停止一个运行的容器
- docker start:让一个停止的容器再次运行

- docker rm:删除一个容器

 1. Case - Create and run a container

        ​​​​Commands to create and run nginx containers:

docker run --name containerName -p 80:80 -d nginx

命令解读:

    - docker run :创建并运行一个容器
    - --name : 给容器起一个名字,比如叫做mn
    - -p :将宿主机端口与容器端口映射,冒号左侧是宿主机端口,右侧是容器端口
    - -d:后台运行容器
    - nginx:镜像名称,例如nginx

        The -p parameter here maps the container port to the host port.

        By default, the container is an isolated environment. If we directly access port 80 of the host, we will definitely not be able to access nginx in the container.

        Now, associate the container's 80 with the host's 80. When we access the host's port 80, it will be mapped to the container's 80, so that we can access nginx;

 2. Case - enter the container and modify the file

Requirements: Enter the Nginx container, modify the content of the HTML file, and add "Welcome to Chuanzhi Education"

Tips: You need to use the docker exec command to enter the container.

Walk

1) Enter the container. The command to enter the nginx container we just created is:

docker exec -it mn bash
命令解读:

    - docker exec :进入容器内部,执行一个命令

    - -it : 给当前进入的容器创建一个标准输入、输出终端,允许我们与容器交互

    - mn :要进入的容器的名称

    - bash:进入容器后执行的命令,bash是一个linux终端交互命令

2) Enter the directory where nginx’s HTML is located /usr/share/nginx/html

        An independent Linux file system will be simulated inside the container, which looks like a Linux server: nginx's environment, configuration, and running files are all in this file system, including the html file we want to modify.

View the nginx page on the DockerHub website and you can know the location of the nginx html directory./usr/share/nginx/html

We execute the command and enter the directory:

cd /usr/share/nginx/html

3) Modify the content of index.html

There is no vi command in the container and cannot be modified directly. We use the following command to modify it:

sed -i -e 's#Welcome to nginx#世界欢迎您#g' -e 's#<head>#<head><meta charset="utf-8">#g' index.html

 Visit your own virtual machine address in the browser, for example, mine is:http://192.168.37.121, you can see result.

3. Basic operations of data volumes (volume)

        In the previous nginx case, when modifying the nginx html page, you need to enter nginx. And because there is no editor, modifying files is also very troublesome. This is the consequence of the coupling between the container and the data (files in the container). To solve this problem, the data must be decoupled from the container, which requires the use of data volumes.

1. What is a data volume?

Data volume (volume) is a virtual directory that points to a directory in the host file system.

        Once the data volume is mounted, all operations on the container will be applied to the host directory corresponding to the data volume. In this way, when we operate the /var/lib/docker/volumes/html directory on the host, it is equivalent to operating the /usr/share/nginx/html directory in the container.

2.3.2.Dataset operation commands

        The basic syntax of data volume operations is as follows:

docker volume [COMMAND]

docker volume命令是数据卷操作,根据命令后跟随的command来确定下一步的操作:

- create 创建一个volume
- inspect 显示一个或多个volume的信息
- ls 列出所有的volume
- prune 删除未使用的volume
- rm 删除一个或多个指定的volume

3. Create and view data volumes

Requirement: Create a data volume and check the directory location of the data volume on the host machine

① Create data volume

docker volume create html

② View all data

docker volume ls

③ View data volume details volume

docker volume inspect html

4. Mount the data volume

When we create a container, we can use the -v parameter to mount a data volume to a directory in a container. The command format is as follows:

docker run \
  --name mn \
  -v html:/root/html \
  -p 8080:80
  nginx \

        The -v here is the command to mount the data volume:

`-v html:/root/htm` :把html数据卷挂载到容器内的/root/html这个目录中

5. Case - Mount data to nginx

Requirement: Create an nginx container and modify the index.html content in the html directory in the container

Analysis: In the last case, we entered the nginx container and already knew the location of nginx’s html directory /usr/share/nginx/html. We need to mount this directory Load it to the html data volume to facilitate the manipulation of its contents.

Tip: Use the -v parameter to mount the data volume when running the container

step:

① Create a container and mount the data volume to the HTML directory in the container

docker run --name mn -v html:/usr/share/nginx/html -p 80:80 -d nginx

② Enter the location of the html data volume and modify the HTML content

# 查看html数据卷的位置
docker volume inspect html
# 进入该目录
cd /var/lib/docker/volumes/html/_data
# 修改文件
vi index.html

 If you like it, please give it a follow!

Guess you like

Origin blog.csdn.net/qq_45672041/article/details/134897417