操作Docker container
1:新建一个容器
ubuntu@ubuntu:~$ docker create -it ubuntu:18.04
05ebf18db758ddf8ff238cbc85e108235e5742587eefddb4272e980e1f504854
ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05ebf18db758 ubuntu:18.04 "/bin/bash" 14 seconds ago Created frosty_northcutt
#创建的容器处于停止状态
2:启动一个容器
ubuntu@ubuntu:~$ docker start 05ebf18db758
05ebf18db758
ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05ebf18db758 ubuntu:18.04 "/bin/bash" 3 minutes ago Up 7 seconds frosty_northcutt
3:新建并启动容器
ubuntu@ubuntu:~$ docker run -it ubuntu:18.04 /bin/bash
root@292c73812a92:/#
#-t选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上,-i则让容器的标准输入保持打开。
#man docker-run
-d:让容器以守护态运行
ubuntu@ubuntu:~$ docker run -d ubuntu:18.04 /bin/bash -c "while true;do echo hello world;sleep 1;done"
f37eba8f478a0150a6b6f0e09148d847af479c9cc5f797e4bde2c3b44c81a3da
ubuntu@ubuntu:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f37eba8f478a ubuntu:18.04 "/bin/bash -c 'while…" 11 seconds ago Up 11 seconds youthful_boyd
查看某容器的输出
ubuntu@ubuntu:~$ docker logs f37eba8f478a
hello world
hello world
hello world
...
4:停止容器
ubuntu@ubuntu:~$ docker run --name test -d --rm -it ubuntu:18.04 bash #后台运行一个容器
9834e3c598f85531902cee9734af1d847e09fa82ef63e4235358e86a69dca1fc
# --name :Assign a name to the container
# --rm:可以与-d一起工作,自动删除将在守护进程端完成
ubuntu@ubuntu:~$ docker pause test #停止容器
test
ubuntu@ubuntu:~$ docker ps #查看容器状态,处于(Paused)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9834e3c598f8 ubuntu:18.04 "bash" About a minute ago Up About a minute (Paused) test
ubuntu@ubuntu:~$ docker unpause test #恢复容器
test
ubuntu@ubuntu:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9834e3c598f8 ubuntu:18.04 "bash" 2 minutes ago Up 2 minutes test
5:终止容器|重新启动容器
#终止容器
ubuntu@ubuntu:~$ docker stop test
test
#:查看容器
ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f37eba8f478a ubuntu:18.04 "/bin/bash -c 'while…" 16 minutes ago Exited (137) 10 minutes ago youthful_boyd
#启动容器
ubuntu@ubuntu:~$ docker start f37eba8f478a
f37eba8f478a
#重启容器
ubuntu@ubuntu:~$ docker restart f37eba8f478a
f37eba8f478a
6:进入容器---当使用-d参数的容器就会进入后台,用户无法看到容器中的信息,也无法进行操作
ubuntu@ubuntu:~$ docker run -itd ubuntu:18.04 #后台运行容器
e3b8bf83a13e3a79cf63ee1e7cc6ead2bb5ad30de5d820e89b63a0a2be38a005
ubuntu@ubuntu:~$
#进入刚创立的容器中,并且打开一个新的bash终端
ubuntu@ubuntu:~$ docker exec -it e3b8bf83a13e3a79cf /bin/bash
root@e3b8bf83a13e:/#
7:删除容器-----删除处于终止或者退出状态的容器
ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3b8bf83a13e ubuntu:18.04 "/bin/bash" 4 minutes ago Up 3 minutes modest_rosalind
ubuntu@ubuntu:~$ docker rm -f e3b8bf83a13e # -f: 强制删除正在运行的容器
e3b8bf83a13e
ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f37eba8f478a ubuntu:18.04 "/bin/bash -c 'while…" 39 minutes ago Exited (137) 5 minutes ago youthful_boyd
fc37c81884cf ubuntu:18.04 "-c 'while true;do e…" 39 minutes ago Created peaceful_sammet
292c73812a92 ubuntu:18.04 "/bin/bash" 43 minutes ago Exited (0) 41 minutes ago happy_nobel
#一次性删除多个容器
ubuntu@ubuntu:~$ docker rm $(docker ps -aq)
ubuntu@ubuntu:~$ docker ps -a -q | xargs docker rm
8:导入和导出容器---将容器从一个系统导入到另一个系统
创建一个正在运行和退出的容器
ubuntu@ubuntu:~$ docker run -d -it ubuntu:18.04 /bin/bash
c3637c3828b88995f9db358fb08dbc8d66bacb5605f764a75a2343340ec2bebd
ubuntu@ubuntu:~$ docker run -it ubuntu:18.04 /bin/bash
root@4f595f3e726d:/# exit
exit
ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f595f3e726d ubuntu:18.04 "/bin/bash" 9 seconds ago Exited (0) 5 seconds ago hungry_leavitt
c3637c3828b8 ubuntu:18.04 "/bin/bash" 29 seconds ago Up 29 seconds nifty_ritchie
ubuntu@ubuntu:~$ docker export -o test_stop.tar 4f5 #导出一个退出的容器
ubuntu@ubuntu:~$ docker export -o test_run.tar c36 #导出一个正在运行的容器
ubuntu@ubuntu:~$ ls -lt
total 195112
-rw------- 1 ubuntu ubuntu 66570752 Nov 14 21:30 test_run.tar
-rw------- 1 ubuntu ubuntu 66571776 Nov 14 21:29 test_stop.tar
导出的文件可以通过docker import 命令导入为镜像.
scp到另一台服务器上:
ubuntu@ubuntu:~$ scp -r test_run.tar root@192.168.1.10:/home
#格式:docker import [-c|--change[=[]]] [-m|--message[=MESSAGE]] file|URL|[REPOSITORY[:TAG]]
[root@localhost home]# docker import test_run.tar test/ubuntu:v1.0
sha256:7d1a701d2885277b0cbb5160a9947eec965b612d4674e872a62047ef0d3f1b39
[root@localhost home]# docker images #新服务器上查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
test/ubuntu v1.0 7d1a701d2885 3 minutes ago 64.2MB
9:查看容器
#inspect:查看镜像的具体信息
[root@localhost home]# docker image inspect test/ubuntu:v1.0
[
{
"Id": "sha256:7d1a701d2885277b0cbb5160a9947eec965b612d4674e872a62047ef0d3f1b39",
"RepoTags": [
"test/ubuntu:v1.0"
...
#查看容器的具体信息
ubuntu@ubuntu:~$ docker container inspect c3637c3828b8
[
{
"Id": "c3637c3828b88995f9db358fb08dbc8d66bacb5605f764a75a2343340ec2bebd",
"Created": "2019-11-14T13:20:17.367777873Z",
"Path": "/bin/bash",
...
#查看容器内进程可以使用docker [container] top [OPTIONS] CONTAINER [CONTAINER...]子命令
ubuntu@ubuntu:~$ docker top c3637c3828b8
#查看统计信息可以使用docker [container] stats [OPTIONS] [CONTAINER...]子命令,会显示CPU、内存、存储、网络等使用情况的统计信息
-a :输出所有统计信息,默认仅输出运行中的
ubuntu@ubuntu:~$ docker stats -a
10:复制文件:可以在容器和主机之间复制文件
命令格式为docker [container] cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- 。
#将本地路径的test.txt 复制到 容器的/tmp下
ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4f595f3e726d ubuntu:18.04 "/bin/bash" About an hour ago Exited (0) About an hour ago hungry_leavitt
c3637c3828b8 ubuntu:18.04 "/bin/bash" About an hour ago Up About an hour nifty_ritchie
ubuntu@ubuntu:~$ docker cp test.txt c3637c3828b8:/tmp
ubuntu@ubuntu:~$ docker exec -it c3637c3828b8 /bin/bash
root@c3637c3828b8:/# cd /tmp/
root@c3637c3828b8:/tmp# ls
test.txt
root@c3637c3828b8:/tmp#
11:查看容器变更
命令格式为docker [container] diff CONTAINER
ubuntu@ubuntu:~$ docker diff c3637c3828b8
C /root
A /root/.bash_history
C /tmp
A /tmp/test.txt
12:查看端口映射
命令格式为docker container port CONTAINER [PRIVATE_PORT[/PROTO]]
ubuntu@ubuntu:~$ docker port c3637c3828b8 #没有映射端口
小结:
docker container help命令查看Docker支持的容器操作子命令
ubuntu@ubuntu:~$ docker container help
Usage: docker container COMMAND
Manage containers
Options:
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Run 'docker container COMMAND --help' for more information on a command.