Some basic instructions of docker

  • 2021-10-27 09:56:39
  • OfStack

Directory 1 Some basic instructions
1. View the container situation of the current machine. 2. Download or pull an image. 3. Start, stop and restart the container 4. View the process in the container 5. Execute Command 6 inside the container. Delete the container

In recent work, MySQL and MongoDB have less content in operation and maintenance. The main work content will frequently contact the related contents of docker and k8s, so I am ready to write this piece of content.

In the previous article, the process of deploying MySQL on docker was introduced. Here is a link for easy review:

Deploying an MySQL instance on docker

In the above article, we have already talked about what docker is, the difference between docker and virtual machines, how to install it and how to use it basically. Let's take a look at other aspects of knowledge.

1 Some basic instructions

1. View the container status of the current machine

Typically, we can use the docker ps command to see which containers are currently running on the machine, as follows:


[root@VM-16-13-centos ~]# docker ps 
CONTAINER ID        IMAGE                                                        COMMAND                  CREATED             STATUS              PORTS               NAMES
7a57ee5286e8        kubeguide/tomcat-app:v1                                      "catalina.sh run"        8 days ago          Up 8 days                               k8s_myweb.7c32387a_myweb-8gmc1_default_b44279ec-48b9-11eb-86e4-5254002dbd85_ec48ef90
b2ad9bea8d0d        kubeguide/tomcat-app:v1                                      "catalina.sh run"        8 days ago          Up 8 days                               k8s_myweb.7c32387a_myweb-60r22_default_b4426b29-48b9-11eb-86e4-5254002dbd85_28dd55c9
4f8f6ea37578        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           8 days ago          Up 8 days                               k8s_POD.24f70ba9_myweb-8gmc1_default_b44279ec-48b9-11eb-86e4-5254002dbd85_2d2aa42e
3d198ee60f2a        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           8 days ago          Up 8 days                               k8s_POD.24f70ba9_myweb-60r22_default_b4426b29-48b9-11eb-86e4-5254002dbd85_416acda7
94332658780b        busybox                                                      "sh -c 'tail -f /l..."   6 weeks ago         Up 6 weeks                              k8s_busybox.5057389f_volume-pod_default_13702698-2b17-11eb-86e4-5254002dbd85_a553d58b
f11f11999c42        tomcat                                                       "catalina.sh run"        6 weeks ago         Up 6 weeks                              k8s_tomcat.aeb855f2_volume-pod_default_13702698-2b17-11eb-86e4-5254002dbd85_aa729e3d
d2ec1526079f        kubeguide/redis-master                                       "redis-server /etc..."   6 weeks ago         Up 6 weeks                              k8s_redis.deb7361f_redis-php_default_f70a055b-2b18-11eb-86e4-5254002dbd85_23135019
5b83d7645b1f        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           6 weeks ago         Up 6 weeks                              k8s_POD.3fe22e5d_redis-php_default_f70a055b-2b18-11eb-86e4-5254002dbd85_c418f108
3b7634ee3eff        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           6 weeks ago         Up 6 weeks                              k8s_POD.24f70ba9_volume-pod_default_13702698-2b17-11eb-86e4-5254002dbd85_0610ed58
4784586d01e0        mysql                                                        "docker-entrypoint..."   6 weeks ago         Up 6 weeks                              k8s_mysql.16d54d16_mysql-pd7jr_default_0032bce0-2b0f-11eb-98ad-5254002dbd85_d775f414
c8acc287dc06        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           6 weeks ago         Up 6 weeks                              k8s_POD.1d520ba5_mysql-pd7jr_default_0032bce0-2b0f-11eb-98ad-5254002dbd85_94c9c30a

Other notes:

The-a parameter is used to view all containers, including those that are running, and if-a is not added, only the running containers are displayed.

The-l parameter, which lists the containers that were last run, both running and stopped.

-q parameter, quite, only show container ID

-s parameter, size, showing total file size

2. Download or pull an image.

You can use the methods of docker pull or docker run as follows:


[root@VM-16-13-centos ~]# docker run -i -t ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
Trying to pull repository docker.io/library/ubuntu ... 
latest: Pulling from docker.io/library/ubuntu
da7391352a9b: Downloading [=>                                                 ] 588.8 kB/28.56 MB
14428a6d4bcd: Download complete 
2c2d948710f2: Download complete 

[root@VM-16-13-centos ~]# docker pull mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ...

If we use the docker run command to pull the image, docker will automatically name it randomly for us. If we don't want to use a randomly named container, we can use the-name parameter to specify the name. If we don't specify the version number of the image, the latest version of the image will be automatically pulled.

The docker run command is also often accompanied by the-d parameter, which indicates that the daemon container is running in the background

You can also use the --restart=always parameter to restart the container, of course, the default is not to restart the container.

3. Start, stop, and restart the container

Note that the ID of the container or the name of the container is followed at the back

docker start containerID/name

docker stop containerID/name

docker restart containerID/name

4. View the processes in the container

The docker top command can view all processes within a container.


[root@VM-16-13-centos ~]# docker top  k8s_mysql.16d54d16_mysql-pd7jr_default_0032bce0-2b0f-11eb-98ad-5254002dbd85_d775f414        
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                5059                5044                0                   2020                pts/5               00:00:00            /bin/bash
root                5152                5059                0                   2020                pts/5               00:00:00            mysql -uroot -px xxxx
root                13644               24879               0                   2020                pts/4               00:00:00            mysql -uroot -px xxxx
polkitd             18853               18837               0                   2020                ?                   01:31:43            mysqld
root                24153               24137               0                   2020                pts/1               00:00:00            /bin/bash
root                24376               24153               0                   2020                pts/1               00:00:00            mysql -uroot -px xxxx
root                24879               24864               0                   2020                pts/4               00:00:00            /bin/bash
root                25833               25817               0                   23:45               pts/8               00:00:00            /bin/bash
root                28493               28477               0                   2020                pts/3               00:00:00            /bin/bash
root                28609               28493               0                   2020                pts/3               00:00:00            mysql -uroot -px xxxx
root                29484               29468               0                   2020                pts/2               00:00:00            /bin/bash
root                29601               29484               0                   2020                pts/2               00:00:00            mysql -uroot -px xxxx

5. Execute commands inside the container

For example, we create a folder inside the container, where the container ID is 4784586d01e0, and create the data folder


[root@VM-16-13-centos ~]# docker exec -d 4784586d01e0 mkdir -p /data
[root@VM-16-13-centos ~]# docker exec -it 4784586d01e0 /bin/bash
root@mysql-pd7jr:/# ls -l
total 76
drwxr-xr-x   2 root root 4096 Nov 17 00:00 bin
drwxr-xr-x   2 root root 4096 Sep 19 21:39 boot
drwxr-xr-x   2 root root 4096 Jan  5 15:51 data
drwxr-xr-x   5 root root  360 Nov 20 09:16 dev
drwxr-xr-x   2 root root 4096 Nov 18 08:16 docker-entrypoint-initdb.d
.....

6. Delete the container

docker rm containID/name

Note that the container needs to be stop before deleting it, otherwise the deletion will report an error.

docker rm `docker -a -q`

This command can delete all containers.

These are the details of docker's 1 basic instructions. For more information about docker's basic instructions, please pay attention to other related articles on this site!


Related articles: