docker - dockerfile构建镜像
程序员文章站
2022-03-29 22:37:57
...
1.目录结构:
root@ubuntu:~/docker/dockerfile# pwd
/root/docker/dockerfile
2.在/root/docker/dockerfile
目录下下载和保存pcre和nginx
wget http://nginx.org/download/nginx-1.15.3.tar.gz
wget http://ftp.pcre.org/pub/pcre/pcre-8.37.tar.gz
3.在/root/docker/dockerfile
创建并编写Dockerfile文件了(注意Dockerfile的D需要大写)
root@ubuntu:~/docker/dockerfile# pwd
/root/docker/dockerfile
root@ubuntu:~/docker/dockerfile# ls
Dockerfile nginx-1.15.3.tar.gz pcre-8.37.tar.gz
Dockerfile
# This is My first Dockerfile
# Version 1.0
# Author: Bruce Wang
# Base images
FROM centos
# MAINTAINER
MAINTAINER Bruce Wang
# ADD <src> <dest>
ADD pcre-8.37.tar.gz /usr/local/src
ADD nginx-1.15.3.tar.gz /usr/local/src
#RUN
RUN yum install -y wget gcc gcc-c++ make openssl-devel
RUN useradd -s /sbin/nologin -M www
#WORKDIR
WORKDIR /usr/local/src/nginx-1.15.3
RUN ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.37 && make && make install
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
CMD ["nginx"]
简述下ADD
格式为 ADD src dest
该命令将复制指定的 到容器中的 。 其中 可以是Dockerfile所在目录的一个相对路径;也可以是一个URL;还可以是一个tar文件(自动解压为目录);刚开始pcre和nginx两个压缩包没有放在和Dockerfile同目录下,一直报错no such file or directory
;点击可以参考
4.执行docker build
进行构建
docker build --no-cache=true -t nginx-file:v1 /root/docker/dockerfile
5.构建成功后docker images
查看镜像
root@ubuntu:~/docker/dockerfile# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
nginx-file v1 45f6d8687605 About an hour ago 410.8 MB
wfq/my-nginx v1 f00523be45a1 2 days ago 425.5 MB
ubuntu latest cef111cd4bae 9 days ago 84.12 MB
nginx latest e37dd9b27b4f 10 days ago 109.1 MB
centos latest c5507be714a7 5 weeks ago 199.7 MB
6.运行镜像
docker run -d -p 93:80 nginx-file:v1
7.查看启动状态 up
,并将80端口映射到本机的93端口
root@ubuntu:~/docker/dockerfile# docker -l
flag needs an argument: -l
See 'docker --help'.
root@ubuntu:~/docker/dockerfile# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21508944c996 nginx-file:v1 "nginx" 13 seconds ago Up 13 seconds 0.0.0.0:93->80/tcp elegant_mestorf
推荐阅读