自定义Dockerfile 构建django和java
Posted 我的紫霞辣辣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义Dockerfile 构建django和java相关的知识,希望对你有一定的参考价值。
Dockerfile构建django
# 创建目录
[root@docker01 ~]# mkdir -p /docker/django
[root@docker01 ~]# cd /docker/django/
# 创建django的Dockerfile
[root@docker01 django]# vim Dockerfile
FROM python:3.6
RUN cd /opt/ &&\\
pip install django &&\\
django-admin startproject docker &&\\
cd docker/ &&\\
django-admin startapp aplication
WORKDIR /opt/docker
EXPOSE 8080
CMD python manage.py runserver 0.0.0.0:8080
# 创建镜像
[root@docker01 django]# docker build -t "blog-django:v1" .
...
Step 5/5 : CMD python manage.py runserver 0.0.0.0:8080
---> Running in d203bb79742d
Removing intermediate container d203bb79742d
---> cd0d8bc28d92
Successfully built cd0d8bc28d92
Successfully tagged blog-django:v1
# 启动容器
[root@docker01 django]# docker run -d -p 8080:8080 blog-django:v1
# 查看正在django是否正在运行
[root@docker01 django]# docker ps | head -2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
adf9309dca73 blog-django:v1 "/bin/sh -c 'python …" 17 seconds ago Up 17 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp hardcore_williams
# 修改django的配置文件
[root@docker01 django]# docker cp adf9309dca73:/opt/docker/docker/settings.py .
[root@docker01 django]# vim settings.py
...
ALLOWED_HOSTS = ["*"]
...
[root@docker01 django]# docker cp settings.py adf9309dca73:/opt/docker/docker/settings.py
# 重启django容器
[root@docker01 django]# docker restart adf9309dca73
# 访问测试
浏览器输入:http://192.168.15.80:8080/
访问页面如下:
Dockerfile构建java
java安装包链接:java安装包
提取码:1234
# 上传并解压java安装包
[root@docker01 ~]# mkdir -p /docker/java
[root@docker01 ~]# cd /docker/java/
[root@docker01 java]# rz -E springboot-master.zip
rz waiting to receive.
[root@docker01 java]# unzip springboot-master.zip
[root@docker01 java]# ll
total 60
drwxr-xr-x 4 root root 108 Jul 24 21:14 springboot-master
-rw-r--r-- 1 root root 58361 Aug 1 22:38 springboot-master.zip
# 下载编译工具镜像
[root@docker01 java]# cd /docker/java/springboot-master/
[root@docker01 springboot-master]# docker run -it -v /docker/java:/opt/ maven:3.6-openjdk-8 bash
# 进入容器里面进行编译安装
root@e2aa8a62d1b0:/# cd /opt/springboot-master
root@e2aa8a62d1b0:/opt/springboot-master# mvn clean package # 进行编译,预计5分钟
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17:34 min
[INFO] Finished at: 2021-08-01T15:05:25Z
[INFO] ------------------------------------------------------------------------
root@e2aa8a62d1b0:/opt/springboot-master# exit
exit
# 构建镜像
[root@docker01 springboot-master]# docker build -t blog-java-v1 .
# 查看生成的镜像
[root@docker01 springboot-master]# docker images | head -2
REPOSITORY TAG IMAGE ID CREATED SIZE
blog-java-v1 latest 46e97198cc29 About a minute ago 488MB
# 生成容器
[root@docker01 springboot-master]# docker run -d -p 8888:8080 46e97198cc29
# 查看正在运行的容器
[root@docker01 springboot-master]# docker ps | head -2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e54bc6047a26 46e97198cc29 "/bin/sh -c 'java -j…" 12 minutes ago Up 12 minutes 0.0.0.0:8888->8080/tcp, :::8888->8080/tcp confident_haibt
# 访问测试
浏览器输入: http://192.168.15.80:8888/
访问页面如下:
以上是关于自定义Dockerfile 构建django和java的主要内容,如果未能解决你的问题,请参考以下文章