首页 > 其他分享 >[Docker] Docker Images with Docker

[Docker] Docker Images with Docker

时间:2023-07-27 15:14:41浏览次数:31  
标签:container kill ubuntu Images run Docker docker

So it's much easier to do what we did with Docker. Run this command:

docker run --interactive --tty alpine:3.10 # or, to be shorter: docker run -it alpine:3.10

A bit easier to remember, right? This will drop you into a Alpine ash shell inside of a container as the root user of that container. When you're done, just run exit or hit CTRL+D. Notice that this will grab the [alpine][alpine] image from Docker for you and run it. The run part of the command is telling Docker you're going to be executing a container (as opposed to building it.) The -it part says you want to be dropped into the container interactively so you can run commands and inspect the container. By default containers run and then exit as soon as they're done. Go ahead and try docker run alpine:3.10. It'll look it did nothing but it actually starts the container and then, because it has nothing defined for it to do, it just exits.

So what if you wanted it to execute something? Try this:

docker run alpine:3.10 ls

Or let's switch to Ubuntu now, since it's more familiar to most. We'll talk about Alpine later on in-depth.

docker run ubuntu:bionic ls

The ls part at the end is what you pass into the container to be run. As you can see here, it executes the command, outputs the results, and shuts down the container. This is great for running a Node.js server. Since it doesn't exit, it'll keep running until the server crashes or the server exits itself.

So now what if we want to detach the container running from the foreground? Let's try that.

docker run --detach -it ubuntu:bionic # or, to be shorter: docker run -dit ubuntu:bionic

So it prints a long hash out and then nothing. Oh no! What happened to it!? Well, it's running in the background. So how do we get ahold of it?

docker ps

This will print out all the running containers that Docker is managing for you. You should see your container there. So copy the ID or the name and say:

docker attach <ID or name> # e.g. `docker attach 20919c49d6e5` would attach to that container

This allows you to attach a shell to a running container and mess around with it. Useful if you need to inspect something or see running logs. Feel free to type exit to get out of here. Run docker run -dit ubuntu:bionic one more time. Let's kill this container without attaching to it. Run docker ps, get the IDs or names of the containers you want to kill and say:

docker kill <IDs or names of containers> # e.g. `docker kill fae0f0974d3d 803e1721dad3 20919c49d6e5` would kill those three containers

A fun way to kill all running containers would be

docker kill $(docker ps -q)

The $() portion of that will evaluate whatever is inside of that first and plug its output into the second command. In this case, docker ps -q returns all the IDs and nothing else. These are then passed to docker kill which will kill all those IDs. Neat!

--name and --rm

Let's make it a bit easier to keep track of these. Try this

docker run -dit --name my-ubuntu ubuntu:bionic
docker kill my-ubuntu

Now you can refer to these by a name you set. But now if you tried it again, it'd say that my-ubuntu exists. If you run docker ps --all you'll see that the container exists even if it's been stopped. That's because Docker keeps this metadata around until you tell it to stop doing that. You can run docker rm my-ubuntu which will free up that name or you can run docker container prune to free up all existing stopped containers (and free up some disk space.)

In the future you can just do

docker run --rm -dit --name my-ubuntu ubuntu:bionic
docker kill my-ubuntu

This will automatically clean up the container when it's done.

标签:container,kill,ubuntu,Images,run,Docker,docker
From: https://www.cnblogs.com/Answer1215/p/17584969.html

相关文章

  • 使用 QEMU 代替 STM32 开发版本 docker 一键启动
    dockerfile#Compileandinstallqemu_stm32fromfedora:28RUNdnfinstall-y\arm-none-eabi-gcc\arm-none-eabi-newlib\findutils\gcc\git\glib2-devel\libfdt-devel\......
  • Docker不能启动,ERROR: ZONE_CONFLICT: 'docker0' already bound to a zone
    Docker服务意外停止,想要重启Docker服务时,却遇到了 ERROR:ZONE_CONFLICT:'docker0'alreadyboundtoazone的错误,解决方案如下:https://stackoverflow.com/questions/67497455/failed-to-start-docker-daemon-firewalld-docker-zone-already-existsthisworks(doallthes......
  • 通过qemu和docker搭建检查编译环境
    背景在工作中我们经常需要交叉编译一些可执行程序或者动态库,有时要编译的程序过于复杂,如果靠纯的交叉编译,费事又费力,需要解决大量的编译依赖以及报错。解决方案docker+qemu-userqemu-user提供可以运行不同架构的用户态程序的方案,而docker可以帮我们搭建一个运行qemu-user的......
  • grafana 监控docker
    使用Grafana监控Docker在容器化应用的开发和部署中,Docker已经变得非常流行。作为一个容器编排和管理工具,Docker可以极大地简化应用程序的部署和管理过程。然而,随着容器数量的增加,监控容器和了解它们的性能变得越来越重要。Grafana是一个强大的监控工具,可以可视化和分析各种......
  • Dokcer学习之旅(2)——Dockerfile基础应用
    什么是Dockerfile?从dockercommit的学习中,我们可以了解到,镜像的定制实际上就是定制每一层所添加的配置、文件。如果我们可以把每一层修改、安装、构建、操作的命令都写入一个脚本,用这个脚本来构建、定制镜像,那么之前提及的无法重复的问题、镜像构建透明性的问题、体积的问题就......
  • Dockerfile example
    FROMpytorch/pytorch:1.13.1-cuda11.6-cudnn8-runtimeARGDEBIAN_FRONTEND=noninteractiveENVTZ=Asia/Shanghai#RUNrm/etc/apt/sources.list.d/cuda.listRUNapt-getupdate#安装ifconfig命令RUNapt-getinstall-ynet-tools#安装ping命令RUNapt-getinstall-yiputils......
  • docker安装jenkins记录
    docker安装jenkins记录docker拉取jenkins镜像dockerpulljenkinszh/jenkins-zh创建Jenkins挂载目录并授权权限mkdir-p/docker/jenkinschmod777/docker/jenkins创建并启动Jenkins容器dockerrun-d-p10240:8080-p10241:50000-v/docker/jenkins:/var/jenkins......
  • Docker学习路线12:开发者体验
    到目前为止,我们只讨论了使用Docker来部署应用程序。然而,Docker也是一个极好的用于开发应用程序的工具。可以采用一些不同的建议来改善开发体验。在应用程序中使用docker-compose以方便开发。使用绑定挂载将本地代码挂载到容器文件系统中,以避免每次更改都需要重新构建容器映像。......
  • Docker安装部署ElasticSearch
    参考文档:https://blog.csdn.net/qq_37726813/article/details/129917352https://blog.csdn.net/yangkei/article/details/126837326https://blog.csdn.net/yangkei/article/details/1268373261.部署单点ElasticSearch因为我们还需要部署kibana容器,因此需要让ElasticSearch和......
  • xxl-job docker 搭建&接入
    项目地址:https://github.com/xuxueli/xxl-job1初始化数据库新建表sql语句在项目里/xxl-job/doc/db/tables_xxl_job.sql在目标数据库执行语句建库、建表##XXL-JOBv2.4.1-SNAPSHOT#Copyright(c)2015-present,xuxueli.CREATEdatabaseifNOTEXISTS`xxl_job`......