Introduction to Docker basic operations of Docker deployment, image and container

Posted by j_miguel_y on Wed, 02 Mar 2022 11:17:27 +0100

preface

The Docker introduction series of articles is based on the record of the whole process of practical operation of the video tutorial. Basically, operate according to this series of articles, and you can master the basic introduction skills of Docker.

1. Prepare the environment

Prepare the Linux server. CentOS 7, 64 bit, and system kernel version above 3.10 are recommended

[root@k8s-master ~]# uname -r
3.10.0-1160.53.1.el7.x86_64

2. Configure docker image source

The largest open image warehouse is Docker Hub:
https://hub.docker.com
Foreign image sources will be blocked. Alibaba cloud is recommended:

sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

3. Install docker

Docker version is divided into Ce (community version, free) and EE (enterprise version, security CE). We install the Community Edition.
Execute the following command to install:

yum install docker-ce -y

If you have previously installed docker s from other sources, there may be conflicts. If the installation fails, uninstall the old version first and execute the command:

sudo yum remove docker  docker-common docker-selinux docker-engine

Start the docker daemon

systemctl start docker

Set the docker daemon to start automatically after startup

systemctl enable docker

View docker version

docker --version
 perhaps
docker -v

View docker details

docker info

4. Operation of docker image

Online search docker image

docker search [Image name]

be careful:
Judge whether the image is official: if the official column shows OK, it is the official image.

About the kernel of docker:
The docker images of centos or ubuntu we downloaded do not have a system kernel, only the software and files such as the lib library needed to run. The kernel used by the image is the kernel of the host. Therefore, the kernel used by docker on the same host is the same, which is the kernel of the host. Therefore, if the host system is centos, it is impossible to use the feature of docker as ubuntu.

docker Download Image

docker pull [Image name]

Example:

docker pull centos

About docker image label: if you do not set the label manually, the image label defaults to latest:

[root@k8s-master ~]# docker images | grep centos
centos                                                            latest     5d0da3dc9764   5 months ago   231MB

View all images on the local host: docker images. Each mirror has a unique ID

[root@k8s-slave1 ~]# docker images
REPOSITORY                                           TAG       IMAGE ID       CREATED        SIZE
registry.aliyuncs.com/google_containers/kube-proxy   v1.23.4   2114245ec4d6   13 days ago    112MB
rancher/mirrored-flannelcni-flannel                  v0.16.3   8cb5de74f107   4 weeks ago    59.7MB
rancher/mirrored-flannelcni-flannel-cni-plugin       v1.0.1    ac40ce625740   5 weeks ago    8.1MB
centos                                               latest    5d0da3dc9764   5 months ago   231MB
registry.aliyuncs.com/google_containers/pause        3.6       6270bb605e12   6 months ago   683kB
kubernetesui/metrics-scraper                         v1.0.7    7801cfc6d5c0   8 months ago   34.4MB

Mirror export

docker save -o [Name of the image package] centos:latest

Note: the exported image is in the form of tar package by default.
For example:

docker save -o centos.tar centos:latest

Mirror import

docker load --input centos.tar
 perhaps
docker load < centos.tar

Delete image s and delete containers

docker rmi [Image name or image ID]

Force deletion:

docker rmi -f [Image name or image ID]

be careful:
If the image has started the container, the command cannot delete the image. The system will prompt that the image has started the container and give the ID of the container. The image can be deleted only after the container is deleted. Do not easily execute the forced deletion command in the production environment!!!

5. docker container operation

Start the container and output Hello world

docker run centos /bin/echo "Hello world"

be careful:
The Hello world output from this command is output from the container, not from the host. In this command mode, the container terminates after executing the echo task.

Start the container and name it

docker run --name [Custom container name] -t -i [Image name] /bin/bash

explain:
-t is tty, which refers to assigning a pseudo terminal- i is the standard input, because it needs to log into the container. When the command is executed, it will check whether the specified [image name] exists locally. If it does not exist, it will be downloaded from the public warehouse. Then start the container, assign a file system to the container and hang it on the image. Finally, assign an IP address to the container.
Example:

docker run --name mycentos -t -i centos /bin/bash

Container exit
Execute the exit command to exit the container, and the container is terminated.

Start the container again
Execute first

docker ps -a

Check the name or ID when the container was started before executing

docker start [Container name]

perhaps

docker start [container ID]

You can start the container just now.

Close the container

docker stop [Container name]

perhaps

docker stop [container ID]

View detailed container configuration

docker inspect [Container name]

Let a container automatically delete after running

docker run --rm [Image name] /bin/echo "hehe"

After executing this command, you can't see the container for outputting hehe just run by using the command docker ps -a

Delete container:

docker rm [Container name or container ID]

Topics: Docker Container