Home >Backend Development >Golang >How can I run the CMD and ENTRYPOINT scripts simultaneously if they are in different images?

How can I run the CMD and ENTRYPOINT scripts simultaneously if they are in different images?

王林
王林forward
2024-02-09 08:57:321189browse

如果 CMD 和 ENTRYPOINT 脚本位于不同的映像中,如何同时运行它们?

When arranging containers, we often encounter situations where we need to run multiple scripts at the same time. Especially when the CMD and ENTRYPOINT scripts are in different images, how to run them simultaneously becomes a problem that needs to be solved. In this case, we can achieve the purpose of running both scripts simultaneously by using a multi-stage build. First, we need to write a script in an image that will run both the CMD and ENTRYPOINT scripts. We can then use the Dockerfile's multi-stage build feature to copy this script into the final image and execute it when the container starts. In this way, we can realize the need to run CMD and ENTRYPOINT scripts at the same time, improving the flexibility and scalability of the container.

Question content

I am using docker multi-stage build and trying to add live reload functionality to my dockerized go application. I have an entrypoint.sh in the second picture which has its own configuration.

Now, the problem is that the command cmd ["air", "-c", ".air.toml"] in the first image is blocked by entrypoint ["/entrypoint.sh" ] The script overwrites the second image, so only entrypoint is started and cmd is not run.

I can't combine them into a unique command like this

entrypoint ["/entrypoint.sh", "air", "-c", ".air.toml"]

Because the second image does not have the golang language and corresponding libraries installed.

Is it possible to somehow run cmd and entrypoint in parallel? Thanks.

dockerfile

from golang:1.17.2
copy . /go/src/sample

workdir /go/src/sample
run go install github.com/go-delve/delve/cmd/dlv@latest
run go install github.com/cosmtrek/air@latest
cmd ["air", "-c", ".air.toml"]

from eclipse-temurin:17-focal
copy entrypoint.sh /entrypoint.sh
run chmod +x /entrypoint.sh
entrypoint ["/entrypoint.sh"]

docker-compose.yml

version: '3'
services:
  go:
    build:
      context: ./backend
      dockerfile: Dockerfile
    volumes:
      - ./backend:/go/src/backend
    working_dir: /go/src/backend
    ports:
      - 8080:8080

Solution

Read the parameters passed to the entry point (i.e. cmd).

For example, below is your entry point script. You can access parameters and do something with them, i.e. execute them.

#!/bin/bash

# dome something in your entrypoint

# execute the original command
# substituting the current process id
# so that command is run with pid 1
exec "$@"

In your docker image, make sure you have the required command, which is

entrypoint ["/entryppoint.sh"]
cmd ["echo", "command"]

Beyond this technical aspect, you seem to be implying that you want to run cmd that depends on go being available, without go being available. it's out of the question. You need to make sure that what you are trying to execute and its dependencies are available.

You may be able to copy the air binary from the first stage. Something like this.

COPY --from=0 /go/bin/air /usr/local/bin/air

You may wish to compile air with cgo_enabled=0.

However, I assume that you need the go compiler present in the image for hot reload to work properly, since your application will need to be recompiled when the code changes. So maybe you shouldn't even use multiphase here.

That, say. Hot reloading in containers seems a bit like an anti-pattern. Containers are often a way to distribute artifacts.

The above is the detailed content of How can I run the CMD and ENTRYPOINT scripts simultaneously if they are in different images?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete