拉取最新的Ubuntn官方镜像
[root@host xwk]# docker pull ubuntu:latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
查看该镜像的详细信息
docker inspect ubuntu
查看该镜像的构建历史
[root@host xwk]# docker history ubuntu
IMAGE CREATED CREATED BY SIZE COMMENT
ba6acccedd29 3 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
3 weeks ago /bin/sh -c #(nop) ADD file:5d68d27cc15a80653… 72.8MB
[root@host xwk]#
删除该镜像
[root@host xwk]# docker rmi ubuntu
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Deleted: sha256:ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1
Deleted: sha256:9f54eef412758095c8079ac465d494a2872e02e90bf1fb5f12a1641c0d1bb78b
[root@host xwk]#
以交互方式启动 ubuntu 容器
[root@host xwk]# docker history centos
Error response from daemon: No such image: centos:latest
[root@host xwk]# docker run -it ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
在该容器中执行以下命令安装 nginx
更新软件包
apt update
安装 Nginx
apt install -y nginx
过程中需要选择亚洲和上海
检查nginx服务版本
nginx -v
nginx version: nginx/1.18.0 (Ubuntu)
基于该容器生成新的镜像。
先退出容器,再执行 docker commit 命令将该容器提交并在本地生成新的镜像。
exit
[root@host xwk]# docker commit d7aaabf0105b ubuntu-with-nginx
sha256:a4d027ba7f66f4c93cfa8a9aa4f4bbfe8391a1e9be6be218cbd253ee6d358d58
[root@host xwk]#
d7aaabf0105b可以在命令行root@d7aaabf0105b看到,每个人应该都不一样
基于新的镜像启动新容器
以交互方式启动容器之后,在容器中执行 nginx 命令启动 Nginx 服务,然后使用 ps –aux
查看相关的进程,结果表明已成功运行 Nginx 服务
[root@host xwk]# docker run -it ubuntu-with-nginx /bin/bash
root@e839944e18c1:/#
root@e839944e18c1:/# nginx
root@e839944e18c1:/# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 4104 2168 pts/0 Ss 19:56 0:00 /bin/bash
root 9 0.0 0.1 55276 1444 ? Ss 19:57 0:00 nginx: master process nginx
www-data 10 0.0 0.2 55600 2100 ? S 19:57 0:00 nginx: worker process
root 11 0.0 0.1 5892 1412 pts/0 R+ 19:57 0:00 ps -aux
root@e839944e18c1:/#
[root@host bin]# docker run --rm -it -p 8000:80 ubuntu-with-nginx /bin/bash
root@afaa559381c6:/# nginx
exit
[root@host bin]# docker stop ubuntu-with-nginx
ubuntu-with-nginx
[root@host bin]#
准备构建 Dockerfile 构建上下文。
建立一个目录用作 Dockerfile 的构建上下文并准备所需的文件
[root@host bin]# mkdir ubuntu-nginx && cd ubuntu-nginx
[root@host ubuntu-nginx]#
编写 Dockerfile 文件。
可以使用 nano 进行编辑
nano Dockerfile
# 从基础镜像 ubuntu 开始构建
FROM ubuntu:latest
# 安装 nginx
RUN apt update && apt install -y nginx
# 修改 nginx 首页信息
RUN echo "Hello! This is nginx server " > /usr/share/nginx/html/index.html
# 对外暴露 80 端口
EXPOSE 80
# 启动 nginx
CMD ["nginx", "-g", "daemon off;"]
不要忘了后面的点号,代表当前路径
docker build -t ubuntu-with-nginx:1.0 .
等待运行完毕,出现下面语句代表安装成功
Successfully built 7175ab8a67d3
Successfully tagged ubuntu-with-nginx:1.0
基于该镜像启动容器进行测试
[root@host ubuntu-nginx]# docker run --rm -d -p 8000:80 --name test-nginx ubuntu-with-nginx:1.0
03a07c0ffa98e8ee63abf0a5aafd6d6ae3a57b30829a0ca2df5082568b336ca4
[root@host ubuntu-nginx]# curl 127.0.0.1:8000
<!DOCTYPE html>
Welcome to nginx!</title>