创建Nginx镜像
我们先创建一个目录,在目录里创建Dockerfile
[root@docker-3 ~]# mkdir mynginx[root@docker-3 ~]# cd mynginx[root@docker-3 ~]# vim Dockerfile
Dockerfile的内容
FROM daocloud.io/library/centos:7RUN buildDeps='readline-devel pcre-devel openssl-devel gcc telnet wget curl make' && \yum -y install $buildDeps && \yum clean allRUN wget https://nginx.org/download/nginx-1.24.0.tar.gz && \tar zfx nginx-1.24.0.tar.gz -C /optRUN cd /opt/nginx-1.24.0 && \mkdir /usr/local/nginx && \./configure --prefix=/usr/local/nginx \--with-http_ssl_module \--with-http_stub_status_module && \make && make install && \rm -rf /usr/local/nginx/html/* && \echo "nihao" > /usr/local/nginx/html/index.html && \cd / && rm -rf /opt/nginx-1.24.0 && \ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeENV PATH /usr/local/nginx/sbin:$PATHWORKDIR /usr/local/nginxEXPOSE 80CMD ["nginx","-g","daemon off;"]
开始构建,别忘了最后的 .
[root@docker-3 mynginx]# docker build -t nginx:v1 .
构建过程
[+] Building 241.2s (9/9) FINISHEDdocker:default => [internal] load build definition from Dockerfile0.0s => => transferring dockerfile: 824B0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for daocloud.io/library/centos:7 1.3s => CACHED [1/5] FROM daocloud.io/library/centos:7@sha256:fe2347002c630d5d61bf2f28f21246ad1c21cc6fd343e70b4cf1e5102f8711a90.0s => [2/5] RUN buildDeps='readline-devel pcre-devel openssl-devel gcc telnet wget curl make' && yum -y install $buildDe210.5s => [3/5] RUN wget https://nginx.org/download/nginx-1.24.0.tar.gz && tar zfx nginx-1.24.0.tar.gz -C /opt7.8s => [4/5] RUN cd /opt/nginx-1.24.0 && mkdir /usr/local/nginx && ./configure --prefix=/usr/local/nginx --with-ht21.0s => [5/5] WORKDIR /usr/local/nginx0.0s => exporting to image0.6s => => exporting layers 0.6s => => writing image sha256:0cef6f4d7d9fdb2438160b5df566e442fe72b2eb04150ac0504d5af34472fb9d0.0s => => naming to docker.io/library/nginx:v1 0.0s
查看镜像
[root@docker-3 mynginx]# docker imagesREPOSITORY TAG IMAGE ID CREATEDSIZEnginxv10cef6f4d7d9f 10 seconds ago 325MB
运行镜像
[root@docker-3 mynginx]# docker run -itd --name nginxv1 -p 80:80 nginx:v13ba682f5bb1f12b6c318903e6d8e83fe22c7e9769c196d361862152da2d4c29d[root@docker-3 mynginx]# docker psCONTAINER ID IMAGECOMMAND CREATED STATUS PORTS NAMES3ba682f5bb1f nginx:v1 "nginx -g 'daemon of…" 7 seconds ago Up 7 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginxv1
网页查看
创建Redis镜像
创建目录,在目录里创建Dockerfile
[root@docker-2 ~]# mkdir myRedis[root@docker-2 ~]# cd myRdis[root@docker-2 ~]# vim Dockerfile
FROM daocloud.io/library/centos:7RUN yum -y install gcc gcc-c++ net-tools make wget \&& mkdir -p /data/application/ \&& wget https://download.redis.io/releases/redis-6.2.7.tar.gz \&& tar zxf redis-6.2.7.tar.gz -C/data/application/RUN cd /data/application \&& mv redis-6.2.7/ redis \&& cd redis/ \&& make \&& sed -i -e 's/bind 127.0.0.1 -::1/bind 0.0.0.0/g' ./redis.confEXPOSE 6379ENTRYPOINT /data/application/redis/src/redis-server /data/application/redis/redis.conf
构建
[root@docker-2 myRedis]# docker build -t redis:v1 .
构建过程
[+] Building 0.1s (7/7) FINISHEDdocker:default => [internal] load build definition from Dockerfile0.0s => => transferring dockerfile: 591B0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for daocloud.io/library/centos:7 0.0s => [1/3] FROM daocloud.io/library/centos:7 0.0s => CACHED [2/3] RUN yum -y install gcc gcc-c++ net-tools make wget && mkdir -p /data/application/ && wget https://d0.0s => CACHED [3/3] RUN cd /data/application && mv redis-6.2.7/ redis && cd redis/ && make && mkdir /data/appli0.0s => exporting to image0.0s => => exporting layers 0.0s => => writing image sha256:15c74862351b269ae259a12d6c8d43a4e68dd97db5363acdd7e965c885b33a150.0s => => naming to docker.io/library/redis:v1 0.0s
运行容器
[root@docker-2 myRedis]# vim Dockerfile [root@docker-2 myRedis]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEredisv115c74862351b 6 days ago672MB[root@docker-2 myRedis]# docker run -itd --name redis1 -p 6379:6379 redis:v1dc76e2462b7ae0a63ed673bc2f247302a1bec336bc6320a5f67c5cd2051ebf41[root@docker-2 myRedis]# docker psCONTAINER ID IMAGECOMMAND CREATED STATUS PORTS NAMESdc76e2462b7a redis:v1 "/bin/sh -c '/data/a…" 4 seconds ago Up 3 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis1
进入容器运行Redis
[root@docker-2 myRedis]# docker exec -it redis1 /bin/shsh-4.2# lsanaconda-post.logbindatadevetchomeliblib64mediamntoptprocredis-6.2.7.tar.gzrootrunsbinsrvsystmpusrvarsh-4.2# cd data/application/redis/sh-4.2# ./src/redis-cli 127.0.0.1:6379> pingPONG127.0.0.1:6379>
我这里只修改了配置文件的bind, 如果有需要其他使用sed更改即可