docker installation, command Detailed

First, the environment

cat /etc/redhat-release 
uname -r

Second, the installation

yum install docker

Uninstall docker

yum remove docker-*

Third, start

#启动
systemctl start docker
#查看状态
systemctl status docker

Error may start reading this https://www.jianshu.com/p/a62ffb13ada6
script installation

[root@vultr ~]# vim installDocker.sh
#!/bin/bash
# install docker shell
# made by if
# 0. 关闭防火墙
echo "0.使用脚本关闭防火墙..."
sudo systemctl stop firewalld
sudo systemctl disable firewalld
# 1. 使用脚本自动安装
echo "1.使用脚本自动安装..."
curl -fsSL get.docker.com -o get-docker.sh
sudo sh get-docker.sh --mirror Aliyun
  
# 2.启动 Docker CE
echo "2.启动 Docker CE..."
sudo systemctl enable docker
sudo systemctl start docker
# 3.测试 Docker 是否安装正确
echo "3.测试 Docker 是否安装正确..."
docker run hello-world
# 4.添加镜像加速器
echo "4.添加镜像加速器..."
echo "{ 
  "registry-mirrors": [
    "https://5ajk0rns.mirror.aliyuncs.com"
    ]
}" > /etc/docker/daemon.json
# 5.重新启动服务
echo "5.重新启动服务..."
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl enable docker
[root@vultr ~]#sh installDocker.sh

Four, Docker basic commands

1. View the current mirror

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

2. Search Mirror
execution docker search centos searches image from the dockerhub

docker search 镜像名字

Here Insert Picture Description
3. Download image

The default is a foreign source, download will slow, mirroring suggested domestic warehouse

cat /etc/docker/daemon.json 
{
    "registry-mirrors": [ "https://registry.docker-cn.com"]
}
#重启docker
systemctl restart docker

Download image

docker pull centos #安装centos的镜像
docker pull nginx #安装nginx的镜像

4. Export mirror

docker save -o name Mirror Mirror

[root@localhost ~]# docker save -o nginx.tar nginx
[root@localhost ~]# ls
anaconda-ks.cfg  nginx.tar
#需要将docker导出为tar,后面为镜像名称

5. Mirror introduced

docker load --input nginx.tar  #使用input导入
docker load < nginx.tar    #使用重定向导入

6. Remove Mirror

docker delete can be used later docker rmi plus docker's ID

Tip: If the image has been created a container, it will not be deleted
Here Insert Picture Description

7. Delete container:

docker rm 容器名
或者使用
docker rm -f 容器名
删除所有容器
docker  rm -f `docker ps -qa

The second vessel will be prompted to turn it off

8. Create a Startup container

docker run Mirror

docker  container run -itd --name bs centos
#centos是镜像的名称,镜像的名称必须在选项的后面
#--name 容器的名称 
#-t 让docker分配一个伪终端
#-i 让docker的标准输入打开{input}
#-d 后台分配一个终端

Here Insert Picture Description
9. Check container

ps show is running -a container is not running the show

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
1fca4596401c        centos              "/bin/bash"         About a minute ago   Up About a minute                       bs
[root@localhost ~]#

Start container

docker start bs    #容器名启动
docker start bcededa4b82c #id号启动

Into the container

docker attach bs

Exit container

ctrl+d    #关闭退出容器
ctrl+p+q  #退出不关闭容器

Five, Docker Management Case

Tip: production scenarios is not in use docker attach into the vessel, we need to use nsenter this tool, this tool is included in util-linux package inside

[root@localhost ~]#yum install util-linux -y 
#Centos7默认最小化已经安装

We nsenter can enter the container, but nsenter by pid into the container, so we need to know the pid container. We can get to by docker inspect pid

[root@localhost ~]# docker start abcdocker
abcdocker
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
573227eb789b        centos              "/bin/bash"         25 minutes ago      Up 1 second                             abcdocker
[root@localhost ~]# docker inspect -f "{{ .State.Pid }}" abcdocker
8603
[root@localhost ~]# nsenter -t 8603 -m -u -i -n -p
[root@573227eb789b /]#

docker inspect -f {{.State.Pid}} container vessel name or id
# Each container has .State.Pid, so this command in addition to the id of the container we need to find according to docker ps -a, all the other fixed the format
nsenter --target found above process --net --pid # enter the command id --mount --uts --ipc will enter into the container

Meaning of the parameters after the explanation nsenter instruction process id:

* –mount参数是进去到mount namespace中 
* –uts参数是进入到uts namespace中 
* –ipc参数是进入到System V IPC namaspace中 
* –net参数是进入到network namespace中 
* –pid参数是进入到pid namespace中 
* –user参数是进入到user namespace中

We can get more parameters carried out by nsenter --help

We see the process into the container in
the boot process is nsenter

/ bin / bash is the process we run the container produced
-bash we use nsenter produced, so if we exit the container, the container will not quit, because -bash still running

Because every time you need to enter into the container that two commands, so we can write a script to get.
Script reads as follows:

[root@linux-node1 ~]# cat docker_in.sh 
#!/bin/bash
# Use nsenter to access docker
docker_in(){
  NAME_ID=$1
  PID=$(docker inspect -f "{{ .State.Pid }}" $NAME_ID)
  nsenter -t $PID -m -u -i -n -p
}
docker_in $1

Execution results are as follows

[root@linux-node1 ~]# chmod +x docker_in.sh 
[root@linux-node1 ~]# ./docker_in.sh abcdocker
[root@f8c8c8156e26 /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 Oct18 ?        00:00:00 /bin/bash
root         54      0  0 00:23 ?        00:00:00 -bash
root         67     54  0 00:23 ?        00:00:00 ps -ef
[root@f8c8c8156e26 /]#

We can not see into the container

[root@linux-node1 ~]# docker exec  abcdocker ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 Oct18 ?        00:00:00 /bin/bash
root         85      0  0 00:28 ?        00:00:00 ps -ef
[root@linux-node1 ~]# docker exec  abcdocker ls /
anaconda-post.log
bin
dev

Tip: You can use exec parameters, do not view the contents into the container

We can also use the exec into the docker container

[root@linux-node1 ~]# docker exec -it abcdocker /bin/bash

But it is better to use less exec, there might be some unexpected effects caused by container
original http://www.dhhblog.com/?p=539

Released three original articles · won praise 0 · Views 429

Guess you like

Origin blog.csdn.net/qq_43296870/article/details/103921685