Docker image management quick start

Introduction

Introduce how to use Docker to build an image, distribute it to the ECS server through Alibaba Cloud Image Service, and run the image.

background knowledge

Container technology
Container is a lightweight, operating system-level virtualization technology that allows us to run applications and their dependencies in the process of resource isolation. All necessary components required to run applications are packaged into a single image. This mirror can be reused. When the image is running, it runs in an independent environment and does not share the memory, CPU, or disk of the host operating system with other applications. This ensures that the processes inside the container will not affect any processes outside the container.

Mirror warehouse (Registry)
Docker's mirror storage center is usually called Registry. When you need to obtain your own private image, you first need to log in to the Registry, and then pull the image. After modifying the image, you can push the image back to the Registry again. Or generate an image locally through the function of Docker image construction, and then push it to the Registry.

Container image service ACR (Alibaba Cloud Container Registry) The
default instance version of Alibaba Cloud Container Registry provides basic container image services, including secure application image hosting capabilities, accurate image security scanning functions, stable domestic and foreign image construction services, and convenience The mirroring authorization function of, so as to facilitate users to manage the whole life cycle of mirroring.

Connect to the ECS server (If you haven't purchased it, you can experience ECS here )

1. Open the terminal tool that comes with the system.
Windows: CMD or Powershell.
MAC: Terminal.

  1. Enter the connection command ssh [username]@[ipaddress] in the terminal. You need to replace username and ipaddress with the login name and public network address of the ECS server created in Section 1. E.g:
ssh [email protected]

Insert picture description here
The command display results are as follows:

Insert picture description here
3. Enter yes, after agreeing to continue, you will be prompted to enter the login password. The password is the login password of the ECS of the created cloud service.

Build Docker service

Docker is an open source container engine for creating, managing, and orchestrating containers. It can easily create a lightweight, portable, and self-sufficient container for any application. This step will deploy a Docker service on ECS and configure DockerHub's image accelerator.

  1. Install Docker's dependent libraries.
yum install -y yum-utils device-mapper-persistent-data lvm2
  1. Add the software source information of Docker CE.
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  1. Install Docker CE.
yum makecache fast &&
yum -y install docker-ce
  1. Start the Docker service.
systemctl start docker
  1. Configure DockerHub image accelerator.
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF
  1. Restart the Docker service.
systemctl restart docker

Prepare application code and Dockerfile

This step will create a Golang-based HelloWorld code file and a Dockerfile file used to build the environment image required to run the Hello code in the workspace.

  1. Create a workspace.
mkdir -p /tmp/demo && cd /tmp/demo
  1. Create a HelloWorld code file in the workspace to monitor the HTTP service in the container environment and output the HelloWorld string.
cat > /tmp/demo/main.go << EOF
package main

import (
        "fmt"
        "net/http"
)

func main() {
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
                fmt.Fprintf(w, "Hello! World\n")
        })

        fmt.Println("start to serve...")
        http.ListenAndServe(":80", nil)
}
EOF
  1. Create a Dockerfile file in the workspace.
cat > /tmp/demo/Dockerfile << EOF
FROM golang:1.12-alpine

# change current working dir
WORKDIR /go/src/app

# copy main.go into /go/src/app
COPY . .

# go build and install the app
RUN go install -v ./...

# run the app by default
CMD ["app"]
EOF

Build the image locally and run the image

This step will guide you to use Docker basic commands to build and run the mirror

  1. Use the docker build command to build the image.
docker build . -t demo:v1

The command execution result is as follows:

Insert picture description here
Description: The
parameter. means to specify the current path as the build context, that is, the local path where the Dockerfile is located.
The parameter -t demo:v1 specifies the image name and label.

  1. Use the docker run command to run the image.
docker run -d -p 8000:80 demo:v1

The command execution result is as follows:

Insert picture description here

Description:

The parameter -d sets the container operation mode to background operation.
The parameter -p 8000:80 maps the network port used inside the container to the host, where 8000 is the host port and 80 is the port used inside the container.

  1. Use the curl tool to access the HelloWorld service in the container.
curl localhost:8000

The command execution result is as follows:
Insert picture description here

4. Use the docker rm command to delete the container.

docker rm -f $(docker ps -a | grep "demo:v1" | awk '{print $1}')

The command execution result is as follows:

Insert picture description here

Create a remote mirror warehouse

This step will guide you to use your Alibaba Cloud account to open the container image service and create an image warehouse. The default instance version of Alibaba Cloud Container Mirroring Service can be used for free. For usage restrictions, please refer to the Billing Instructions for Container Mirroring Service.

1. Use your Alibaba Cloud main account to log in to the container mirroring service console .

2. [Go to Activation]" Click [Set Registry Login Password] "Set Alibaba Cloud Docker Registry login password, and then click [OK].
3. In the container mirroring service console, click [Default Instance]> [Namespace]> [Create Namespace], fill in the command space name in the [Create Namespace] pop-up box, and then click [OK].
Insert picture description here

  1. Click [Local Warehouse]> [Create Mirror Warehouse]. In the mirror warehouse list, select the mirror warehouse created in the previous step, and click [Manage] to view the mirror warehouse details.

Push mirror

In this step, the local mirror is pushed to the remote warehouse, and the specified version of the mirror in the remote warehouse is run. Please replace the remote mirror address involved in the command in this step with the public network address of the mirror warehouse created in step 6.

1. Execute the following command to log in to the Alibaba Cloud Docker Registry.

docker login --username="用户名" registry.cn-hangzhou.aliyuncs.com

Note: Please replace the user name in the following command with your full name of your Alibaba Cloud account, and then press Enter and enter the password of the remote mirror warehouse. The password is the password set when you activate the service in step 6.

Please log in to the Alibaba Cloud User Center to view your main account user name.
Insert picture description here
The command execution result is as follows:

Insert picture description here
2. Mark the local mirror and put it into the remote warehouse.

docker tag demo:v1 registry.cn-hangzhou.aliyuncs.com/space_test/demo:v1

3. Push the local mirror to the remote warehouse.

docker push registry.cn-hangzhou.aliyuncs.com/space_test/demo:v1

The command execution result is as follows:
Insert picture description here

  1. Pull the remote mirror of the specified version.
docker pull registry.cn-hangzhou.aliyuncs.com/space_test/demo:v1

The command execution result is as follows:
Insert picture description here

  1. Run the pulled remote mirror.
docker run -d -p 8000:80 registry.cn-hangzhou.aliyuncs.com/space_test/demo:v1

The command execution result is as follows:
Insert picture description here

  1. Access the HelloWorld service.
curl localhost:8000

The command execution result is as follows:
Insert picture description here

Guess you like

Origin blog.51cto.com/14981263/2678339