Docker筑基篇-上

本文档详细记录了在Linux环境下Docker的安装过程,包括卸载旧版本、安装依赖、设置阿里云镜像仓库、安装Docker引擎、启动Docker服务以及运行Hello-World验证。此外,还介绍了Docker的常用命令,如镜像搜索、下载、删除,以及容器的创建、启动、停止、删除等操作。最后提到了配置阿里镜像加速器的步骤,以提高Docker镜像下载速度。
摘要由CSDN通过智能技术生成

1.Docker安装心酸历程

step:

1.1.卸载旧的版本

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

1.2.安装一些docker需要的安装包

yum install -y yum-utils

1.3.设置镜像仓库

yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo #国外的非常慢
yum-config-manager \
         --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #推荐使用阿里云

1.4.安装docker引擎

yum install docker-ce docker-ce-cli containerd.io

1.5.启动docker

systemctl start docker

1.6.hello world

docker run hello-world

1.7.

docker version                 #docker版本信息
docker info                    #docker系统信息,包括镜像和容器数量
docker 命令 --help            #万能命令

1.8.

docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    bf756fb1ae65   12 months ago   13.3kB

执行过程:

login as: root
Last login: Tue Mar 23 17:57:54 2021 from localhost
[root@localhost ~]# sudo yum remove docker \
>                   docker-client \
>                   docker-client-latest \
>                   docker-common \
>                   docker-latest \
>                   docker-latest-logrotate \
>                   docker-logrotate \
>                   docker-engine
Loaded plugins: fastestmirror, langpacks
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-engine
No Packages marked for removal
[root@localhost ~]# yum install -y yum-utils
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package yum-utils.noarch 0:1.1.31-50.el7 will be updated
---> Package yum-utils.noarch 0:1.1.31-54.el7_8 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package           Arch           Version                    Repository    Size
================================================================================
Updating:
 yum-utils         noarch         1.1.31-54.el7_8            base         122 k

Transaction Summary
================================================================================
Upgrade  1 Package

Total download size: 122 k
Downloading packages:
No Presto metadata available for base
warning: /var/cache/yum/x86_64/7/base/packages/yum-utils-1.1.31-54.el7_8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for yum-utils-1.1.31-54.el7_8.noarch.rpm is not installed
yum-utils-1.1.31-54.el7_8.noarch.rpm                       | 122 kB   00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-6.1810.2.el7.centos.x86_64 (@anaconda)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : yum-utils-1.1.31-54.el7_8.noarch                             1/2
  Cleanup    : yum-utils-1.1.31-50.el7.noarch                               2/2
  Verifying  : yum-utils-1.1.31-54.el7_8.noarch                             1/2
  Verifying  : yum-utils-1.1.31-50.el7.noarch                               2/2

Updated:
  yum-utils.noarch 0:1.1.31-54.el7_8

Complete!
[root@localhost ~]# yum-config-manager \
>          --add-repo \
>     http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror, langpacks
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@localhost ~]# yum install docker-ce docker-ce-cli containerd.io
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.aliyun.com
docker-ce-stable                                         | 3.5 kB     00:00
(1/2): docker-ce-stable/7/x86_64/updateinfo                |   55 B   00:00
(2/2): docker-ce-stable/7/x86_64/primary_db                |  58 kB   00:00
Resolving Dependencies
--> Running transaction check
---> Package containerd.io.x86_64 0:1.4.4-3.1.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: containerd.io-1.4.4-3.1.el7.x86_64
---> Package docker-ce.x86_64 3:20.10.5-3.el7 will be installed
--> Processing Dependency: docker-ce-rootless-extras for package: 3:docker-ce-20.10.5-3.el7.x86_64
---> Package docker-ce-cli.x86_64 1:20.10.5-3.el7 will be installed
--> Running transaction check
---> Package container-selinux.noarch 2:2.119.2-1.911c772.el7_8 will be installed
---> Package docker-ce-rootless-extras.x86_64 0:20.10.5-3.el7 will be installed
--> Processing Dependency: fuse-overlayfs >= 0.7 for package: docker-ce-rootless-extras-20.10.5-3.el7.x86_64
--> Processing Dependency: slirp4netns >= 0.4 for package: docker-ce-rootless-extras-20.10.5-3.el7.x86_64
--> Running transaction check
---> Package fuse-overlayfs.x86_64 0:0.7.2-6.el7_8 will be installed
--> Processing Dependency: libfuse3.so.3(FUSE_3.2)(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
--> Processing Dependency: libfuse3.so.3(FUSE_3.0)(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
--> Processing Dependency: libfuse3.so.3()(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
---> Package slirp4netns.x86_64 0:0.4.3-4.el7_8 will be installed
--> Running transaction check
---> Package fuse3-libs.x86_64 0:3.6.1-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch   Version                   Repository        Size
================================================================================
Installing:
 containerd.io          x86_64 1.4.4-3.1.el7             docker-ce-stable  33 M
 docker-ce              x86_64 3:20.10.5-3.el7           docker-ce-stable  27 M
 docker-ce-cli          x86_64 1:20.10.5-3.el7           docker-ce-stable  33 M
Installing for dependencies:
 container-selinux      noarch 2:2.119.2-1.911c772.el7_8 extras            40 k
 docker-ce-rootless-extras
                        x86_64 20.10.5-3.el7             docker-ce-stable 9.1 M
 fuse-overlayfs         x86_64 0.7.2-6.el7_8             extras            54 k
 fuse3-libs             x86_64 3.6.1-4.el7               extras            82 k
 slirp4netns            x86_64 0.4.3-4.el7_8             extras            81 k

Transaction Summary
================================================================================
Install  3 Packages (+5 Dependent packages)

Total download size: 102 M
Installed size: 424 M
Is this ok [y/d/N]: y
Downloading packages:
(1/8): container-selinux-2.119.2-1.911c772.el7_8.noarch.rp |  40 kB   00:00
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-20.10.5-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for docker-ce-20.10.5-3.el7.x86_64.rpm is not installed
(2/8): docker-ce-20.10.5-3.el7.x86_64.rpm                  |  27 MB   00:23
(3/8): docker-ce-cli-20.10.5-3.el7.x86_64.rpm              |  33 MB   00:18
(4/8): fuse3-libs-3.6.1-4.el7.x86_64.rpm                   |  82 kB   00:00
(5/8): containerd.io-1.4.4-3.1.el7.x86_64.rpm              |  33 MB   00:42
(6/8): fuse-overlayfs-0.7.2-6.el7_8.x86_64.rpm             |  54 kB   00:01
(7/8): slirp4netns-0.4.3-4.el7_8.x86_64.rpm                |  81 kB   00:02
(8/8): docker-ce-rootless-extras-20.10.5-3.el7.x86_64.rpm  | 9.1 MB   00:04
--------------------------------------------------------------------------------
Total                                              2.2 MB/s | 102 MB  00:45
Retrieving key from https://download.docker.com/linux/centos/gpg
Importing GPG key 0x621E9F35:
 Userid     : "Docker Release (CE rpm) <docker@docker.com>"
 Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
 From       : https://download.docker.com/linux/centos/gpg
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch           1/8
  Installing : containerd.io-1.4.4-3.1.el7.x86_64                           2/8
  Installing : slirp4netns-0.4.3-4.el7_8.x86_64                             3/8
  Installing : fuse3-libs-3.6.1-4.el7.x86_64                                4/8
  Installing : fuse-overlayfs-0.7.2-6.el7_8.x86_64                          5/8
  Installing : 1:docker-ce-cli-20.10.5-3.el7.x86_64                         6/8
  Installing : docker-ce-rootless-extras-20.10.5-3.el7.x86_64               7/8
  Installing : 3:docker-ce-20.10.5-3.el7.x86_64                             8/8
  Verifying  : 1:docker-ce-cli-20.10.5-3.el7.x86_64                         1/8
  Verifying  : fuse3-libs-3.6.1-4.el7.x86_64                                2/8
  Verifying  : fuse-overlayfs-0.7.2-6.el7_8.x86_64                          3/8
  Verifying  : slirp4netns-0.4.3-4.el7_8.x86_64                             4/8
  Verifying  : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch           5/8
  Verifying  : 3:docker-ce-20.10.5-3.el7.x86_64                             6/8
  Verifying  : docker-ce-rootless-extras-20.10.5-3.el7.x86_64               7/8
  Verifying  : containerd.io-1.4.4-3.1.el7.x86_64                           8/8

Installed:
  containerd.io.x86_64 0:1.4.4-3.1.el7     docker-ce.x86_64 3:20.10.5-3.el7
  docker-ce-cli.x86_64 1:20.10.5-3.el7

Dependency Installed:
  container-selinux.noarch 2:2.119.2-1.911c772.el7_8
  docker-ce-rootless-extras.x86_64 0:20.10.5-3.el7
  fuse-overlayfs.x86_64 0:0.7.2-6.el7_8
  fuse3-libs.x86_64 0:3.6.1-4.el7
  slirp4netns.x86_64 0:0.4.3-4.el7_8

Complete!
[root@localhost ~]# systemctl start docker
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
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/get-started/
 
[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.5
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        55c4c88
 Built:             Tue Mar  2 20:33:55 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.5
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       363e9a8
  Built:            Tue Mar  2 20:32:17 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.4
  GitCommit:        05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc:
  Version:          1.0.0-rc93
  GitCommit:        12644e614e25b05da6fd08a38ffa0cfe1903fdec
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    d1165f221234   2 weeks ago   13.3kB
[root@localhost ~]#

2.配置阿里镜像加速器

[root@localhost ~]# sudo mkdir -p /etc/docker
[root@localhost ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": ["https://xdswrn5u.mirror.aliyuncs.com"]
> }
> EOF
{
  "registry-mirrors": ["https://xdswrn5u.mirror.aliyuncs.com"]
}
[root@localhost ~]# sudo systemctl daemon-reload
[root@localhost ~]# sudo systemctl restart docker

3.docker常用命令:

3.1.帮助命令:

docker -version #显示docker版本信息
docker info #显示docker的系统信息,包括镜像和容器数量
docker 命令 --help #帮助命令

docker官网帮助文档:
文档地址
左侧栏有一对命令可供学习
在这里插入图片描述

3.2.镜像命令:

3.2.1.普通命令

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   2 weeks ago    13.3kB
tomcat        9.0       040bdb29ab37   2 months ago   649MB
tomcat        latest    040bdb29ab37   2 months ago   649MB
nginx         latest    f6d0b4767a6c   2 months ago   133MB

#参数解释
REPOSITORY  镜像的仓库源
TAG         镜像的标签
IMAGE ID    镜像的id
CREATED     镜像的创建时间
SIZE        镜像的大小

#可选项:
-a , --all      #列出所有镜像
-q , --quiet    #只显示镜像id

[root@localhost ~]# docker images -a
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   2 weeks ago    13.3kB
tomcat        9.0       040bdb29ab37   2 months ago   649MB
tomcat        latest    040bdb29ab37   2 months ago   649MB
nginx         latest    f6d0b4767a6c   2 months ago   133MB

[root@localhost ~]# docker images -q
d1165f221234
040bdb29ab37
040bdb29ab37
f6d0b4767a6c

[root@localhost ~]# docker images -aq
d1165f221234
040bdb29ab37
040bdb29ab37
f6d0b4767a6c

[root@localhost ~]# docker images hello-world -q
d1165f221234


3.2.2.docker search 搜索镜像:

[root@localhost ~]# docker mysql
docker: 'mysql' is not a docker command.
See 'docker --help'
[root@localhost ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10656     [OK]
mariadb                           MariaDB Server is a high performing open sou…   3997      [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   779                  [OK]
percona                           Percona Server is a fork of the MySQL relati…   528       [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   87

#可选项:
-f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output
      
--filter=STARS=3000 #搜索出来的镜像就是收藏大于3000的

[root@localhost ~]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   10656     [OK]
mariadb   MariaDB Server is a high performing open sou…   3997      [OK]

[root@localhost ~]# docker search mysql --filter=STARS=5000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   10656     [OK]

3.2.3.docker pull 下载镜像:

分层下载:相同的层共用,不再重复下载,极大节省内存资源,显示为Already exists

#下载镜像 docker pull镜像名[:tag]
root@localhost ~]# docker pull nginx
Using default tag: latest #不写tag(版本,默认就是latest(最新的)
latest: Pulling from library/nginx
a076a628af6f: Pull complete  #分层下载,docker的核心就是联合文件系统
0732ab25fa22: Pull complete
d7f36f6fe38f: Pull complete
f72584a26f32: Pull complete
7125e4df9063: Pull complete
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest #真实地址

#以下两个命令是等价的
docker pull nginx
docker pull docker.io/library/nginx:latest

#指定版本下载,并且一定是dockerhub上面有的,如下图
docker pull nginx 1.18

在这里插入图片描述

3.2.3.docker rmi 删除镜像:

听说容器id写前三位就可以了

docker rmi -f 容器id #删除指定容器
docker rmi -f 容器id 容器id 容器id #删除多个容器
docker rmi -f ${docker images -aq} #删除全部容器

3.3.容器命令:

注意:有了镜像才能创建容器,相当于类跟实例的关系。

3.3.1创建一个centos来测试:

[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

3.3.2新建容器并启动:

docker run [可选参数] image

#参数说明
--name="Name" 容器名字,tomcat01,tomcat02用来区分容器
-d            后台方式运行
-it           使用交互方式运行,进入容器查看内容
-P            使用容器端口 -P 8080:8080
	-P ip:主机端口:容器端口
	-P 主机端口:容器端口(常用)
	-P 容器端口
-p            随机指定端口

#测试,启动并进入容器
[root@localhost ~]# docker run -it centos /bin/bash
[root@c6e6c5dc6f24 /]# ls #查看容器内部的centos,基础版本,很多命令都是不完善的
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr

#从容器退回主机
[root@c6e6c5dc6f24 /]# exit
exit
[root@localhost ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg

3.3.3.列出所有运行的容器:

#docker ps 命令
		#列出当前正在运行的容器
-a		#列出当前正在运行的容器+历史运行过的容器
-n=?		#列出最近创建的容器
-q      #只显示容器的编号
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED        STATUS        PORTS                    NAMES
aec63ded193c   tomcat    "catalina.sh run"        17 hours ago   Up 17 hours   0.0.0.0:3355->8080/tcp   tomcat01
3cb23d454a26   nginx     "/docker-entrypoint.…"   18 hours ago   Up 18 hours   0.0.0.0:3344->80/tcp     nginx_1
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS                     PORTS                    NAMES
c6e6c5dc6f24   centos        "/bin/bash"              5 minutes ago   Exited (0) 2 minutes ago                            vibrant_bassi
aec63ded193c   tomcat        "catalina.sh run"        17 hours ago    Up 17 hours                0.0.0.0:3355->8080/tcp   tomcat01
3cb23d454a26   nginx         "/docker-entrypoint.…"   18 hours ago    Up 18 hours                0.0.0.0:3344->80/tcp     nginx_1
53c6c1d7b511   hello-world   "/hello"                 26 hours ago    Exited (0) 26 hours ago                             priceless_sanderson
[root@localhost ~]# docker ps -a -n=1
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
c6e6c5dc6f24   centos    "/bin/bash"   8 minutes ago   Exited (0) 6 minutes ago             vibrant_bassi

3.3.4.退出容器:

exit  #直接停止容器并退出
ctrl+p+q #容器不停止退出

部署nginx
demo:

# 搜索nginx
docker search nginx
# 下载nginx镜像
docker pull nginx
# 查看镜像
docker images
# 后台,重命名,外部端口和内部端口映射 启动
docker run -d --name nginx_1 -p 3344:80 nginx
# 测试是否成功
curl localhost:3344
# 进入nginx容器内部
docker exec -it nginx_1 /bin/bash
        # 查看nginx位置
        whereis nginx
        # 进入nginx
        cd /etc/nginx
        ls
        # 退出容器,不停止应用
        ctrl+p+q

3.3.4.删除容器:

docker rm 容器id  #删除指定容器,但不能删除正在运行的容器,如果要强制删除 rm -f
docker rm -f ${docker ps -aq} #删除所有容器
docker ps -a -q|xargs docker rm #删除所有容器

3.3.4.启动和停止容器操作:

docker start 容器id    #启动容器
docker restart 容器id  #重启容器
docker stop 容器id     #停止正在运行的容器
docker kill 容器id     #强制停止正在运行的容器
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a076a628af6f: Pull complete
0732ab25fa22: Pull complete
d7f36f6fe38f: Pull complete
f72584a26f32: Pull complete
7125e4df9063: Pull complete
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   2 weeks ago    13.3kB
nginx         latest    f6d0b4767a6c   2 months ago   133MB
[root@localhost ~]# docker run -d --name nginx_1 -p 3344:80 nginx
3cb23d454a2624268fab7970442a168f50cd4b362a999c387c0957571b813898
[root@localhost ~]# curl localhost:3344
<!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>
   0.0.0.0:3344->80/tcp   nginx_1
[root@localhost ~]# docker exec -it nginx_1 /bin/bash
root@3cb23d454a26:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@3cb23d454a26:/# cd /etc/nginx
root@3cb23d454a26:/etc/nginx# ls
conf.d          koi-utf  mime.types  nginx.conf   uwsgi_params
fastcgi_params  koi-win  modules     scgi_params  win-utf
root@3cb23d454a26:/etc/nginx# read escape sequence
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                  NAMES
3cb23d454a26   nginx     "/docker-entrypoint.…"   20 minutes ago   Up 20 minutes   0.0.0.0:3344->80/tcp   nginx_1

部署tomcat9.0
此命令用完即删容器,镜像还在

[root@localhost ~]# docker run -it --rm tomcat:9.0

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   2 weeks ago    13.3kB
tomcat        9.0       040bdb29ab37   2 months ago   649MB
nginx         latest    f6d0b4767a6c   2 months ago   133MB

自行拉取运行
注意:访问tomcat主页可能会出现404,通过阿里镜像拉取的tomcat是被阉割掉的(只保证最小运行条件),没有了许多命令,webapps下并没有tomcat默认主页,这时我们可以将webapps.dist下的所有文件复制到webapps下就可以访问了。

部署tomcat
demo:

#从docker官网拉取镜像,注意拉去的版本要docker官网有才可以
docker pull tomcat
#启动运行,-d以后台方式运行,-p 3355:8080暴露端口,使得tomcat的8080端口映射到虚拟机3355端口
docker run -d -p 3355:8080 --name tomcat01 tomcat
curl localhost:3355
#进入tomcat容器
docker exec -it tomcat01 /bin/bash
#发现连ls命令都没有
ls
ls -al
#webapps放tomcat默认主页,这里是空的
cd webapps
cd ..
#保存webapps应该有的东西
cd webapps.dist
cd ..
#复制到webapps
cp -r webapps.dist/* webapps 
ls webapps 
 docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
Digest: sha256:94cc18203335e400dbafcd0633f33c53663b1c1012a13bcad58cced9cd9d1305
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
[root@localhost ~]# docker run -d -p 3355:8080 --name tomcat01 tomcat
aec63ded193c8918ffdd33c80f9d3dc45998c58582e9368883ab9b09d66aec5a
[root@localhost ~]# curl localhost:3355
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/9.0.41</h3></body></html>
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                    NAMES
aec63ded193c   tomcat    "catalina.sh run"        2 minutes ago    Up 2 minutes    0.0.0.0:3355->8080/tcp   tomcat01
3cb23d454a26   nginx     "/docker-entrypoint.…"   37 minutes ago   Up 37 minutes   0.0.0.0:3344->80/tcp     nginx_1
[root@localhost ~]# docker exec -it tomcat01 /bin/bash
root@aec63ded193c:/usr/local/tomcat# ls
BUILDING.txt     NOTICE         RUNNING.txt  lib             temp          work
CONTRIBUTING.md  README.md      bin          logs            webapps
LICENSE          RELEASE-NOTES  conf         native-jni-lib  webapps.dist
root@aec63ded193c:/usr/local/tomcat# ls -al
total 128
drwxr-xr-x. 1 root root    30 Jan 13 08:25 .
drwxr-xr-x. 1 root root    20 Jan 13 08:19 ..
-rw-r--r--. 1 root root 18982 Dec  3 11:48 BUILDING.txt
-rw-r--r--. 1 root root  5409 Dec  3 11:48 CONTRIBUTING.md
-rw-r--r--. 1 root root 57092 Dec  3 11:48 LICENSE
-rw-r--r--. 1 root root  2333 Dec  3 11:48 NOTICE
-rw-r--r--. 1 root root  3257 Dec  3 11:48 README.md
-rw-r--r--. 1 root root  6898 Dec  3 11:48 RELEASE-NOTES
-rw-r--r--. 1 root root 16507 Dec  3 11:48 RUNNING.txt
drwxr-xr-x. 2 root root  4096 Jan 13 08:25 bin
drwxr-xr-x. 1 root root    22 Mar 24 09:12 conf
drwxr-xr-x. 2 root root  4096 Jan 13 08:25 lib
drwxrwxrwx. 1 root root   177 Mar 24 09:12 logs
drwxr-xr-x. 2 root root   134 Jan 13 08:25 native-jni-lib
drwxrwxrwx. 2 root root    30 Jan 13 08:25 temp
drwxr-xr-x. 2 root root     6 Jan 13 08:25 webapps
drwxr-xr-x. 7 root root    81 Dec  3 11:45 webapps.dist
drwxrwxrwx. 2 root root     6 Dec  3 11:43 work
root@aec63ded193c:/usr/local/tomcat# cd webapps
root@aec63ded193c:/usr/local/tomcat/webapps# ls
root@aec63ded193c:/usr/local/tomcat/webapps# cd ..
root@aec63ded193c:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@aec63ded193c:/usr/local/tomcat# cd webapps
root@aec63ded193c:/usr/local/tomcat/webapps# ls
ROOT  docs  examples  host-manager  manager
root@aec63ded193c:/usr/local/tomcat/webapps# cd ..
root@aec63ded193c:/usr/local/tomcat# ls
BUILDING.txt     NOTICE         RUNNING.txt  lib             temp          work
CONTRIBUTING.md  README.md      bin          logs            webapps
LICENSE          RELEASE-NOTES  conf         native-jni-lib  webapps.dist
root@aec63ded193c:/usr/local/tomcat# ls webapps.dist
ROOT  docs  examples  host-manager  manager

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fire king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值