docker生成镜像的两种方式

更新镜像

先使用基础镜像创建一个容器,然后对容器内容进行更改,然后使用docker commit命令提交为一个新的镜像

docker search centos

docker pull centos

docker images

docker run -it --name=web centos /bin/bash
# CentOS8已于2021年12月31日寿终正寝,但软件包仍在官方镜像上保留了一段时间。现在他们被转移到https://vault.centos.org,所以需要更改源地址
vi /etc/yum.repos.d/CentOS-Linux-BaseOS.repo
[baseos]
name=CentOS Linux $releasever - BaseOS
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/BaseOS/$basearch/os/
baseurl=https://vault.centos.org/centos/$releasever/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
vi /etc/yum.repos.d/CentOS-Linux-AppStream.repo
[appstream]
name=CentOS Linux $releasever - AppStream
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=AppStream&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/AppStream/$basearch/os/
baseurl=https://vault.centos.org/centos/$releasever/AppStream/$basearch/os/ 
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
yum clean all
yum install -y wget vim
# nginx华为云加速镜像源
wget https://mirrors.huaweicloud.com/nginx/nginx-1.9.9.tar.gz
# tar.gz源码包解压
tar -zxvf nginx-1.9.9.tar.gz
# nginx依赖库
yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 解决不识别make命令依赖
yum -y install gcc automake autoconf libtool make
# 进入nginx源码目录
cd nginx-1.9.9

vim objs/Makefile
删除 -Werror -g 添加 -Wno-implicit-fallthrough

vim src/os/unix/ngx_user.c
注释 cd.current_salt[0] = ~salt[0];

# 配置nginx安装选项
./configure --prefix=/usr/local/nginx

# 编译并安装
make && make install

# 启动nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

# 删除nginx-1.9.9.tar.gz和nginx-1.9.9并退出容器
# 查看docker commit命令帮助
[root@localhost data]# docker commit --help

Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)
[root@localhost data]# docker commit -m "complete nginx in centos" web buddha/centos_nginx:v1
sha256:daf50d8dc9ef9f30df637ea065639bef4a2d7106e6989952b64db77d4c1d2e39

[root@localhost data]# docker images
REPOSITORY                  TAG          IMAGE ID       CREATED         SIZE
buddha/centos_nginx         v1           daf50d8dc9ef   2 minutes ago   555MB

# /usr/local/nginx/sbin/nginx 指在容器里面执行的命令
# -g 添加指令到nginx的配置文件
# daemon off 指nginx服务不运行在后端而是在前台运行
docker run -d -p80:80 --name web buddha/centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"
# 先要去https://hub.docker.com/注册账号
# docker login后输入用户名和密码,注意不是注册的时候邮箱
docker login
# 推送镜像的规范是:注册的用户名/镜像名:版本号
# 要推送的镜像修改为规范的镜像名
docker tag buddha/centos_nginx:v1 3539949703/nginx:v1

# 往hub.docker.com推送镜像
docker push 3539949703/nginx:v1

在这里插入图片描述

# 退出登陆状态
docker logout

构建镜像

创建Dockerfile文件,再使用docker build命令构建一个新的镜像

[root@localhost data]# tree ../data
../data
├── Dockerfile
└── repo
    ├── CentOS-Linux-AppStream.repo
    └── CentOS-Linux-BaseOS.repo
# 基础镜像
FROM centos

# 维护者
MAINTAINER 3539949703@qq.com

# 切换到/etc/yum.repos.d/目录
WORKDIR /etc/yum.repos.d

# COPY /data/repo/CentOS-Linux-BaseOS.repo .这样是错的,不能用绝对路径
COPY repo/CentOS-Linux-BaseOS.repo .
COPY repo/CentOS-Linux-AppStream.repo .

# 清除缓存
RUN yum clean all

# 切换到usr/lcoal/src/目录
WORKDIR /usr/local/src

# 添加远程文件到当前文件夹, 注意:后面有个点(.) 代表当前目录。ADD可以添加远程文件到镜像,但COPY仅可以添加本地文件到镜像中。
ADD nginx-1.9.9.tar.gz .

#RUN yum install -y vim wget
#ADD wget wget https://mirrors.huaweicloud.com/nginx/nginx-1.9.9.tar.gz

# 切换目录
WORKDIR /usr/local/src/nginx-1.9.9

# 安装必要的软件和添加nginx用户
RUN yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
RUN yum -y install gcc automake autoconf libtool make
RUN  useradd -M -s /sbin/nologin nginx

# 编译安装nginx
RUN ./configure --prefix=/usr/local/nginx && make && make install

# 切换目录
WORKDIR /usr/local/src

# 删除远程下载的镜像源
RUN rm -rf nginx-1.9.9 nginx-1.9.9.tar.gz

# 设置变量,执行命令时,就可以省略前缀目录了	
ENV PATH /usr/local/nginx/sbin:$PATH

# 暴露端口
EXPOSE 80

# the command of entrypoint
ENTRYPOINT ["nginx"]

# 执行命令,数组形式, "-g daemon off;" 使我们运行容器时,容器可以前台运行,不会退出
CMD ["-g", "daemon off;"]
[root@localhost data]# docker build --help

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

Options:
      --add-host list           Add a custom host-to-IP mapping (host:ip)
      --build-arg list          Set build-time variables
      --cache-from strings      Images to consider as cache sources
      --cgroup-parent string    Optional parent cgroup for the container
      --compress                Compress the build context using gzip
      --cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota
  -c, --cpu-shares int          CPU shares (relative weight)
      --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
      --disable-content-trust   Skip image verification (default true)
  -f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')
      --force-rm                Always remove intermediate containers
      --iidfile string          Write the image ID to the file
      --isolation string        Container isolation technology
      --label list              Set metadata for an image
  -m, --memory bytes            Memory limit
      --memory-swap bytes       Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --network string          Set the networking mode for the RUN instructions during build (default "default")
      --no-cache                Do not use cache when building the image
      --pull                    Always attempt to pull a newer version of the image
  -q, --quiet                   Suppress the build output and print image ID on success
      --rm                      Remove intermediate containers after a successful build (default true)
      --security-opt strings    Security options
      --shm-size bytes          Size of /dev/shm
  -t, --tag list                Name and optionally a tag in the 'name:tag' format
      --target string           Set the target build stage to build.
      --ulimit ulimit           Ulimit options (default [])
docker build -t 3539949703/nginx:v1 .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员buddha2080

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

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

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

打赏作者

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

抵扣说明:

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

余额充值