Getting started with docker and docker common commands

Introduction to Docker

Docker is an open source application container engine, based on the Go language and open source following the Apache2.0 protocol.
Docker allows developers to package their applications and dependencies into a lightweight, portable container, and then publish it to any popular Linux machine, and it can also be virtualized.
Containers use the sandbox mechanism completely, and there will be no interfaces between them (apps similar to iPhone), and more importantly, the container performance overhead is extremely low.
Docker has been divided into CE (Community Edition: Community Edition) and EE (Enterprise Edition: Enterprise Edition) since version 17.03, we can use the Community Edition.
Insert picture description here
Docker official website: https://www.docker.com/


Automated packaging and publishing of Docker application scenarios Web applications.

Automated testing and continuous integration and release.

Deploy and adjust databases or other background applications in a service-oriented environment.

Compile or extend the existing OpenShift or Cloud Foundry platform from scratch to build your own PaaS environment.

Advantages of Docker
1. Simplify the program:
Docker allows developers to package their applications and dependent packages into a portable container, and then publish it on any popular Linux machine to achieve virtualization. Docker changes the way of virtualization, allowing developers to directly put their results into Docker for management. Convenience and speed are already the biggest advantage of Docker. Tasks that used to take days or even weeks can be completed in seconds under the processing of Docker containers.

2. Avoid choice phobia:
If you have choice phobia, you are still a senior patient. Then you can use Docker to package your entanglements! For example, Docker image; Docker image contains the operating environment and configuration, so Docker can simplify the deployment of multiple application instances. For example, Web applications, background applications, database applications, big data applications such as Hadoop clusters, message queues, etc. can all be packaged into a mirror deployment.

3. Save expenses:
On the one hand, the era of cloud computing has made it unnecessary for developers to configure expensive hardware in order to pursue results. Docker has changed the mindset that high performance is necessarily high in price. The combination of Docker and cloud makes cloud space more fully utilized. Not only solves the problem of hardware management, but also changes the way of virtualization.

Docker's architecture
Docker uses a client-server (C/S) architecture model, using remote APIs to manage and create Docker containers.
Docker containers are created through Docker images.
The relationship between containers and mirrors is similar to objects and classes in object-oriented programming.
Insert picture description here
Insert picture description here
Insert picture description here
The difference between Docker and virtual machines
Docker is a lightweight virtualization technology that has better performance than traditional virtual machines.
The following figure shows the architecture of the virtual machine:
Insert picture description here
server-represents a real computer.

Host OS-the operating system of the real computer, such as Windows, Linux

Hypervisor-virtual machine platform, simulated hardware, such as VMWare, VirtualBox

Guest OS-the operating system installed on the virtual machine platform, such as CentOS Linux

App-Application on the virtual machine operating system, such as nginx

The following figure shows the architecture of Docker:
Insert picture description here
server-represents a real computer.

Host OS-the operating system of the real computer, such as Windows, Linux

Docker Engine-a new generation of virtualization technology, does not need to include a separate operating system.

App-All applications are now running as Docker containers.

The obvious advantage of this architecture is that there is no need to provide hardware emulation for the virtual machine operating system. All applications work as Docker containers with better performance.

Centos7 install Docker

Docker adopts the timeline method as the version number since version 1.13, divided into community version CE and enterprise version EE.
The community version is free for individual developers and small groups to use, and the enterprise version will provide additional paid services, such as infrastructure, containers, and plug-ins that have been officially tested and certified.
The community version is released in two ways: stable and edge. The stable version is updated every quarter, such as 17.06, 17.09; the edge version is updated every month, such as 17.09, 17.10.

We usually use the community version is enough. So we install the community version;

We mainly refer to: https://docs.docker.com/install/linux/docker-ce/centos/ to install;

我们切换到root用户
1、Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 。
通过 uname -r 命令查看你当前的内核版本
 $ uname -r

2、使用 root 权限登录 Centos。确保 yum 包更新到最新。
$ yum update -y

3、卸载旧版本(如果安装过旧版本的话)
$ yum remove docker  docker-common docker-selinux docker-engine

4、安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
$ yum install -y yum-utils device-mapper-persistent-data lvm2

5、设置yum源
$ yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

6,安装最新版本的Docker
$ yum install docker-ce docker-ce-cli containerd.io -y -y

If the following problems
Insert picture description here
occur, there are two ways to do this:
1. There is no internet, try: ping www.baidu.com
If it shows no connection, it means there is no internet and you cannot use the yum command.

2. If the ping is successful, the yum command is still not available, indicating that the yum mirror is gone, so you have to download one to update.
After installing CentOS, you generally need to modify the yum source to get the ideal speed when installing and updating the rpm package. There are 163 sources and sohu sources that are relatively fast in China. Take 163 source as an example.

A, cd /etc/yum.repos.d
B, mv CentOS-Base.repo CentOS-Base.repo.backup
C, wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
D , Mv CentOS6-Base-163.repo CentOS-Base.repo
E, yum clean all and
finally use yum.

7,启动Docker并设置开机启动
$ systemctl start docker
$ systemctl enable docker

8,验证Docker
$ docker version

9,Docker HelloWorld测试;
$ docker run hello-world

The default remote warehouse https://hub.docker.com/ The network speed is sparse Alibaba
Cloud mirror warehouse under configuration
Insert picture description here

Docker HelloWorld operating principle analysis

Run docker run hello-world
local warehouse fails to find the mirror, then go to the remote warehouse to find and download the mirror;

Then we execute the command:

Hellowold came out. Let's analyze the execution principle and process in detail;
Insert picture description here
from left to right client client, Docker running host, remote warehouse;
docker build, pull, and run are respectively to build, pull, and run commands, and we will talk more about it later; in the
middle Docker host There are the main running thread of the Docker daemon and the Containers. Many instances can be run in the container. (The instance is instantiated from the Images image on the right). Images are image files stored locally, such as Redis and Tomat;
on the right ; It is the Registry mirror warehouse, the default remote mirror warehouse https://hub.docker.com/ is a foreign host, the download is slow and unstable, so we will configure it as the Alibaba Cloud warehouse mirror address later, which is stable and fast;

The process of executing docker run hello-world is shown in the following figure:
Insert picture description here

Alibaba Cloud Image Warehouse

The default remote warehouse of Docker is https://hub.docker.com/ For
example, if we download a larger thing, the turtle speed

Because it is a foreign host, similar to the Maven warehouse, it is slow, often delayed and damaged;
so we generally configure domestic mirrors, such as Alibaba Cloud, Netease Cloud, etc.; recommend Alibaba Cloud, which is stable;

The configuration steps are as follows:
1. Log in to the Alibaba Cloud Mirroring Service Center and obtain the mirroring address.
Enter the Alibaba Cloud Container Mirroring Service Address:
https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
Use your Taobao account password Login
Insert picture description here
here we get the mirror address;

2. Find the daemon.json file in the /etc/docker directory (create it if you don’t have it), and write the following content into
{ "registry-mirrors": ["https://xxxxxxx.mirror.aliyuncs.com"] }

3,重启daemon
systemctl daemon-reload

4. Restart the docker service
systemctl restart docker

5. Test

Docker basic commands

启动Docker
systemctl start docker

停止Docker
systemctl stop docker

重启Docker
systemctl restart docker

Start Docker
systemctl enable docker

View Docker summary information
docker info

View the Docker help document
docker --help

View Docker version information
docker version

Common commands for Docker images

1. docker images lists all the mirrors of the machine
docker images
lists all the mirrors of the machine
Insert picture description here
Insert picture description here
2. docker search searches for mirrors
and the search effect here is the same as https://hub.docker.com/;

OPTIONS optional parameters:
Insert picture description here
3, docker pull download image
docker pull image name: [TAG]

Note: Without TAG, the latest version latest is downloaded by default

4. docker rmi delete mirror
1, delete a single: docker rmi mirror name: [TAG]

If you do not write TAG, the latest version latest is deleted by default

When the container generated by the image is run again, an error will be reported and the deletion will fail;
Insert picture description here
we need to add -f to force the deletion

2. Delete multiple: docker rmi -f Mirror name 1: [TAG] Mirror name 2: [TAG]
separated by a space

3. Delete all: docker rmi -f $(docker images -qa)

Common commands for Docker containers

1. Docker creates and starts the container

docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

-Name="Container new name": specify a name for the container;
-i: run the container in interactive mode, usually used with -t or -d;
-t: reassign a pseudo input terminal for the container, usually with -i Use at the same time;
-d: run the container in the background, and return the container ID;
-P: random port mapping, the internal port of the container is randomly mapped to the port of the host
-p: designated port mapping, the format is: host (host) port: container port

Start a normal container: docker run --name alias image ID to
start an interactive container: docker run -it --name alias image ID to run a container, get the alias, run in interactive mode, assign a pseudo terminal, and enter the pseudo terminal;
Example:
docker run -it --name mycentos03 67fa590cfc1c

Note:
1. There is basically no way to start a normal container, there is no pseudo terminal, and there is not much value;
2. The way to start an interactive container, once the container is created, immediately enter the pseudo terminal
Insert picture description here
. Create and start the container in daemon mode
docker run -di --name alias image ID
example:
docker run -di --name mycentos02 67fa590cfc1c After
Insert picture description here
executing the command, the terminal is still on the host computer;
Insert picture description here
start the container and execute the /bin/bash command;
docker run -it --name alias image ID /bin/bash command

Port mapping;
docker run -it -p 8888:8080 tomcat
docker run -it -P tomcat

2. Docker lists the container
docker ps [OPTIONS]

OPTIONS description:
-a: Display all containers, including those that are not running.
-f: Filter the displayed content according to conditions.
--Format: Specify the template file of the return value.
-l: Display the recently created container.
-n: List the recently created n containers.
--No-trunc: Do not truncate the output.
-q: Silent mode, only the container number is displayed.
-s: Display the total file size.
Insert picture description here
docker ps View running containers
docker ps -a View all containers
docker ps -n 2 Display the two recently created containers
docker ps -f status=exited View stopped containers

3.Docker exits the container

exit The container stops exiting
ctrl+P+Q The container does not stop exiting
Insert picture description here

4. Docker enters the container

a, docker attach container ID or container name

Examples:
Docker The attach ce6343ee288f
not entered the stopped state container
You can not attach to a stopped container , start it first

b. Docker enters the container to execute the command
docker exec -it container name or container ID to execute the command
Example:
docker exec -it tomcat02 ls -l /root/webapp02

Operate the container directly and return to the host host terminal after execution;

We are generally used to start applications in containers such as tomcat nginx redis elasticsearch, etc.
Insert picture description here
5. Docker starts the container

docker start container ID or container name

Example:
docker start mycentos00
6. Docker restarts the container

docker restart Container ID or container name

Example:
docker restart f9cadea1a5e7
Insert picture description here
7. Docker stop container
docker stop Container ID or container name

Example:
docker stop 865b755cd0b2
Insert picture description here
brute force delete, kill the process directly (not recommended)
docker kill container ID or container name

8. Docker deletes the container

docker rm container ID

Insert picture description here
If you delete a running container, an error will be reported. If we need to delete it, we need to delete it forcefully;
force delete the docker rm -f container ID

Delete multiple containers
docker rm -f container ID1 container ID2 separated by spaces
Example:
docker rm 865b755cd0b2 ce6343ee288f

Delete all containers
docker rm -f $(docker ps -qa)

9, Docker container log

$ docker logs [OPTIONS] CONTAINER
Options:
--details Show more information
-f, --follow Track real-time logs
–since string Show logs since a certain timestamp, or relative time, such as 42m (ie 42 minutes)
–tail string show how many lines of log from the end of the log, the default is all
-t, --timestamps show timestamp
–until string show the log before a certain timestamp, or relative time, such as 42m (ie 42 minutes)
(understand above)

Recommended, simple and rude way, go directly to the docker container file;
specific location: /var/lib/docker/containers/,

Insert picture description here

Guess you like

Origin blog.csdn.net/xieminglu/article/details/103543535