foreword
There must be a process running in the container, but we can specify the running process. If no container process is specified, the process specified by CMD in the image will be run. You can view the process specified by CMD through the following command,
There are many objects, such as images, containers, networks, etc. To view their properties, the general syntax is as follows:
docker 属性类型 inspect 对象名称
docker image inspect nginx
docker containerd inspect c1
#可直接写docker inspect c1
docker network inspect net1
1. Manage containers
view container
docker ps
#查看正在运行的容器
docker ps -a
#查看所有容器
create container
docker run 镜像名
When the container is running normally, the state is up. If the CMD specifies the bash process, the process will only exist for a moment, and if you want the bash process to exist all the time, you need to give it a terminal.
docker run -it hub.c.163.com/library/centos
#-it参数,退出该容器时容器会自动挂掉,如果想该容器在后台运行需:
docker run -dit hub.c.163.com/library/centos
#docker run -dit --restart=always hub.c.163.com/library/centos
--restart=always 为重启docker时该容器自动启动
Enter the container as attach, do not enter the container as detach, do not enter the container and add -d when creating the container. In docker, -d and -it can be used together, but others such as nerdctl cannot be used together.
Use --name to give the container a name, otherwise the container name is randomly assigned.
docker run -dit --restart=always hub.c.163.com/library/centos sleep 10d
#sleep 10d指该容器运行十天
#加-e可指定变量
docker run -dit --restart=always -e xx=AA -e yy=bb hub.c.163.com/library/centos
进入容器后就会有指定的变量
Add port to container
docker run -d -p N:M
docker run -d -p M
#M容器端口,N宿主机端口,如果只写容器端口则随机指定宿主机端口
The port of the container and the specified host port can be viewed through the nerdctl port container name
stop container, start container, restart container
nerdctl stop xxx (容器名) #停止容器
nerdctl start xxx (容器名) #启动容器
nerdctl restart xxx (容器名) #重启容器
View the processes running in the container
nerdctl top xxx (容器名)
View the output (logs) inside the container
nerdctl logs xxx (容器名)
nerdctl logs -f xxx (容器名)
#-f为查看不退出,有新的日志会持续显示
Execute the command in the container
nerdctl exec xxx (容器名) ls /root (要执行的命令)
#容器外执行命令
nerdctl exec -it xxx (容器名) bash
#进入容器,输入exit退出
nerdctl does not support attach into the container
Can copy things in the host machine to the container
You can also copy the files in the container to the host machine
nerdctl cp web1:/etc/hosts hosts
data volume
Any data added to the container will be written to the container layer, and the data will be there regardless of whether the container is restarted, stopped, or started. But whenever the container is deleted, the container layer is also deleted. If you want the data to be stored permanently (it still exists after the container is deleted), you need to make a data volume.
-v M
#只写M表示容器的目录,宿主机则随机生成一个挂载点
-v N:M
#N表示宿主机目录
例如:
nerdctl run -it --name=a1 --rm -v /data nginx bash
#--rm表临时容器 -v 数据卷 没写宿主机挂载点则随机生成,可通过inspect查看mounts栏查看宿主机挂载点
nerdctl run -it --name=a1 --rm -v /xxx:/data:ro nginx bash
#ro表只读形式挂载,容器里只能读该文件夹
The mount point needs to be determined according to the actual situation of the application
nginx-/usr/share/nginx/html/
mysql-/var/lib/mysql
apache-/var/www/html
When using mysql mirroring, you need to specify some variables:
MYSQL_ROOT_PASSWORD root密码
MYSQL_USER 创建普通用户
MYSQL_PASSWORD 普通用户的密码
MYSQL_DATABASE 创建一个库
Deploy the MySQL container and mount it to the /db directory of the host, and then install the client mariadb on the host
Use the following command to view the ip address of the container db
enter the database
View databases and create new ones
Check the /db of the host and find that aa has been stored
If you delete the container and recreate the database container and mount /db, you will find that the aa library can still be connected after logging in to the database.