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

[Docker] Docker Images with Docker

时间:2024-09-06 14:36:24浏览次数:4  
标签:container -- 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.19.1
# or, to be shorter: docker run -it alpine:3.19.1

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 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.19.1. 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.19.1 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:jammy 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:jammy # or, to be shorter: docker run -dit ubuntu:jammy

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:jammy 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

 

--name and --rm

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

docker run -dit --name my-ubuntu ubuntu:jammy
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:jammy
docker kill my-ubuntu

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

标签:container,--,ubuntu,Images,run,Docker,docker
From: https://www.cnblogs.com/Answer1215/p/18400182

相关文章

  • Docker 容器技术:简化 MySQL 主从复制部署与优化
    文章目录前言一、为什么基于Docker搭建?二、利用Docker搭建主从服务器2.1配置Master(主)2.2配置Slave(从)2.3链接Master(主)和Slave(从)2.4测试主从复制三、常见问题3.1什么时候用读写分离?3.2MySQL主从复制原理3.3解决主从复制延迟有几种常见的方法?3.4造成mysql同步......
  • grafana/K6 的 Docker 镜像pull另存为tar的文件
    因国内环境限制,如果直接pull grafana/K6的镜像会失败,可通过以下方式pull配置方式1:临时使用直接使用,直接拿镜像域名拼接上官方镜像名,例如要拉去镜像dockerpullloadimpact/k6,可以用下面写法dockerpulldocker.registry.cyou/loadimpact/k6配置方式2:长久有效修改文件 /......
  • docker file
    dockerfiledockerfile是一个文本格式的配置文件,用户可以使用dockerfile快速的创建和自定义镜像。docker一般由四部分组成:基础镜像信息(父镜像)、维护者信息、镜像操作、容器启动时执行的命令。dockerfile可以通过#标识注释。dockerfile的格式都是采用“指令用户定义的内容”,注意......
  • Ubuntu 安装Docker 运行Nginx
    目录介绍主要功能和特点常用组件安装Docker卸载旧版本Docker(如果有的话)更新apt包索引安装Docker所需的包 添加Docker官方GPG密钥设置Docker仓库安装最新版本Docker验证Docker是否安装成功允许当前用户操作Docker(可选,避免每次使用docker都需要sudo)在Docker......
  • Docker 安装mysql
    1、从docker hub上拉取镜像到本地#dockerpullmysql:5.62.如果是不加版本直接拉取的话,默认的是最新版本 #dockerpullmysql:latest默认拉取最新的版本3.启动:#dockerrun--namemysql-p3306:3306-eMYSQL_ROOT_PASSWORD="设置你的mysql的密码"-dmysql:5.6......
  • 【达梦】Docker安装达梦数据库 dm8
    1.docker启动达梦8镜像命令dockerrun-p5236:5236--namedmdb\-eLD_LIBRARY_PATH=/opt/dmdbms/bin\-ePAGE_SIZE=32\-eEXTENT_SIZE=32\-eLOG_SIZE=2048\-eUNICODE_FLAG=1\-eLENGTH_IN_CHAR=1\-eBLANK_PAD_MODE=1\-v/home/docker/dmdbms/data::/opt......
  • Docker 常用命令
    介绍下Docker常用命令‍帮助启动类命令启动docker:systemctlstartdocker​停止docker:systemctlstopdocker​重启docker:systemctlrestartdocker​查看docker状态:systemctlstatusdocker​开机自启:systemctlenabledocker​查看docker概要信息:dockeri......
  • Docker - 在Rockly Linux 9.4 上安装Docker-CE
    安装Docker-CE修改repo源修改为国内阿里源以提高安装速度sed-e's|^mirrorlist=|#mirrorlist=|g'\-e's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g'\-i.bak\/etc/yum.repos.d/rocky*.repo......
  • docker部署mysql8主从
     一、拉取mysql镜像dockerpullmysql 二、创建容器#主库dockerrun-itd-p3306:3306-eMYSQL_ROOT_PASSWORD=123456--namemysql-mastermysql#从库dockerrun-itd-p3307:3306-eMYSQL_ROOT_PASSWORD=123456--namemysql-slavemysql 三、配置主库[m......
  • BEVFormer复现(使用docker搭建训练环境)
    文章目录一、使用docker创建环境1.1创建容器1.2在容器中安装常用的包1.3安装miniconda1.4安装Pytorch二、环境配置2.1下载源码2.2安装mmcv-full2.3安装mmdet和mmseg2.4从源码安装mmdet3d2.5安装Detectron2和Timm2.6下载预训练模型三、数据准备3.1下载数据集......