17

一起跟我来学dockerfile创建镜像

 5 years ago
source link: https://www.linuxprobe.com/dockerfile-create-mirror.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
导读 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。

docker命令语法
1.ADD
ADD命令有两个参数,源和目标。它的基本作用是从源系统的文件系统上复制文件到目标容器的文件系统。如果源是一个URL,那该URL的内容将被下载并复制到容器中
ADD /my_app_folder /my_app_folder

2.ENTRYPOINT
配置容器启动后执行的命令,并且不可被 docker run 提供的参数覆盖,每个 Dockerfile 中只能有一个 ENTRYPOINT,当指定多个时,只有最后一个起效。

3.ENV 
ENV命令用于设置环境变量。这些变量以”key=value”的形式存在,并可以在容器内被脚本或者程序调用。这个机制给在容器中运行应用带来了极大的便利。  
ENV PATH /usr/local/nginx/sbin:$PATH

4.EXPOSE 
EXPOSE用来指定端口,使容器内的应用可以通过端口和外界交互。 
EXPOSE 80

5.FROM 
这个命令用于声明作者,并应该放在FROM的后面。   
MAINTAINER authors_name 

6.RUN

7.USER
USER命令用于设置运行容器的UID。

8.WORKDIR
WORKDIR命令用于设置CMD指明的命令的运行目录。
dockerfile创建镜像

下面就构建一个简单的dockerfile

1.需要一个基础镜像

docker pull centos

2.在某一个目录下面创建一个专门存放此demo的目录,也就是Dockerfile所在的context:

[root@docker ~]# mkdir docker_demo
[root@docker ~]# cd docker_demo/
[root@docker docker_demo]# touch Dockerfile
[root@docker docker_demo]# pwd
/root/docker_demo
[root@docker docker_demo]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 1 04:34 Dockerfile

 

下载nginx源码包到docker_demo这个目录下:

[root@docker docker_demo]# ll
total 960
-rw-r--r--. 1 root root      0 Nov  1 04:34 Dockerfile
-rw-r--r--. 1 root root 981687 Oct 17 09:20 nginx-1.12.2.tar.gz

以下是编写好的Dockerfile v1版:

[root@docker docker_demo]# cat Dockerfile 
# base image
FROM centos
# MAINTAINER
MAINTAINER [email protected]
# put nginx-1.12.2.tar.gz into /usr/local/src and unpack nginx
ADD nginx-1.12.2.tar.gz /usr/local/src
# running required command
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel 
RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
RUN useradd -M -s /sbin/nologin nginx
# change dir to /usr/local/src/nginx-1.12.2
WORKDIR /usr/local/src/nginx-1.12.2
# execute command to compile nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install

ENV PATH /usr/local/nginx/sbin:$PATH


EXPOSE 80



[root@docker docker_demo]# docker build -t centos_nginx:v2 .
Sending build context to Docker daemon 985.6kB
Step 1/10 : FROM centos
---> 196e0ce0c9fb
Step 2/10 : MAINTAINER [email protected]
---> Using cache
---> cde1d7830106
Step 3/10 : ADD nginx-1.12.2.tar.gz /usr/local/src
---> Using cache
---> 1e4d16340af0
Step 4/10 : RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel
---> Using cache
---> 405835ad9b0b
Step 5/10 : RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
---> Using cache
---> 4002738cf7a6
Step 6/10 : RUN useradd -M -s /sbin/nologin nginx
---> Using cache
---> 02961c5c564d
Step 7/10 : WORKDIR /usr/local/src/nginx-1.12.2
---> Using cache
---> f1da71a93c5e
Step 8/10 : RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
---> Using cache
---> cd2ad4c45004
Step 9/10 : ENV PATH /usr/local/nginx/sbin:$PATH
---> Running in 07ba2f7129bc
---> 9588fa1058aa
Removing intermediate container 07ba2f7129bc
Step 10/10 : EXPOSE 80
---> Running in 473cd847154a
---> 2031faf8894a
Removing intermediate container 473cd847154a
Successfully built 2031faf8894a
Successfully tagged centos_nginx:v2

 

$ docker images

$ docker run -d -p81:80 centos_nginx nginx -g "daemon off;"

$ docker ps -l

最后通过浏览器访问就可以了


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK