Docker image management and container management

1. Image management

1.1, configure the mirror accelerator

By default, the image resources are downloaded from Docker Hub, but the speed is relatively slow. Here we use Alibaba Cloud’s容器镜像服务
Insert picture description here

1.2, view configuration results

command:docker info

...
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://xxxxxxxx.mirror.aliyuncs.com/
 Live Restore Enabled: false

1.3, mirror operation

Docker image is superimposed by file system (a form of file storage), at the bottom is a file boot system (bootfs)

1.3.1, search mirror

mysql is the search keyword

[root@localhost docker]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9790                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3572                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   717                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   79                                      
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   73                                      
...

1.3.2, download mirror

[root@localhost docker]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
...

1.3.3, view image

[root@localhost docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              e3fcc9e1cc04        10 days ago         544MB

1.3.4, delete mirror

docker rmi REPOSITORY:TAG(或docker rmi IMAGE_ID)

Two, container management

2.1, create a new container

Create two containers

[root@localhost docker]# docker create -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=root mysql
8e672109a7931b12236d86176c82bbe9d840ecd1b0c1f22d885aa3786ab37ae0
[root@localhost docker]# docker create -p 3306:3306 --name mysql_new -e MYSQL_ROOT_PASSWORD=root \
> -v /usr/local/docker/mysql/conf:/etc/mysql \
> -v /usr/local/docker/mysql/logs:/var/log/mysql \
> -v /usr/local/docker/mysql/data:/var/lib/mysql \
> mysql
a0013b8fbc9cc3897469dc1a061fa15362282c5cce3292b2e9004797166a607f

name: Custom container name
-v: the directory mapping relationship between the host and the container, ":" before the host directory, and then the container directory
Use the command to docker container ls -aview the container

[root@localhost docker]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
a0013b8fbc9c        mysql               "docker-entrypoint.s…"   21 seconds ago       Created                                 mysql_new
8e672109a793        mysql               "docker-entrypoint.s…"   About a minute ago   Created                                 mysql

2.2, start the container

[root@localhost docker]# docker start mysql
mysql
[root@localhost docker]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
8e672109a793        mysql               "docker-entrypoint.s…"   3 minutes ago       Up 3 seconds        0.0.0.0:3306->3306/tcp, 33060/tcp   mysql

2.3, interact with the container

[root@localhost docker]# docker exec -it mysql /bin/bash
root@8e672109a793:/# ls
bin  boot  dev	docker-entrypoint-initdb.d  entrypoint.sh  etc	home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

drop out

root@8e672109a793:/# exit
exit

2.4, pause/resume the container

[root@localhost docker]# docker pause mysql
mysql
[root@localhost docker]# docker unpause mysql
mysql

2.5, stop the container

[root@localhost docker]# docker stop mysql
mysql

2.6, delete the container

[root@localhost docker]# docker rm mysql_new
mysql_new

2.7, create and start the container

[root@localhost local]# docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=root -d mysql
0298f0809bc32fd5f395934b8d27227bde9f0a5b48747b4e4f152b468822b891
[root@localhost local]# docker run -p 3306:3306 --name mysql_new -e MYSQL_ROOT_PASSWORD=root \
> -v /usr/local/docker/mysql/conf:/etc/mysql \
> -v /usr/local/docker/mysql/logs:/var/log/mysql \
> -v /usr/local/docker/mysql/data:/var/lib/mysql \
> -d mysql
337531e0121fb38f4065533e7b4755dc87f804474ec217e8b9447a016dd27030

2.8. View container logs

[root@localhost local]# docker logs -f -t --tail 5 mysql
2020-08-02T10:09:38.288064489Z 2020-08-02T10:09:38.283328Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2020-08-02T10:09:38.449419625Z 2020-08-02T10:09:38.448423Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-08-02T10:09:38.449447239Z 2020-08-02T10:09:38.448685Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2020-08-02T10:09:38.453364765Z 2020-08-02T10:09:38.451629Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2020-08-02T10:09:38.480806158Z 2020-08-02T10:09:38.480325Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.21'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

2.9. View the processes running in the container

[root@localhost local]# docker top mysql
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
systemd+            34564               34549               1                   06:09               ?                   00:00:01            mysqld

2.10. View the internal details of the container

[root@localhost local]# docker inspect mysql
[
    {
    
    
        "Id": "0298f0809bc32fd5f395934b8d27227bde9f0a5b48747b4e4f152b468822b891",
        "Created": "2020-08-02T10:09:24.47849614Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "mysqld"
        ],
        "State": {
    
    
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 34564,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-08-02T10:09:24.917252644Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
......

2.11, copy container data to the host

[root@localhost ~]# docker cp mysql:/etc/mysql ~/temp/conf
[root@localhost conf]# ls ~/temp/conf/
conf.d  my.cnf  my.cnf.fallback

Guess you like

Origin blog.csdn.net/shaixinxin/article/details/107744613