docker使用

本文介绍了Docker的基本命令,包括查看和搜索镜像、下载和运行容器等,并详细讲解了如何通过Dockerfile创建镜像及容器的各种操作方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2.1.命令帮助

Docker查看docker所有命令

Docker run --help查看具体命令使用说明

2.2.镜像

镜像网站
https://hub.docker.com/explore/
所有镜像都在这里
在这里插入图片描述

2.2.1.列出本机上的镜像

Docker images,

REPOSITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如ubuntu仓库源里,有15.10、14.04等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。

2.2.2.查找镜像

Docker search centos,

2.2.3.下载镜像

Docker pull 镜像:tag

Docker pull centos,默认tag是latest,latest是最新版本,如下图,即latest右边的centos7

如果拉取centos7,下载失败的,提示repository not found。

我是centos6.6,所以要用命令Docker pull centos:6.6

多阅读以下镜像的说明

2.2.4.运行镜像

Docker run hello-world
[root@localhost ~]# docker run hello-world
Unable to find image ‘hello-world:latest’ locally
latest: Pulling from hello-world
882673a3c694: Pull complete
83f0de727d85: Pull complete
Digest: sha256:4555e23a9cf5a1a216bd8b0d71b08a25e4144c2ecf6adb26df9620245ba99529
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:

  1. The Docker client contacted the Docker daemon.
  2. The Docker daemon pulled the “hello-world” image from the Docker Hub.
    (amd64)
  3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
  4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
先从网站下载镜像,然后输出内容,内容是镜像确定的

Docker run运行一个容器, 以centos:6.6镜像创建一个新容器,执行命令/bin/echo “Hello World”。
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
hello-world latest 83f0de727d85 11 days ago 1.848 kB
centos 6.6 abd4a196b77f 5 months ago 202.6 MB
[root@localhost ~]# docker run centos:6.6 /bin/echo “Hello World”
Hello World

2.3.镜像存储路径

/var/lib/docker

文件用户都是root, 所以切换到root用户

2.3.1.镜像
image/overlay2/imagedb/content/sha256下的文件和镜像对应

2.3.2.容器
Containers文件夹内文件名和docker ps -a的容器id对应

2.4.创建镜像

2.4.1.修改镜像

2.4.2.Dockerfile创建
dockerfile的命令都是大写的,比如:FROM、RUN等

2.5.容器

2.5.1.交互式容器

docker run -t -i centos:6.6 /bin/bash
-t 在新容器内指定一个终端,
-i 对容器内的标准输入stdin交互

[root@localhost ~]# docker run -t -i centos:6.6 /bin/bash
[root@6741ded7bac8 /]# pwd
/
[root@6741ded7bac8 /]# cat /proc/version
Linux version 2.6.32-504.el6.x86_64 (mockbuild@c6b9.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) ) #1 SMP Wed Oct 15 04:27:16 UTC 2014
[root@6741ded7bac8 /]# ls
bin etc lib lost+found mnt proc sbin srv tmp var
dev home lib64 media opt root selinux sys usr
[root@6741ded7bac8 /]# exit
Exit
[root@localhost ~]#
标志变成了[root@6741ded7bac8 /],说明进入容器了
在容器内执行cat /proc/version,ls等命令。
Exit退出容器

2.5.2.启动容器(后台模式)

docker run -d centos:6.6 /bin/sh -c “while true;do echo hello world;sleep 1;done”
-d, --detach=false Run container in background and print container ID,即后台运行

[root@localhost ~]# docker run -d centos:6.6 /bin/sh -c “while true;do echo hello world;sleep 1;done”
b76e968933da87cf9b7da5aa9019a8e9a6a03ca7da93805efe0345d996f6a0ac
打印出容器id。

Docker ps查看容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b76e968933da centos:6.6 "/bin/sh -c 'while t 9 seconds ago Up 9 seconds adoring_stallman
CONTAINER ID容器id,为b76e968933da。
NAMES容器名称,为adoring_stallman。

Docker logs 查看日志
后面用容器id的前面部分字符,或者容器名称的全部字符。
[root@localhost ~]# docker logs b7
hello world
hello world
[root@localhost ~]# docker logs adoring
Error response from daemon: no such id: adoring
[root@localhost ~]# docker logs adoring_stallman
hello world
hello world

2.5.3.停止容器
docker stop 容器id前面部分字符|容器名称全部字符
[root@localhost ~]# docker stop b7
b7
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#

2.5.4.重启容器
已经停止的容器,用Docker start adoring_stallman启动
正在运行的容器,用Docker restart adoring_stallman重启

2.5.5.删除容器
先停止容器,然后删除Docker rm adoring_stallman

2.6.Web容器
docker pull training/webapp,拉取python的web容器
[root@localhost ~]# docker pull training/webapp
latest: Pulling from training/webapp
e9e06b06e14c: Pull complete
a82efea989f9: Pull complete
37bea4ee0c81: Pull complete
07f8e8c5e660: Pull complete
23f0158a1fbe: Pull complete
0a4852b23749: Pull complete
7d0ff9745632: Pull complete
99b0d955e85d: Pull complete
33e109f2ff13: Pull complete
cc06fd877d54: Pull complete
b1ae241d644a: Pull complete
b37deb56df95: Pull complete
02a8815912ca: Already exists
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest

docker run -d -P training/webapp python app.py运行容器,
-d,后台运行
-p, --publish=[] Publish a container’s port(s) to the host,将容器端口映射到主机上
[root@localhost ~]# docker run -d -P training/webapp python app.py
50f9e5186b77952f07eee2910ed7083b1458406668ab51e161c3573fff02a38f
[root@localhost ~]#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值