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

创建Nginx镜像

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

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

Dockerfile的内容

FROM daocloud.io/library/centos:7
RUN buildDeps='readline-devel pcre-devel openssl-devel gcc telnet wget curl make' && \
    yum -y install $buildDeps && \
    yum clean all
RUN wget https://nginx.org/download/nginx-1.24.0.tar.gz && \
    tar zfx nginx-1.24.0.tar.gz -C /opt
RUN 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/localtime
ENV PATH /usr/local/nginx/sbin:$PATH
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["nginx","-g","daemon off;"]

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

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

构建过程

[+] Building 241.2s (9/9) FINISHED                                                                                  docker:default
 => [internal] load build definition from Dockerfile                                                                          0.0s
 => => transferring dockerfile: 824B                                                                                          0.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:fe2347002c630d5d61bf2f28f21246ad1c21cc6fd343e70b4cf1e5102f8711a9    0.0s
 => [2/5] RUN buildDeps='readline-devel pcre-devel openssl-devel gcc telnet wget curl make' &&     yum -y install $buildDe  210.5s
 => [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
 => [4/5] RUN cd /opt/nginx-1.24.0 &&     mkdir /usr/local/nginx &&     ./configure --prefix=/usr/local/nginx     --with-ht  21.0s
 => [5/5] WORKDIR /usr/local/nginx                                                                                            0.0s
 => exporting to image                                                                                                        0.6s
 => => exporting layers                                                                                                       0.6s
 => => writing image sha256:0cef6f4d7d9fdb2438160b5df566e442fe72b2eb04150ac0504d5af34472fb9d                                  0.0s
 => => naming to docker.io/library/nginx:v1                                                                                   0.0s

查看镜像

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

运行镜像

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

[root@docker-2 ~]# mkdir myRedis
[root@docker-2 ~]# cd myRdis
[root@docker-2 ~]# vim Dockerfile

FROM daocloud.io/library/centos:7
RUN 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.conf
EXPOSE 6379
ENTRYPOINT /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) FINISHED                                                                                    docker:default
 => [internal] load build definition from Dockerfile                                                                          0.0s
 => => transferring dockerfile: 591B                                                                                          0.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://d  0.0s
 => CACHED [3/3] RUN cd /data/application     && mv redis-6.2.7/ redis     && cd redis/     && make     && mkdir /data/appli  0.0s
 => exporting to image                                                                                                        0.0s
 => => exporting layers                                                                                                       0.0s
 => => writing image sha256:15c74862351b269ae259a12d6c8d43a4e68dd97db5363acdd7e965c885b33a15                                  0.0s
 => => naming to docker.io/library/redis:v1                                                                                   0.0s

运行容器

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

[root@docker-2 myRedis]# docker exec -it redis1 /bin/sh
sh-4.2# ls
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
sh-4.2# cd data/application/redis/
sh-4.2# ./src/redis-cli 
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

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

相关推荐
南猿北者6 小时前
docker容器
docker·容器
苹果醋36 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
二十雨辰6 小时前
[linux]docker基础
linux·运维·docker
time never ceases7 小时前
使用docker方式进行Oracle数据库的物理迁移(helowin/oracle_11g)
数据库·docker·oracle
雨雪飘零7 小时前
Windows系统使用OpenSSL生成自签名证书
nginx·证书·openssl
yanwushu7 小时前
Xserver v1.4.2发布,支持自动重载 nginx 配置
mysql·nginx·php·个人开发·composer
MonkeyKing_sunyuhua9 小时前
ubuntu22.04 docker-compose安装postgresql数据库
数据库·docker·postgresql
追风林9 小时前
mac m1 docker本地部署canal 监听mysql的binglog日志
java·docker·mac
Lovely_red_scarf10 小时前
Jenkins系列
jenkins
€☞扫地僧☜€10 小时前
docker 拉取MySQL8.0镜像以及安装
运维·数据库·docker·容器