docker 常用命令

📅 发布时间:2026/7/5 1:24:10 👁️ 浏览次数:
docker 常用命令
目录一. 创建Dockerfile文件1.1 下载示例项目1.2 编辑Dockerfile文件二. image镜像2.1 镜像构建2.2 镜像查看2.2.1 docker images 查看所有镜像2.2.2 docker image ls 查看所有镜像2.2.3 镜像详细信息查看2.2.4 镜像指定信息查看2.3 镜像导出与导入2.3.1 导出镜像为文件2.3.2 将导出的镜像文件恢复为镜像2.4 镜像删除三. container容器3.1 查看容器3.1.1 docker container ls --all 查看所有容器3.1.2 docker container logs 查看 docker 容器的输出3.2 启动容器3.2.1 docker run 创建并启动容器3.2.2 ⭕启动容器时挂载数据3.2.3 docker container start 启动一个既存的容器3.3 停止容器3.3.1 docker stop3.3.2 docker container stop3.4 删除容器3.4.1 docker rm3.4.2 docker container rm3.5 docker exec进入容器内部3.6 docker container cp 复制容器内的文件到本地四. registry仓库4.1 docker pull 镜像拉取4.2 docker push 镜像推送一. 创建Dockerfile文件1.1 下载示例项目⏹执行下面的git命令下载示例项目git clone https://github.com/mattwojo/helloworld-django.git⏹下载完成之后移动到示例项目的根目录应该会看到这样的目录apluserFengYeHong-HP:helloworld-django$ls-ltotal148-rw-r--r--1apluser apluser131072Jun716:56 db.sqlite3 drwxr-xr-x4apluser apluser4096Jun709:38 hello -rwxr-xr-x1apluser apluser667Jun709:38 manage.py -rw-r--r--1apluser apluser16Jun709:38 requirements.txt drwxr-xr-x3apluser apluser4096Jun709:38 web_project1.2 编辑Dockerfile文件⏹在项目的根目录下手动创建一个Dockerfile文件创建之后的目录如下apluserFengYeHong-HP:helloworld-django$ls-ltotal148-rw-r--r--1apluser apluser196Jun716:54 Dockerfile -rw-r--r--1apluser apluser131072Jun716:56 db.sqlite3 drwxr-xr-x4apluser apluser4096Jun709:38 hello -rwxr-xr-x1apluser apluser667Jun709:38 manage.py -rw-r--r--1apluser apluser16Jun709:38 requirements.txt drwxr-xr-x3apluser apluser4096Jun709:38 web_project apluserFengYeHong-HP:helloworld-django$ apluserFengYeHong-HP:helloworld-django$catDockerfile# 安装python环境, 指定基础镜像FROM python:3.11-slim# 创建一个名为 app 的工作目录WORKDIR /app# 将当前目录下的 requirements.txt 复制到app工作目录中COPY requirements.txt.# 运行pip命令, 安装python项目所需的依赖RUN pipinstall--no-cache-dir-rrequirements.txt# 将当前目录下的所有内容都复制到app工作目录中COPY./app# 指定8000端口EXPOSE8000# 启动python项目CMD[python,manage.py,runserver,0.0.0.0:8000]二. image镜像2.1 镜像构建-t参数用来指定 image 文件的名字后面还可以用冒号指定标签。如果不指定默认的标签就是latest。最后的那个.表示Dockerfile文件所在的路径因为当前路径在Dockerfile文件所在的目录所示是一个.。dockerimage build-thelloworld-django.# 或者dockerimage build-thelloworld-django:0.0.1.2.2 镜像查看2.2.1docker images查看所有镜像apluserFengYeHong-HP:~$dockerimages REPOSITORY TAG IMAGE ID CREATED SIZE helloworld-django latest 51fedeaf85ff2days ago 363MB hello-world latest 0b6a027b5cf34months ago20.4kB2.2.2docker image ls查看所有镜像apluserFengYeHong-HP:~$dockerimagelsREPOSITORY TAG IMAGE ID CREATED SIZE helloworld-django latest 51fedeaf85ff2days ago 363MB hello-world latest 0b6a027b5cf34months ago20.4kB2.2.3 镜像详细信息查看docker inspect 镜像名:版本号docker inspect 镜像IDapluserFengYeHong-HP:project$dockerimagelsREPOSITORY TAG IMAGE ID CREATED SIZE guohuiyuan/go-music-dl latest bdafc937ce7f4days ago 327MB apluserFengYeHong-HP:project$ apluserFengYeHong-HP:project$dockerinspect guohuiyuan/go-music-dl:latest|head[{Id:sha256:bdafc937ce7fe64084ec2e81941f3d933ae5e4b856f06842b0718e262b2621b4,RepoTags:[guohuiyuan/go-music-dl:latest],RepoDigests:[guohuiyuan/go-music-dlsha256:bdafc937ce7fe64084ec2e81941f3d933ae5e4b856f06842b0718e262b2621b4],Parent:, apluserFengYeHong-HP:project$dockerinspect bdafc937ce7f|head[{Id:sha256:bdafc937ce7fe64084ec2e81941f3d933ae5e4b856f06842b0718e262b2621b4,RepoTags:[guohuiyuan/go-music-dl:latest],RepoDigests:[guohuiyuan/go-music-dlsha256:bdafc937ce7fe64084ec2e81941f3d933ae5e4b856f06842b0718e262b2621b4],Parent:, apluserFengYeHong-HP:project$2.2.4 镜像指定信息查看apluserFengYeHong-HP:~$dockerimagelsREPOSITORY TAG IMAGE ID CREATED SIZE guohuiyuan/go-music-dl latest bdafc937ce7f4days ago 327MB apluserFengYeHong-HP:~$ apluserFengYeHong-HP:~$dockerinspect--format{{.Created}}guohuiyuan/go-music-dl2026-03-03T11:00:25.406122852Z apluserFengYeHong-HP:~$ apluserFengYeHong-HP:~$dockerinspect--format{{.RepoTags}} {{.Created}}bdafc937ce7f[guohuiyuan/go-music-dl:latest]2026-03-03T11:00:25.406122852Z apluserFengYeHong-HP:~$ apluserFengYeHong-HP:~$dockerimages--formattable {{.Repository}}\t{{.Tag}}\t{{.CreatedAt}}guohuiyuan/go-music-dl REPOSITORY TAG CREATED AT guohuiyuan/go-music-dl latest2026-03-0320:00:25 0900 JST2.3 镜像导出与导入2.3.1 导出镜像为文件dockersave 镜像名称:版本号-o镜像名称.tardockersave 镜像id-o镜像名称.tar2.3.2 将导出的镜像文件恢复为镜像dockerload-i镜像名称.tar2.4 镜像删除docker image rm 镜像IDapluserFengYeHong-HP:~$dockerimagerm51fedeaf85ff Untagged: helloworld-django:latest Deleted: sha256:51fedeaf85ffdc569edae785cce00602eb90b4a7a12766c6f671a517f0c4f199 apluserFengYeHong-HP:~$ apluserFengYeHong-HP:~$dockerimagelsREPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 0b6a027b5cf34months ago20.4kB三. container容器3.1 查看容器3.1.1docker container ls --all查看所有容器docker container ls查看正在运行的容器docker container ls --all查看正在所有容器包括已经停止运行的容器⏹docker ps的作用和docker container相同只不过从语义化上看来docker container更好。3.1.2docker container logs查看 docker 容器的输出⏹docker 容器的输出即容器里面 Shell 的标准输出。如果docker run命令运行容器的时候没有使用-it参数docker container start直接启动既存容器时就要用这个命令查看输出。3.2 启动容器3.2.1docker run创建并启动容器-it容器的 Shell 映射到当前的 Shell然后你在本机窗口输入的命令就会传入容器--rm容器一旦停止就会自动删除(仅在临时测试时使用)-p容器的 8000 端口映射到本机的 8100 端口helloworld-django:latest指定要启动的容器apluserFengYeHong-HP:~$dockerrun-it--rm-p8100:8000 helloworld-django:latest Watchingforfilechanges with StatReloader Performing system checks... System check identified no issues(0silenced). You have18unapplied migration(s). Your project may not work properlyuntilyou apply the migrationsforapp(s): admin, auth, contenttypes, sessions. Runpython manage.py migrateto apply them. June14,2025- 00:12:19 Django version3.2.25, using settingsweb_project.settingsStarting development server at http://0.0.0.0:8000/ Quit the server with CONTROL-C.⏹容器启动之后在浏览器中就可以看到如下所示的界面3.2.2 ⭕启动容器时挂载数据-v /主机路径文件:/容器路径文件这会让容器内部使用主机上的文件从而让db.sqlite3的内容得以持久保存。-w /app设置容器内的工作目录是/app这个要与你 Dockerfile 中 WORKDIR 设置一致通常也是/appdockerrun-it-p8100:8000\-v/home/apluser/project/helloworld-django/db.sqlite3:/app/db.sqlite3\-w/app helloworld-django:0.0.13.2.3docker container start启动一个既存的容器⏹docker run的方式会创建并启动一个容器同样的命令运行两次就会生成两个一模一样的容器文件。如果希望重复使用容器就要使用docker container start命令它用来启动已经生成、已经停止运行的容器文件。3.3 停止容器3.3.1docker stop⏹docker stop 容器ID停止正在运行的容器。注意停止运行的容器并不会被删除。docker stop 容器ID或名称会向容器发送SIGTERM信号让其优雅退出如果在默认的 10 秒内还没退出则发送SIGKILL强制终止。docker kill 容器ID或名称会立即强制终止容器不等待apluserFengYeHong-HP:~$dockercontainerlsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d32995d7a305 helloworld-django:latestpython manage.py ru…22minutes ago Up22minutes0.0.0.0:8100-8000/tcp kind_bassi apluserFengYeHong-HP:~$ apluserFengYeHong-HP:~$dockerstop d32995d7a305 d32995d7a3053.3.2docker container stopapluserFengYeHong-HP:~$dockercontainerlsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 783e1ad29407 helloworld-django:latestpython manage.py ru…16minutes ago Up16minutes0.0.0.0:8100-8000/tcp reverent_lalande apluserFengYeHong-HP:~$ apluserFengYeHong-HP:~$dockercontainer stop 783e1ad29407 783e1ad294073.4 删除容器3.4.1docker rm⏹如果你不只是要停止容器还想连容器一起删除比如临时容器可以用docker rm -f-f相当于先stop再rmapluserFengYeHong-HP:~$dockercontainerlsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4c07319bbebc helloworld-django:latestpython manage.py ru…11seconds ago Up9seconds0.0.0.0:8100-8000/tcp infallible_sammet apluserFengYeHong-HP:~$ apluserFengYeHong-HP:~$dockerrm-f4c07319bbebc 4c07319bbebc3.4.2docker container rm⏹语义化更好apluserFengYeHong-HP:~$dockercontainerls--allCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6380b917ff0c helloworld-django:latestpython manage.py ru…44seconds ago Up42seconds0.0.0.0:8100-8000/tcp determined_boyd f0e58479ba3d helloworld-djangopython manage.py ru…54minutes ago Exited(0)54minutes ago vigorous_leavitt cca37d72e4ba helloworld-djangopython manage.py ru…54minutes ago Exited(0)54minutes ago musing_franklin f51614fe80d9 helloworld-django:latestpython manage.py ru…55minutes ago Exited(0)54minutes ago unruffled_franklin bb11c3bbaf5f helloworld-djangopython manage.py ru…56minutes ago Exited(0)56minutes ago relaxed_banach apluserFengYeHong-HP:~$ apluserFengYeHong-HP:~$dockercontainerrmbb11c3bbaf5f bb11c3bbaf5f apluserFengYeHong-HP:~$3.5docker exec进入容器内部docker exec -it 容器ID bashdocker exec -it 容器ID shapluserFengYeHong-HP:~$dockercontainerlsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 783e1ad29407 helloworld-django:latestpython manage.py ru…12seconds ago Up11seconds0.0.0.0:8100-8000/tcp reverent_lalande apluserFengYeHong-HP:~$ apluserFengYeHong-HP:~$dockerexec-it783e1ad29407bashroot783e1ad29407:/app# ls -ltotal20-rw-r--r--1root root483Jun1111:48 Dockerfile -rw-r--r--1root root0Jun1323:58 db.sqlite3 drwxr-xr-x4root root4096Jun700:38 hello -rwxr-xr-x1root root667Jun700:38 manage.py -rw-r--r--1root root16Jun700:38 requirements.txt drwxr-xr-x3root root4096Jun700:38 web_project3.6docker container cp复制容器内的文件到本地⏹将容器内的requirements.txt文件复制到本地的指定路径中apluserFengYeHong-HP:~$dockerexec-itf0e58479ba3dbashrootf0e58479ba3d:/app#rootf0e58479ba3d:/app# ls -ltotal20-rw-r--r--1root root483Jun1111:48 Dockerfile -rw-r--r--1root root0Jun1323:58 db.sqlite3 drwxr-xr-x4root root4096Jun700:38 hello -rwxr-xr-x1root root667Jun700:38 manage.py -rw-r--r--1root root16Jun700:38 requirements.txt drwxr-xr-x3root root4096Jun700:38 web_project rootf0e58479ba3d:/app#rootf0e58479ba3d:/app# exitexitapluserFengYeHong-HP:~$dockercontainercpf0e58479ba3d:/app/requirements.txt /home/apluser/work/ Successfully copied2.05kB to /home/apluser/work/ apluserFengYeHong-HP:~$ apluserFengYeHong-HP:~$ls-l/home/apluser/work/requirements.txt -rw-r--r--1apluser apluser16Jun709:38 /home/apluser/work/requirements.txt四. registry仓库⏹Docker Registry是 Docker 提供的镜像仓库服务用于 存储、分发和管理 Docker 镜像。可使用Docker Registry 来上传push镜像下载pull镜像搭建私有镜像仓库Docker Registry常与docker login,docker push,docker pull命令配合使用。4.1docker pull镜像拉取⏹镜像拉取语法docker pull registry/仓库:标签# 从docker的官网仓库拉取nginx镜像dockerpull nginx:latest# 也可以写明官方仓库的id进行镜像拉取dockerpull docker.io/guohuiyuan/go-music-dl:latest# 从私人仓库拉取镜像dockerpull myregistry.com:5000/myimage:latest4.2docker push镜像推送⏹在推送镜像之前你需要为镜像添加仓库地址和标签。# docker tag 本地镜像ID或名称 registry/镜像名称:标签dockertag myapp:latest myregistry.com:5000/myapp:latest⏹向指定的仓库推送镜像# docker push registry/镜像名称:标签dockerpush myregistry.com:5000/myapp:latest