• Docker通过Dockerfile创建Redis、Nginx--详细过程


    创建Nginx镜像

    我们先创建一个目录,在目录里创建Dockerfile

    1. [root@docker-3 ~]# mkdir mynginx
    2. [root@docker-3 ~]# cd mynginx
    3. [root@docker-3 ~]# vim Dockerfile

    Dockerfile的内容

    1. FROM daocloud.io/library/centos:7
    2. RUN buildDeps='readline-devel pcre-devel openssl-devel gcc telnet wget curl make' && \
    3. yum -y install $buildDeps && \
    4. yum clean all
    5. RUN wget https://nginx.org/download/nginx-1.24.0.tar.gz && \
    6. tar zfx nginx-1.24.0.tar.gz -C /opt
    7. RUN cd /opt/nginx-1.24.0 && \
    8. mkdir /usr/local/nginx && \
    9. ./configure --prefix=/usr/local/nginx \
    10. --with-http_ssl_module \
    11. --with-http_stub_status_module && \
    12. make && make install && \
    13. rm -rf /usr/local/nginx/html/* && \
    14. echo "nihao" > /usr/local/nginx/html/index.html && \
    15. cd / && rm -rf /opt/nginx-1.24.0 && \
    16. ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    17. ENV PATH /usr/local/nginx/sbin:$PATH
    18. WORKDIR /usr/local/nginx
    19. EXPOSE 80
    20. CMD ["nginx","-g","daemon off;"]

    开始构建,别忘了最后的  . 

    [root@docker-3 mynginx]# docker build -t nginx:v1 .

    构建过程

    1. [+] Building 241.2s (9/9) FINISHED docker:default
    2. => [internal] load build definition from Dockerfile 0.0s
    3. => => transferring dockerfile: 824B 0.0s
    4. => [internal] load .dockerignore 0.0s
    5. => => transferring context: 2B 0.0s
    6. => [internal] load metadata for daocloud.io/library/centos:7 1.3s
    7. => CACHED [1/5] FROM daocloud.io/library/centos:7@sha256:fe2347002c630d5d61bf2f28f21246ad1c21cc6fd343e70b4cf1e5102f8711a9 0.0s
    8. => [2/5] RUN buildDeps='readline-devel pcre-devel openssl-devel gcc telnet wget curl make' && yum -y install $buildDe 210.5s
    9. => [3/5] RUN wget https://nginx.org/download/nginx-1.24.0.tar.gz && tar zfx nginx-1.24.0.tar.gz -C /opt 7.8s
    10. => [4/5] RUN cd /opt/nginx-1.24.0 && mkdir /usr/local/nginx && ./configure --prefix=/usr/local/nginx --with-ht 21.0s
    11. => [5/5] WORKDIR /usr/local/nginx 0.0s
    12. => exporting to image 0.6s
    13. => => exporting layers 0.6s
    14. => => writing image sha256:0cef6f4d7d9fdb2438160b5df566e442fe72b2eb04150ac0504d5af34472fb9d 0.0s
    15. => => naming to docker.io/library/nginx:v1 0.0s

    查看镜像

    1. [root@docker-3 mynginx]# docker images
    2. REPOSITORY TAG IMAGE ID CREATED SIZE
    3. nginx v1 0cef6f4d7d9f 10 seconds ago 325MB

    运行镜像

    1. [root@docker-3 mynginx]# docker run -itd --name nginxv1 -p 80:80 nginx:v1
    2. 3ba682f5bb1f12b6c318903e6d8e83fe22c7e9769c196d361862152da2d4c29d
    3. [root@docker-3 mynginx]# docker ps
    4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    5. 3ba682f5bb1f 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

    1. [root@docker-2 ~]# mkdir myRedis
    2. [root@docker-2 ~]# cd myRdis
    3. [root@docker-2 ~]# vim Dockerfile
    1. FROM daocloud.io/library/centos:7
    2. RUN yum -y install gcc gcc-c++ net-tools make wget \
    3. && mkdir -p /data/application/ \
    4. && wget https://download.redis.io/releases/redis-6.2.7.tar.gz \
    5. && tar zxf redis-6.2.7.tar.gz -C /data/application/
    6. RUN cd /data/application \
    7. && mv redis-6.2.7/ redis \
    8. && cd redis/ \
    9. && make \
    10. && sed -i -e 's/bind 127.0.0.1 -::1/bind 0.0.0.0/g' ./redis.conf
    11. EXPOSE 6379
    12. ENTRYPOINT /data/application/redis/src/redis-server /data/application/redis/redis.conf

    构建

    [root@docker-2 myRedis]# docker build -t redis:v1 .

    构建过程

    1. [+] Building 0.1s (7/7) FINISHED docker:default
    2. => [internal] load build definition from Dockerfile 0.0s
    3. => => transferring dockerfile: 591B 0.0s
    4. => [internal] load .dockerignore 0.0s
    5. => => transferring context: 2B 0.0s
    6. => [internal] load metadata for daocloud.io/library/centos:7 0.0s
    7. => [1/3] FROM daocloud.io/library/centos:7 0.0s
    8. => CACHED [2/3] RUN yum -y install gcc gcc-c++ net-tools make wget && mkdir -p /data/application/ && wget https://d 0.0s
    9. => CACHED [3/3] RUN cd /data/application && mv redis-6.2.7/ redis && cd redis/ && make && mkdir /data/appli 0.0s
    10. => exporting to image 0.0s
    11. => => exporting layers 0.0s
    12. => => writing image sha256:15c74862351b269ae259a12d6c8d43a4e68dd97db5363acdd7e965c885b33a15 0.0s
    13. => => naming to docker.io/library/redis:v1 0.0s

    运行容器

    1. [root@docker-2 myRedis]# vim Dockerfile
    2. [root@docker-2 myRedis]# docker images
    3. REPOSITORY TAG IMAGE ID CREATED SIZE
    4. redis v1 15c74862351b 6 days ago 672MB
    5. [root@docker-2 myRedis]# docker run -itd --name redis1 -p 6379:6379 redis:v1
    6. dc76e2462b7ae0a63ed673bc2f247302a1bec336bc6320a5f67c5cd2051ebf41
    7. [root@docker-2 myRedis]# docker ps
    8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    9. dc76e2462b7a 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

    1. [root@docker-2 myRedis]# docker exec -it redis1 /bin/sh
    2. sh-4.2# ls
    3. anaconda-post.log bin data dev etc home lib lib64 media mnt opt proc redis-6.2.7.tar.gz root run sbin srv sys tmp usr var
    4. sh-4.2# cd data/application/redis/
    5. sh-4.2# ./src/redis-cli
    6. 127.0.0.1:6379> ping
    7. PONG
    8. 127.0.0.1:6379>

    我这里只修改了配置文件的bind,  如果有需要其他使用sed更改即可

  • 相关阅读:
    html5 文字自动省略,html中把多余文字转化为省略号的实现方法方法
    C++指针传递和引用传递(函数参数中&和*的区别)
    Kylin
    Python下划线中 _xx、__xx、__xx__ 的区别
    阿里巴巴中国站获取1688app上原数据 API 返回值说明
    Git入门---简介,常用命令
    高德地图API-鼠标点击地图获取经纬度坐标(关键操作)
    后端存储实战课——高速增长篇
    [windows][操作系统]复制文件夹到桌面经常到跑左上角导致桌面图标位置错乱
    C++ 通过CreateProcess函数和ShellExecute函数打开另一个exe
  • 原文地址:https://blog.csdn.net/qq_61838278/article/details/133556746