41 【docker】初识

常用的docker命令:

docker ps  #查看当前正在运行的容器

docker ps -a | grep <keyword> #查看所有的容器,运行的或者停止的

docker stop <container-id> #停掉某个容器, 类似于主机的睡眠或者虚拟机的挂起

docker start <container-id> #重新启动某个容器,和刚才stop是相反的操作

docker rm <container-id> #删除某个容器

docker rmi <image-name> #删除某个image镜像

docker run ... #参数很多,根据已有镜像启动某个容器

docker build ... #用dockerfile构建自定义的镜像

docker insepct <container-id> #查看某个容器的各种信息

docker exec -it <container-id> /bin/bash #正在运行的镜像,我们想进去看看要使用该命令

sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container-ID>: 查看某个容器的IP

sudo docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(sudo docker ps -aq):查看所有的容器的IP

做如下实验:

1,构建一个镜像文件

创建文件,文件名为:dockerfile

FROM nginx

MAINTAINER [email protected]

然后执行命令:sudo docker build -t xin/luwenwei.nginx .

含义是使用当前文件夹下面的dockefile来构建镜像,镜像名称为<xin/luwenwei.nginx>

2,使用构建好的镜像文件启动一个容器

执行命令:

sudo docker run -d -p 8888:80 xin/luwenwei.nginx

使用刚才构建的镜像,创建一个容器,启动主机端口8888,映射到容器中的80端口

3,从各个角度来观察容器

3.1,查看容器的运行状态

# sudo docker ps

CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                  NAMES
f887c1378676        xin/luwenwei.nginx   "nginx -g 'daemon ..."   29 minutes ago      Up 29 minutes       0.0.0.0:8888->80/tcp   lucid_allen

可以看到,容器正在运行中。

3.2,查看容器的功能是否正常

luwenwei@localhost:~/docker/nginx$ curl 127.0.0.1:8888
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

可以看到,容器的80端口打开的,可以输出html内容。

3.3,到容器内部去看看

luwenwei@localhost:~/docker/nginx$ sudo docker exec -it f887c1378676  /bin/bash
root@f887c1378676:/# ps aux
bash: ps: command not found
root@f887c1378676:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@f887c1378676:/# 

猜你喜欢

转载自www.cnblogs.com/helww/p/10120091.html
41
41A