[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 43c3475f88f4 8 minutes ago 0 B
ralph/nginx latest 8eed31b6faa7 16 minutes ago 126 MB
docker.io/nginx latest ab56bba91343 11 days ago 126 MB
docker.io/centos latest 67fa590cfc1c 4 weeks ago 202 MB
[root@localhost ~]# docker run nginx /bin/echo "ralph"
ralph #创建一个容器执行一条输出“ralph的命令”之后就是死亡
[root@localhost ~]# docker ps -a (-a 显示所有的容器)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7005896ea8b6 nginx "/bin/echo ralph" 17 seconds ago Exited (0) 16 seconds ago jolly_ramanujan
5f4b5d05a5c6 ralph/nginx "/bin/bash" 13 minutes ago Up 13 minutes 80/tcp happy_lumiere
76e2ef67bd36 docker.io/nginx "/bin/bash" 18 minutes ago Exited (130) 13 minutes ago eager_volhard
e0d88df55197 docker.io/nginx "sh" 19 minutes ago Exited (127) 18 minutes ago gifted_kirch
#启动一个 bash 终端,允许用户进行交互。
[root@localhost ~]# docker run --name=mynginx -it nginx /bin/bash
root@721a8c893800:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
721a8c893800 nginx "/bin/bash" 56 seconds ago Up 56 seconds 80/tcp mynginx
[root@docker ~]# docker ps -a
[root@docker ~]# docker start 6d8c8b3de337
[root@docker ~]# docker ps -a #查看是否启动
更多的时候,需要让 Docker 容器在后台以守护形式运行。此时可以通过添加-d参数来实现。
[root@localhost ~]# docker run -d nginx /bin/bash -c "while true; do echo ralph;sleep 1;done"
77e49a25a28fb42a79792ce26003ef4a6121cf0ca62cc914a3d8920bfa538d11
[root@localhost ~]# docker ps #查看正在运行的 docker 容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
77e49a25a28f nginx "/bin/bash -c 'whi..." 6 seconds ago Up 5 seconds 80/tcp gifted_poitras
615f6c8e4c30 nginx "/bin/bash -c 'whi..." About a minute ago Up About a minute 80/tcp cranky_babbage
[root@localhost ~]# docker logs 77e49a25a28f #获取容器输出信息,通过 dockerlogs 命令
ralph
ralph
ralph
ralph
ralph
ralph
ralph
ralph
ralph
ralph
ralph
ralph
ralph
ralph
ralph
ralph
ralph
ralph
PS:
可以使用 docker stop 来终止一个运行中的容器。
此外,当 Docker 容器中指定的应用终结时,容器也自动终止。例如启动一个终端的容器,用户通过 exit 命令或者 ctrl+d 来退出终端时,所创建的容器立刻终止。
[root@docker ~]# docker ps -a
极端方式停止容器(不推荐)
#列出所有启动容器的 ID
[root@docker ~]# docker ps -a -q
#批量杀掉启动的容器
[root@docker ~]# docker kill $(docker ps -a -q)
ctrl+p+q 退出时容器不停止
[root@docker ~]# docker start 6d8c8b3de337
[root@docker ~]# docker attach 6d8c8b3de337 #直接运行
[root@6d8c8b3de337:/# # ctrl+p+q 退出
[root@docker ~]# docker ps -a
[root@docker ~]# docker rm c599b569f387 删除已经停止的容器
[root@docker ~]# docker rm -f 302f39c202c9 删除正在运行的容器
使用-d 参数时,容器启动后会进入后台。某些时候需要进入容器进行操作,有很多种方法,包括使用 docker attach 命令
[root@docker ~]# docker run -dit centos
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
8c6c36796748 centos "/bin/bash" 10 seconds ago Up 9 seconds
eager_ramanujan
#通过 docker attach 进入
[root@docker ~]# docker attach 8c6c36796748
root@8c6c36796748 /]#
2.exec
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6b04d76e32e3 nginx "nginx -g 'daemon ..." 3 minutes ago Exited (0) 39 seconds ago eager_davinci
[root@localhost ~]# docker start 6b04d76e32e3
6b04d76e32e3
[root@localhost ~]# docker exec -it 6b04d76e32e3 /bin/bash
root@6b04d76e32e3:/#
在 windows 上用访问 linux 宿主机 http://你的宿主机IP:8888 访问容器
[root@localhost ~]# docker run -dit nginx
3a142a0759232d3bed327218c6f7d52987417f5de10ce027c89265539e96952f
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a142a075923 nginx "nginx -g 'daemon ..." 5 seconds ago Up 5 seconds 80/tcp dreamy_haibt
[root@localhost ~]# curl 172.17.0.2
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@localhost ~]# docker run -dit -p 8888:80 nginx
#前面是宿主机的端口 后面是容器的端口
f00eed1db06cf00e2d56dc100a19764d625fffc6332d0436d050a0db734b3123
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f00eed1db06c nginx "nginx -g 'daemon ..." 5 minutes ago Up 5 minutes 0.0.0.0:8888->80/tcp sad_keller
3a142a075923 nginx "nginx -g 'daemon ..." 6 minutes ago Up 6 minutes 80/tcp dreamy_haibt