我创建了一个用于从dockerfile构建映像的docker-组合文件,然后运行容器--这是我的代码:
Dockerfile
FROM anapsix/alpine-java
VOLUME [ "/var/run/jars/" ]
ADD hello-world.jar /var/run/jars/
EXPOSE 8080
ENTRYPOINT [ "java" ]
CMD ["-?"]docker-compose.yml
version: '3'
services:
hello-world-image:
build: .
image: hello-world-image
hello-world:
image: hello-world-image
container_name: hello-world
ports:
- "8080:8080"
volumes:
- ./logs_ACM:/root/logs_ACM
command: -jar /var/run/jars/hello-world.jar
restart: always码头ps输出:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
103b0a3c30e3 hello-world-image "java -jar /var/run/…" 5 seconds ago Restarting (1) Less than a second ago hello-world当我用"docker“检查正在运行的容器时,端口列是空的,因此即使我在我的docker文件中指定了端口,也没有进行端口映射。为了解决这个问题,需要对我的坞-撰写文件进行哪些更改?
新版本的dockerfile和docker-组合:
FROM anapsix/alpine-java
USER root
RUN mkdir -p /var/run/jars/
COPY spring-petclinic-2.4.2.jar /var/run/jars/
EXPOSE 8081
ENTRYPOINT [ "java" ]
CMD ["-?"]
version: '3' # '3' means '3.0'
services:
spring-petclinic:
build: .
# Only if you're planning to `docker-compose push`
# image: registry.example.com/name/hello-world-image:${TAG:-latest}
ports:
- "8081:8081"
volumes:
# A bind-mount directory to read out log files is a good use of
# `volumes:`. This does not require special setup in the Dockerfile.
- ./logs_ACM:/root/logs_ACM
command: -jar /var/run/jars/spring-petclinic-2.4.2.jar
mysql:
image: mysql:5.7
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=
- MYSQL_ALLOW_EMPTY_PASSWORD=true
- MYSQL_USER=petclinic
- MYSQL_PASSWORD=petclinic
- MYSQL_DATABASE=petclinic
volumes:
- "./conf.d:/etc/mysql/conf.d:ro"发布于 2021-04-20 12:56:39
我认为这里最大的问题是Dockerfile中的VOLUME指令。Dockerfile documentation for VOLUME指出:
在Dockerfile中更改卷:(如果有任何构建步骤)在声明卷后更改卷内的数据,则这些更改将被丢弃。
因此,当您为包含jar文件的目录声明一个VOLUME,然后尝试对其进行ADD内容时,它就会丢失。
在大多数实际情况下,您不需要VOLUME。您应该能够将Dockerfile重写为:
FROM anapsix/alpine-java
# Do not create a VOLUME.
# Generally prefer COPY to ADD. Will create the target directory if needed.
COPY hello-world.jar /var/run/jars/
EXPOSE 8080
# Don't set an ENTRYPOINT just naming an interpreter.
# Do make the default container command be to run the application.
CMD ["java", "-jar", "/var/run/jars/hello-world.jar"]在docker-compose.yml文件中,您不需要单独的“服务”来构建映像,而且通常不需要覆盖container_name: (由Compose提供)或command: (来自Dockerfile)。这可以减少为:
version: '3.8' # '3' means '3.0'
services:
hello-world:
build: .
# Only if you're planning to `docker-compose push`
# image: registry.example.com/name/hello-world-image:${TAG:-latest}
ports:
- "8080:8080"
volumes:
# A bind-mount directory to read out log files is a good use of
# `volumes:`. This does not require special setup in the Dockerfile.
- ./logs_ACM:/root/logs_ACM
# Don't enable auto-restart until you've debugged the start sequence
# restart: alwayshttps://stackoverflow.com/questions/67177455
复制相似问题