简体   繁体   English

将外壳打开到Java映像中

[英]Opening a shell into a Java image

I'm building a postgres+java container, and I'd like to open a shell into the java "service". 我正在构建一个postgres + java容器,我想在Java“服务”中打开一个外壳。 That service exits immediately after starting, how can I do to open a shell into it? 该服务启动后立即退出,我该如何打开其中的外壳?

I see it in docker ps -a but it has already exited. 我在docker ps -a看到了它,但它已经退出了。

The file I'm using is this .yaml with docker-compose 我正在使用的文件是带有docker-compose的.yaml文件

version: '3.1'

services:

  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_PASSWORD: postgres
    volumes:

      - datavolume:/var/lib/postgresql


  java:

    image: openjdk:8

volumes:

  datavolume:

A Docker container generally runs a single process. Docker容器通常运行单个进程。 In the same way that just running a JVM without an application attached to it isn't really meaningful, running a Docker container with a JVM but no actual application added to it isn't that useful. 就像只运行一个没有附加应用程序的JVM并没有什么意义,以同样的方式运行带有JVM但未添加任何实际应用程序的Docker容器并没有太大用处。

You should write a Dockerfile that adds your application's jar file to a base Java image; 您应该编写一个Dockerfile,将您应用程序的jar文件添加到基本Java映像中。 for instance 例如

FROM openjdk:8
COPY app.jar /
CMD ["java", "-jar", "/app.jar"]

and then your docker-compose.yml file can have instructions to build and run this image 然后您docker-compose.yml文件可以包含构建和运行该映像的说明

services:
  java:
    build: .

If you just want a shell in a copy of the image to poke around and see what's there, you can generally run 如果您只想在图像副本中戳一下外壳,然后查看其中的内容,通常可以运行

docker run --rm -it openjdk:8 sh

The standard openjdk Dockerfile doesn't explicitly declare any specific ENTRYPOINT or CMD so it will exit immediately when run. 标准的openjdk Dockerfile没有显式声明任何特定的ENTRYPOINT或CMD,因此它将在运行时立即退出。 (It probably inherits a default /bin/sh , but with no command to run, that will also exit immediately.) You can declare some other command: in the Dockerfile to cause the "service" to not exit, but it's not really doing anything useful for you. (它可能继承了默认的/bin/sh ,但是由于没有命令运行,该命令也会立即退出。)您可以声明其他command:在Dockerfile中导致“服务”不退出,但实际上并没有对您有用的任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM