Docker容器入门操作之常用命令

1. 运行容器

-i : 保持容器一直运行着

-t: 给容器分配一个终端

(1)运行并进入容器内部

(2)退出容器

(3)-d : 后台 运行容器,就是创建完容器后不会立即进入容器,需要通过命令才能进入,通过exit退出后容器也不会自动关闭

[root@hadoop101 ~]# docker run --name mytomcat -d  tomcat:latest
c78b5046fb352c443ddea780fee85fb492c335c0d943569c51ca40bbee5d7332

(4)进入容器:docker exec

2.查看运行的容器

[root@hadoop101 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
c78b5046fb35        tomcat:latest       "catalina.sh run"   39 seconds ago      Up 35 seconds   

3.查看容器的信息

3.本机主机访问tomcat

(1)直接访问失败


 

原因分析:Linux系统上的tomcat是以docker的形式安装的,在虚拟机 运行tomcat容器,端口号8080;但是,我们访问

http://192.168.93.101:8080/ 这个是访问虚拟机的8080,并不是访问容器内部的端口,因此访问不到虚拟机中的tomcat,我们需要进行映射。

(2)先停止运行的容器

[root@hadoop101 ~]# docker stop c78b5046fb35
c78b5046fb35
[root@hadoop101 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@hadoop101 ~]# 

(3)查看所有的容器,包括运行中的和停止的

[root@hadoop101 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS                    NAMES
c78b5046fb35        tomcat:latest       "catalina.sh run"        14 minutes ago      Exited (143) 3 minutes ago                            mytomcat
7d7877091164        cst_centos:1        "/bin/sh -c /bin/bash"   2 months ago        Exited (137) 2 months ago                             cc1
a2a5dde0e25f        tomcat:latest       "catalina.sh run"        2 months ago        Exited (130) 2 months ago                             t1
ffa1878485cd        redis:latest        "docker-entrypoint.s…"   2 months ago        Exited (0) 2 months ago                               r1
481a8b97d7a6        501d26cf83b5        "/bin/sh -c 'yum ins…"   2 months ago        Exited (1) 2 months ago                               ecstatic_perlman
14bf86841e7f        centos:7            "/bin/bash"              2 months ago        Exited (137) 2 months ago                             c1
1c47f3115d61        dhu_tomcat:1.0      "/bin/bash"              2 months ago        Exited (255) 2 months ago    8080/tcp                 new_tomcat
0d25c3ec315c        redis:latest        "docker-entrypoint.s…"   2 months ago        Exited (255) 2 months ago    0.0.0.0:6379->6379/tcp   c_redis
f81a568b92ff        nginx               "nginx -g 'daemon of…"   2 months ago        Exited (0) 2 months ago      0.0.0.0:80->80/tcp       d_nginx
6b9b8a79841d        tomcat              "catalina.sh run"        2 months ago        Exited (255) 2 months ago    0.0.0.0:8080->8080/tcp   c_tomcat
a8ee730df7bf        centos:7            "/bin/bash"              2 months ago        Exited (137) 2 months ago                             c2
5aed62e3d810        centos:7            "/bin/bash"              2 months ago        Exited (137) 2 months ago                             c3

(4)删除容器,确保容器是停止状态

[root@hadoop101 ~]# docker rm c78b5046fb35
c78b5046fb35

4. 进行端口映射,即虚拟机的端口映射到某一个运行的容器端口上

5. 使用端口映射来启动tomcat容器

(1)-d:后台运行
-p: 将主机的端口映射到容器的一个端口    主机端口:容器内部的端口

[root@hadoop101 ~]# docker run -d -p 8888:8080 tomcat 
b14b78940c549f3dcf1f2f8deead89930e65d5ea474d0cb864e03043065f060a
[root@hadoop101 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
b14b78940c54        tomcat              "catalina.sh run"   10 seconds ago      Up 9 seconds        0.0.0.0:8888->8080/tcp   hopeful_turing

这里,解决首次访问http://192.168.93.101:8080/ 报404的问题 

 (2)进入正在运行的tomcat容器的目录

[root@hadoop101 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
6da0ae9a729d        tomcat              "catalina.sh run"   9 seconds ago       Up 7 seconds        0.0.0.0:8080->8080/tcp   goofy_swirles
[root@hadoop101 ~]# docker exec -it 6da0ae9a729d /bin/bash

(3)进入tomcat的webapps目录

root@6da0ae9a729d:/usr/local/tomcat# cd webapps
root@6da0ae9a729d:/usr/local/tomcat/webapps# ll
bash: ll: command not found
root@6da0ae9a729d:/usr/local/tomcat/webapps# ls
root@6da0ae9a729d:/usr/local/tomcat/webapps# ls -al
total 0

 (4)将webapps.dist 的内容复制到 webapps

root@6da0ae9a729d:/usr/local/tomcat# cp -r webapps.dist webapps
root@6da0ae9a729d:/usr/local/tomcat# cd webapps
root@6da0ae9a729d:/usr/local/tomcat/webapps# ls -al
total 8

 (5)再次访问,成功~

6.查看防火墙状态

[root@hadoop101 ~]# service firewalld status

[root@hadoop101 ~]# service firewalld status
Redirecting to /bin/systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

7.一个镜像可以同时启动多个容器

 更多命令参看
https://docs.docker.com/engine/reference/commandline/docker/
可以参考每一个镜像的文档

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值