helloworld运行一次run多一个容器,我说呢怎么没效果,原来ps看到运行几次就新建几个容器run ubuntu也一样

docker ps -a                           列出容器,显示所有容器,不加-a默认只显示正在运行的
docker run                             创建一个新的容器并运行一个命令
docker run -it --name centos-test centos:centos7(创建容器必用的命令)
docker run -itd --name centos-test centos:centos7

这里run创建一般会绑定一个端口号映射到宿主机。后面部署项目才能通过宿主机的IP实现内网访问,如果不绑定只能宿主机访问,内网其他主机访问不了。

-d: 分离/后台模式: 在后台运行容器,并返回容器ID,类似screen -d 服务名,运行python定时任务时常使用
    所谓在后台运行就是还可以继续在当前终端运行其他命令而不会影响分离的那个服务
-i: 以交互模式运行容器,通常与 -t 同时使用
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用
--name="xxx": 为容器指定一个名称
/bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash
 

docker create            用法同 docker run  创建一个新的容器但不启动它
docker start/stop/restart 容器名/容器id     启动/停止/重启容器
(当run创建之后就可以进行启动停止容器了,这里启动停止并不会进入容器,所以一般启动成功后还得进入容器才能用命令操作容器)
docker images                     列出本地所有镜像
docker rm  -f 容器名/容器id       删除/强制删除容器
docker rmi -f 镜像名/镜像id       删除/强制删除镜像
docker exec -it  容器名/容器id /bin/bash 
进入容器(run是创建容器,所以一般得有镜像,即以某个镜像创建容器,exec是必须存在且正在运行)
进入当前容器后开启一个新的终端,可以在里面操作。(常用)
docker attach 容器名/容器id    进入容器,与exec不同的是退出容器会导致容器停止,这不是我们想要的,所以一般不推荐使用这个命令。
docker inspect : 获取容器/镜像的元数据。
docker cp :用于容器与主机之间的数据拷贝。
docker logs        获取容器的日志(可以查看到都使用了哪些命令)

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
ubuntu        latest    dc4c1391d370   47 hours ago    78.1MB
hello-world   latest    d2c94e258dcb   17 months ago   13.3kB
centos        7         eeb6ee3f44bd   3 years ago     204MB
[root@localhost ~]# docker run hello-world

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/get-started/

[root@localhost ~]# docker ps -a  
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
7ff1c85e7407   hello-world   "/hello"   31 seconds ago   Exited (0) 30 seconds ago             suspicious_merkle
cd866437da41   ubuntu        "bash"     2 minutes ago    Up 2 minutes                          gallant_elion
a5373431efc8   hello-world   "/hello"   4 minutes ago    Exited (0) 4 minutes ago              exciting_feistel
d374bf549106   hello-world   "/hello"   14 minutes ago   Exited (0) 14 minutes ago             nice_rhodes
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS         PORTS     NAMES
cd866437da41   ubuntu    "bash"    2 minutes ago   Up 2 minutes             gallant_elion
[root@localhost ~]# docker ps
docker ps -a  
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS         PORTS     NAMES
cd866437da41   ubuntu    "bash"    3 minutes ago   Up 3 minutes             gallant_elion
[root@localhost ~]# docker ps -a  
CONTAINER ID   IMAGE         COMMAND    CREATED          STATUS                      PORTS     NAMES
7ff1c85e7407   hello-world   "/hello"   54 seconds ago   Exited (0) 53 seconds ago             suspicious_merkle
cd866437da41   ubuntu        "bash"     3 minutes ago    Up 3 minutes                          gallant_elion
a5373431efc8   hello-world   "/hello"   4 minutes ago    Exited (0) 4 minutes ago              exciting_feistel
d374bf549106   hello-world   "/hello"   14 minutes ago   Exited (0) 14 minutes ago             nice_rhodes
[root@localhost ~]# docker run hello-world

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/get-started/

[root@localhost ~]# docker ps
docker ps -a  
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS         PORTS     NAMES
cd866437da41   ubuntu    "bash"    3 minutes ago   Up 3 minutes             gallant_elion
[root@localhost ~]# docker ps -a  
CONTAINER ID   IMAGE         COMMAND    CREATED              STATUS                          PORTS     NAMES
b6e3bb2ad818   hello-world   "/hello"   3 seconds ago        Exited (0) 2 seconds ago                  condescending_jackson
7ff1c85e7407   hello-world   "/hello"   About a minute ago   Exited (0) About a minute ago             suspicious_merkle
cd866437da41   ubuntu        "bash"     3 minutes ago        Up 3 minutes                              gallant_elion
a5373431efc8   hello-world   "/hello"   5 minutes ago        Exited (0) 5 minutes ago                  exciting_feistel
d374bf549106   hello-world   "/hello"   15 minutes ago       Exited (0) 15 minutes ago                 nice_rhodes
[root@localhost ~]# docker exec -it  gallant_elion /bin/bash 
root@cd866437da41:/# uname -a
Linux cd866437da41 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
root@cd866437da41:/# ifconfig
bash: ifconfig: command not found
root@cd866437da41:/# whoami
root
root@cd866437da41:/# df -h
Filesystem               Size  Used Avail Use% Mounted on
overlay                   58G  9.9G   49G  18% /
tmpfs                     64M     0   64M   0% /dev
tmpfs                    5.3G     0  5.3G   0% /sys/fs/cgroup
shm                       64M     0   64M   0% /dev/shm
/dev/mapper/centos-root   58G  9.9G   49G  18% /etc/hosts
tmpfs                    5.3G     0  5.3G   0% /proc/asound
tmpfs                    5.3G     0  5.3G   0% /proc/acpi
tmpfs                    5.3G     0  5.3G   0% /proc/scsi
tmpfs                    5.3G     0  5.3G   0% /sys/firmware
root@cd866437da41:/# exit
exit
[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 5.3G     0  5.3G    0% /dev
tmpfs                    5.3G     0  5.3G    0% /dev/shm
tmpfs                    5.3G  9.7M  5.3G    1% /run
tmpfs                    5.3G     0  5.3G    0% /sys/fs/cgroup
/dev/mapper/centos-root   58G  9.9G   49G   18% /
/dev/sda1               1014M  186M  829M   19% /boot
pub                      190G  134G   56G   71% /pub
tmpfs                    1.1G   36K  1.1G    1% /run/user/0
/dev/sr0                  59M   59M     0  100% /run/media/root/VBox_GAs_6.1.16
overlay                   58G  9.9G   49G   18% /var/lib/docker/overlay2/456ceaa518985e44117c63de121919d39af9a35e52e37908a891c5dc8e729beb/merged
[root@localhost ~]# 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐