Introduction to Container Technology 1: A Quick Start to Docker Image Management

First. Build Docker service


Docker is an open source container engine for creating, managing and orchestrating containers, making it easy to create a lightweight, portable, self-sufficient container for any application. This step will deploy a Docker service on the server and configure the image accelerator of DockerHub.
1. Enter the following command to install Docker's dependencies.

yum install -y yum-utils device-mapper-persistent-data lvm2
2. Add the software source information of Docker CE.

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3. Install Docker CE.

yum makecache fast && yum -y install docker-ce

4. Start the Docker service.

systemctl start docker

5. Configure the DockerHub image accelerator.

tee /etc/docker/daemon.json <<- 'EOF'
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF

6. Restart the Docker service.

systemctl restart docker

Second. Write Dockerfile


This step will create a Golang-based HelloWorld code file and a Dockerfile file for building the environment image required to run the Hello code in the workspace .
1. Create a workspace.


mkdir -p /tmp/demo && cd /tmp/demo

2. 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 ")
})

fmt.Println("start to serve...")
http.ListenAndServe(":80", nil)
}
EOF

Third. Create a Dockerfile 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


3. Build the image locally and run the image
This step will guide you through building and running the mirror using basic Docker commands
1. Use the docker build command to build the image.


docker build . -t demo:v1
The result of running the command is as follows:



illustrate:
•1. Parameter. Indicates that the current path is specified as the build context, that is, the local path where the Dockerfile is located.
•2. The parameter -t demo:v1 specifies the image name and label.


2. Run the image using the docker run command.


docker run -d -p 8000:80 demo:v1

The result of running the command is as follows:


illustrate:
•1. The parameter -d sets the container running mode to background running.
•2. 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.


3. Use the curl tool to access the HelloWorld service in the container.


curl localhost:8000

The result of running the command is as follows:

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

docker rm -f $(docker ps -a | grep "demo:v1" | awk '{print $1}')
The result of running the command is as follows:


Fourth. Remote mirror warehouse operation


1. Push the image
This step pushes the local image to the remote repository and runs the specified version image in the remote repository.
1. Execute the following command to log in to the Docker Registry.

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

2. Mark the local image and put it in the remote repository.

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

3. Push the local image to the remote repository.

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

The result of running the command is as follows:

4. Pull the remote mirror of the specified version.

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

The result of running the command is as follows:

5. Run the pulled remote image.


docker run -d -p 8000:80 registry.cn-hangzhou.aliyuncs.com/space_test/demo:v1



Copyright statement: The content of this article is contributed by Alibaba Cloud's real-name registered users. The copyright belongs to the original author. The Alibaba Cloud developer community does not own the copyright and does not assume the corresponding legal responsibility. For specific rules, please refer to the " Alibaba Cloud Developer Community User Service Agreement " and " Alibaba Cloud Developer Community Intellectual Property Protection Guidelines ". If you find any content suspected of plagiarism in this community, fill out the infringement complaint form to report it. Once verified, this community will delete the allegedly infringing content immediately.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00