linux12 - dockerfile06 --> Dockerfile构建djiango和java

Posted FikL-09-19

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux12 - dockerfile06 --> Dockerfile构建djiango和java相关的知识,希望对你有一定的参考价值。

Dockerfile

一、Dockerfile 编写案例

1、Dockerfile 指令是按照从上到下执行的

2、如果构建的上一个镜像执行过此步骤,下一个则可以使用缓存(Using cache)

3、从互联网下载内容不会使用缓存

4、添加文件的内容(ADD、COPY)

5、如果上一步修改过,则下面的dockerfile步骤全部不使用缓存。

二、Dockerfile搭建django

# 1、创建django目录
# 2、编写Dockerfile django文件
# 3、访问测试

1、创建django目录

[root@docter django]# mkdir -p /root/django/
[root@docter django]# cd /root/django/

2、编写dockerfile文件

# 1、编写Dockerfile文件
[root@docter 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
# 2、构建镜像
[root@docter django]# docker build -t blog-django:v1 . # 构建镜像
Sending build context to Docker daemon  2.048kB
...
Successfully built 75ea06c8060c
Successfully tagged blog-django:v1
# 3、启动容器
[root@docter django]# docker run -d -p 8080:8080 blog-django:v1 
fc9b4d2b4109b5d1c012d98fc9540ca781396992ad0a3691dc86fe8c0848ee1a
# 4、查看容器是否启动
[root@docter django]# docker ps
CONTAINER ID   IMAGE            COMMAND                  CREATED         STATUS        PORTS                                       NAMES
fc9b4d2b4109   blog-django:v1   "/bin/sh -c 'python …"   2 seconds ago   Up 1 second   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   sweet_allen
# 5、修改django配置文件
1、从容器内复制配置文件
[root@docter django]# docker cp fc9b4d2b4109:/opt/docker/docker/settings.py .
[root@docter django]# ll
total 8
-rw-r--r-- 1 root root  240 Jul 30 16:52 Dockerfile
-rw-r--r-- 1 root root 3242 Jul 30 16:04 settings.py
2、修改配置文件
[root@docter django]# egrep 'ALLOWED_HOSTS' settings.py 
ALLOWED_HOSTS = ['*']  # 修改这个配置文件
3、把配置文件复制到容器内
[root@docker1 docker]# docker cp settings.py fc9b4d2b4109:/opt/docker/docker
# 6、重新启动容器
[root@docter django]# docker restart fc9b4d2b4109
fc9b4d2b4109
[root@docter django]# docker ps
CONTAINER ID   IMAGE            COMMAND                  CREATED             STATUS          PORTS                                       NAMES
fc9b4d2b4109   blog-django:v1   "/bin/sh -c 'python …"   About an hour ago   Up 56 minutes   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   sweet_allen
# 7、IP访问
192.168.15.30:8080

三、Dockerfile搭建java

# 1、创建java目录
# 2、上传java安装包
# 3、编译java
# 4、访问测试

1、创建java目录

[root@docter ~]# mkdir java
[root@docter ~]# cd java/

2、上传java安装包

# 1、上传安装包
[root@docter java]# wget http://www.mmin.xyz:81/package/java/springboot-master.zip
# 2、解压并查看
[root@docter java]#  unzip springboot-master.zip
[root@docter java]# ll
total 60
drwxr-xr-x 4 root root   108 Jul 24 21:14 springboot-master
-rw-r--r-- 1 root root 58361 Jul 30 15:49 springboot-master.zip

3、编译java

# 1、下载镜像
[root@docter springboot-master]# pwd
/root/java/springboot-master
[root@docter springboot-master]# docker run -it -v /root/java/:/opt/ maven:3.6-openjdk-8 bash
3.6-openjdk-8: Pulling from library/maven
004f1eed87df: Pull complete 
5d6f1e8117db: Pull complete 
48c2faf66abe: Pull complete 
234b70d0479d: Pull complete 
d7eb6c022a4e: Pull complete 
347c03cf94f8: Pull complete 
914dd506c750: Pull complete 
d7d7fe135d55: Pull complete 
71a3a5508994: Pull complete 
73dccb1622e9: Pull complete 
Digest: sha256:ac624d39dd0b7bed827d4b0d1bbaeb457012e0cba0fb6c53ce34b00bed16ec08
Status: Downloaded newer image for maven:3.6-openjdk-8

# 2、容器里面编译安装
root@5d6ce165b40f:/opt/springboot-master# ls
Dockerfile  mvnw  mvnw.cmd  pom.xml  src
root@5d6ce165b40f:/opt/springboot-master# mvn clean package  # 预计时间5分钟
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/2.4.4/spring-boot-starter-parent-2.4.4.pom
...
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS  #编译成功
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  03:41 min
[INFO] Finished at: 2021-07-30T11:16:39Z
[INFO] ------------------------------------------------------------------------
# 3、构建镜像
[root@docter springboot-master]# docker build -t blog-java-v1 . #构建镜像
Sending build context to Docker daemon  41.79MB
Step 1/5 : FROM openjdk:18-jdk
18-jdk: Pulling from library/openjdk
18-jdk: Pulling from library/openjdk
1da50e1664e1: Pull complete 
e4c8e5a84542: Pull complete 
170c51e5216c: Pull complete 
Digest: sha256:a115ac999336b4bc851abd2ec2eebf8feb01860972e121acd9a74438a5371957
Status: Downloaded newer image for openjdk:18-jdk
 ---> 0072c1906e21
Step 2/5 : ADD target/*.war /usr/local/webapp/
 ---> 1b889632132b
Step 3/5 : WORKDIR /usr/local/webapp
 ---> Running in cb431d072089
Removing intermediate container cb431d072089
 ---> 6935bc6e4bf9
Step 4/5 : EXPOSE 8080
 ---> Running in 939e6a3fc2bd
Removing intermediate container 939e6a3fc2bd
 ---> 64933fc6f4e6
Step 5/5 : CMD java -jar GitLab-0.0.1-SNAPSHOT.war
 ---> Running in 7ff37eb57a00
Removing intermediate container 7ff37eb57a00
 ---> 9658342afa70
Successfully built 9658342afa70
Successfully tagged blog-java-v1:latest
# 4、启动容器
[root@docter springboot-master]# docker run -d -p 8888:8080 blog-java-v1
9929fdafeff8e9d7c842adf7484e0978fabf53713871f2760971efaca5e25ca5
# 5、查看容器是否启动
[root@docter springboot-master]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                       NAMES
9929fdafeff8   blog-java-v1   "/bin/sh -c 'java -j…"   15 seconds ago   Up 15 seconds   0.0.0.0:8888->8080/tcp, :::8888->8080/tcp   eloquent_wilson
# 5、访问测试
192.168.15.30:8888

4、查看java页面


以上是关于linux12 - dockerfile06 --> Dockerfile构建djiango和java的主要内容,如果未能解决你的问题,请参考以下文章

linux12 - dockerfile05 --> Dockerfile基础命令和个人blog搭建

linux12 - dockerfile01 -->构建镜像指令与编写案例

linux12 - dockerfile03 --> 搭建 PHP+Nginx+Wordpress.

linux12 - dockerfile02 --> 实战构建centostomcat

有没有人有一个 Dockerfile 和命令行的简单示例,可以将外部目录从 linux 挂载到 docker 映像中?

linux12 - dockerfile04 --> wordpress+php5.4