Docker learning under windows

1. Why study

        Every time we learn something, we must learn it based on a certain requirement. As we all know, the most troublesome part of software development is the environment configuration. If the development is good, it will be difficult to deploy problems. Therefore, in order to ensure that the development, testing, and deployment environments are consistent, and For efficient deployment, container technology is chosen instead of VM, and Docker is an open source project based on Linux container technology. Its mantra is: "build once, run everywhere", with light weight, speed, active community, and high scalability. Run everywhere, from house helping to moving buildings

(1) Function:

        Solve the problem of operating environment and configuration, facilitate continuous integration and help the overall release of container Unreal technology

(2) Specific scenarios:

        Faster Application Delivery and Deployment

        Faster upgrades and scaling

        Easier system operation and maintenance

        Higher Computing Resource Utilization

Two, docker composition

(1) Mirror image: image

        A read-only template. Mirroring can be used to create Docker containers. One mirroring can create many containers. The docker image file is similar to the java class template, and the docker container instance is similar to the new instance object in java.

(2) Container: container

        It is a running instance created with mirroring, which provides a standard isolated running environment for mirroring, and can be started, started, stopped, and deleted. Similar to classes and instance objects in java, images are static definitions, and containers are entities when images are running.

(3) Warehouse: repository

        It is the place where image files are stored centrally. Similar to the github repository, where various git projects are stored. Warehouses are divided into public warehouses and private warehouses. The largest public warehouse, Docker Hub, stores a large number of images for users to download. Domestic public warehouses include Alibaba Cloud and NetEase Cloud, etc.

3. Installation

        Download the path, select the corresponding operating system—window10 system, Developers - Docker , double-click the downloaded exe file, the first startup will create a virtual machine, wait for a few minutes.

        Check whether the installation is successful: You can enter docker -v in the terminal cmd to check the docker version.

         If there is an installation failure during the docker desktop installation process, then re-run the corresponding steps in the second step and enable hyper-v, then restart the computer, and then install the docker desktop.

4. Simple commands

#启动docker
systemctl start docker
#停止docker
systemctl stop docker
#重启docker
systemctl restart docker
#查看docker状态
systemctl status docker
#查看docker信息
docker info

Mirror command:

#拉取镜像
docker pull 镜像名
docker pull 镜像名:tag

#查看镜像
docker images

#删除镜像
docker rmi -f 镜像名/镜像ID   #删除一个
docker rmi -f 镜像名/镜像ID 镜像名/镜像ID 镜像名/镜像ID  #删除多个 其镜像ID或镜像用用空格隔开即可 
docker rmi -f $(docker images -aq)  #删除全部镜像  -a 意思为显示全部, -q 意思为只显示ID

#搜索镜像
docker search 镜像名
docker search --filter=STARS=9000 mysql 搜索 STARS >9000的 mysql 镜像

#运行镜像
docker run 镜像名
docker run 镜像名:Tag

#保存镜像
docker save 镜像名/镜像ID -o 镜像保存在哪个位置与名字

Container command:

#运行nginx容器
# -it 表示 与容器进行交互式启动 -d 表示可后台运行容器 (守护式运行)  --name 给要运行的容器 起的名字  /bin/bash  交互路径
docker run -it -d --name 要取的别名 镜像名:Tag /bin/bash 
docker run -d --name test-nginx -p 3000:80 nginx

# 查看正在运行的容器
docker ps

# 查看所有创建过的容器(运行或者关闭)
docker ps -a

# 停止容器
docker stop [container]

# 启动容器
docker start [container]

# 删除容器
docker rm -f [containerid]

# 查看后台运行的日志
docker logs [containe]

#进入容器的shell
docker container exec -it [containe] /bin/bash

5. Case of pulling image

1. Pull the nginx image

docker pull nginx

2. Start the nginx mirror

docker run -d --name test-nginx -p 3000:80 nginx

3. Open localhost:3000 to access the familiar nginx page

 

Beginners will definitely wonder what is nginx?

        Nginx (pronounced "engine-x") is an open source reverse proxy server for the HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer, HTTP cache, and web server (origin server). The nginx project started with a strong focus on high concurrency, high performance and low memory usage. It is licensed under a BSD-like Clause 2 license, and it runs on Linux, BSD variants, Mac OS X, Solaris, AIX, HP-UX, and other *nix flavors. It also provides a proof of concept port to Microsoft Windows.

6. Problems encountered

1. When pulling the image, you may encounter the following error message:

The main reason for this problem is that docker desktop is not started

 2. After starting, the following pop-up will pop up (if not, ignore it, if yes, follow the steps below to continue), when you click restart, it always fails, because you need to update WLS 2

 

It is recommended that Windows upgrade WSL 1 to WSL2_Xeon-Shao's blog-CSDN blog_wsl1 upgrade to wsl2 learning,

It can also be performed according to the manual installation steps of the old version of WSL | Microsoft Docs steps

(It is recommended to execute one side first after reading the above two recommended solutions)

3. Download wsl_update_x64.msi, double-click to install

4. Enter wsl-set-default-version-2 on powershell

 After completing this step, click Restart and wait for a while to start docker

 5. Enter docker pull nginx and successfully pull the nginx image

 7. Build a mirror with Dockerfile (windows environment)

(1) Case 1: Output a hello Docker

1. Create a new docker-test folder on the desktop, and place a Dockerfile in it. The contents of the Dockerfile are:

#指定基础的镜像为nginx。
FROM nginx:latest 
#: 指定镜像创建者TTT和联系方式。
MAINTAINER TTT [email protected]
#容器内部执行的命令(在index.html中输出‘hello, Docker!’)。
RUN echo '<h1>hello, Docker!</h1>' > /usr/share/nginx/html/index.html

2. Enter cmd in the directory address bar corresponding to the docker-test folder, open the command prompt window, and enter the command

# 生成一个名为 nginx 标签为0.0.1 的镜像 ,注意最后还有一个 . 
docker image build -t nginx:0.0.1 .

As shown below

 3. View the mirror image, there is an additional mirror image generated by yourself

 4. Start the container

# 根据刚生成的镜像 启动容器
docker run -d --name test-nginx1 -p 3001:80 nginx:0.0.1

5. Accessing locahost:3001 is no longer the default nginx access page

Explain the parameters here

  • The -p parameter means that port 80 of the container is mapped to port 3001 of the machine
  • -d Daemon running (suitable for running applications and services)
  • --name container name if not specified is a random name

 (2) Case 2, output an index.html

1. Add an index.html file in the folder of case one: create a new index.html in the docker-test folder

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Hello World</title>
  </head>
  <body>
    <h1>Hello World!</h1>
  </body>
</html>

2. Modify the Dockerfile file, the focus is: COPY index.html /usr/share/nginx/html/index.html

# 声明基于 nginx 最新的镜像 这里说一下镜像名:后面的是标签 默认是latest
FROM nginx:latest
# 把刚才 index.html 复制到 nginx 的 html 路径去
COPY index.html /usr/share/nginx/html/index.html
# 声明暴露80端口,要是运行不成功就把这句去掉
EXPOSE 80

3. Run the command. Note that the previous container and image should be deleted here (it’s time to test the command to delete the container and image, stop the container first, delete the container and then delete the image ), or you can not delete the image generated in case 1. Change the following Change the image label and change the name when starting the container

#  生成一个名为 nginx 标签为0.0.1 的镜像 ,注意最后还有一个 .
docker image build -t nginx:0.0.1 .
# 根据刚生成的镜像 启动容器
docker run -d --name test-nginx1 -p 3001:80 nginx:0.0.1

4. Visit locahost:3001

 Of course, during real development, it is definitely impossible to modify it like this. For example, the previous port mapping can also map the internal directory of the container to achieve shared sharing. Volume can be used. Learn here first, and Volume will be later learn and update

Guess you like

Origin blog.csdn.net/m0_55173487/article/details/126212900