02-docker容器简单操作
1、下载镜像
docker pull nginx
2、查找镜像
docker search nginx
3、查看本机docker镜像
docker images
4、镜像删除/导出/导入
删除
[root@VM-0-8-centos ~]# docker rmi nginx
导出
[root@VM-0-8-centos ~]# docker save nginx > /tmp/nginx.tar.gz
导入
[root@VM-0-8-centos ~]# docker load < /tmp/nginx.tar.gz
5、运行容器
[root@VM-0-8-centos tmp]# docker run -it alpine sh / # ls bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var / # exit [root@VM-0-8-centos tmp]# docker run -it --name mynginx nginx
6、容器后台运行(按ctrl不放手p q,就会放后台)
[root@VM-0-8-centos tmp]# docker run -it alpine sh
/ # [root@VM-0-8-centos tmp]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 83d422aa0cf1 alpine "sh" 16 seconds ago Up 16 seconds musing_stonebraker
显示正在运行的容器 docker ps
显示所有容器 docker ps -a
[root@VM-0-8-centos tmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
83d422aa0cf1 alpine "sh" 6 minutes ago Up 6 minutes musing_stonebraker
[root@VM-0-8-centos tmp]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4fb1353fec22 alpine "sh" About a minute ago Exited (0) About a minute ago objective_jennings
83d422aa0cf1 alpine "sh" 7 minutes ago Up 7 minutes musing_stonebraker
08c6a724a192 alpine "sh" 9 minutes ago Exited (0) 9 minutes ago eager_hamilton
5c4a42aea338 alpine "sh" 11 minutes ago Exited (0) 9 minutes ago hungry_agnesi
7、删除容器
[root@VM-0-8-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 83d422aa0cf1 alpine "sh" About an hour ago Up About an hour musing_stonebraker [root@VM-0-8-centos ~]# docker rm -f 83d422aa0cf1 83d422aa0cf1 [root@VM-0-8-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8、查看容器详细信息
[root@VM-0-8-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d83f57c2979d nginx "/docker-entrypoin..." 27 seconds ago Up 27 seconds 80/tcp mynginx [root@VM-0-8-centos ~]# docker inspect d83f57c2979d
9、当退出时自动删除容器
docker run -it --rm --name mynginx nginx
docker ps -a
运行时加--rm参数后,ps -a就看不到了
10、、进入容器进行操作、一般不用
docker attach 容器名/ID #2、一般用这个 docker exec it 容器名/ID sh
#3、少用,尽量写成形式脚本进入
docker_in.sh
#!/bin/sh
pid=`docker inspect --format "{{.State.Pid}}" $1`
nsenter -t $pid -m -n -i -n -p
sh docekr_in.sh 容器名/ID
11、后台运行(-d参数,后台运行)
[root@VM-0-8-centos ~]# docker run -it -d --name mynginx nginx 786fc1f32728c23046f2e489b1abdb191bf50edc1b09814e12ff449a2961ef79 [root@VM-0-8-centos ~]#
放后台运行查看日志方式
[root@VM-0-8-centos ~]# docker logs mynginx(容器名/ID)
实时看
[root@VM-0-8-centos ~]# docker logs -f mynginx(容器名/ID)