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

如果 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. If there is any infringement, please contact admin@php.cn delete
Choosing Between Golang and Python: The Right Fit for Your ProjectChoosing Between Golang and Python: The Right Fit for Your ProjectApr 19, 2025 am 12:21 AM

Golangisidealforperformance-criticalapplicationsandconcurrentprogramming,whilePythonexcelsindatascience,rapidprototyping,andversatility.1)Forhigh-performanceneeds,chooseGolangduetoitsefficiencyandconcurrencyfeatures.2)Fordata-drivenprojects,Pythonisp

Golang: Concurrency and Performance in ActionGolang: Concurrency and Performance in ActionApr 19, 2025 am 12:20 AM

Golang achieves efficient concurrency through goroutine and channel: 1.goroutine is a lightweight thread, started with the go keyword; 2.channel is used for secure communication between goroutines to avoid race conditions; 3. The usage example shows basic and advanced usage; 4. Common errors include deadlocks and data competition, which can be detected by gorun-race; 5. Performance optimization suggests reducing the use of channel, reasonably setting the number of goroutines, and using sync.Pool to manage memory.

Golang vs. Python: Which Language Should You Learn?Golang vs. Python: Which Language Should You Learn?Apr 19, 2025 am 12:20 AM

Golang is more suitable for system programming and high concurrency applications, while Python is more suitable for data science and rapid development. 1) Golang is developed by Google, statically typing, emphasizing simplicity and efficiency, and is suitable for high concurrency scenarios. 2) Python is created by Guidovan Rossum, dynamically typed, concise syntax, wide application, suitable for beginners and data processing.

Golang vs. Python: Performance and ScalabilityGolang vs. Python: Performance and ScalabilityApr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Golang vs. Other Languages: A ComparisonGolang vs. Other Languages: A ComparisonApr 19, 2025 am 12:11 AM

Go language has unique advantages in concurrent programming, performance, learning curve, etc.: 1. Concurrent programming is realized through goroutine and channel, which is lightweight and efficient. 2. The compilation speed is fast and the operation performance is close to that of C language. 3. The grammar is concise, the learning curve is smooth, and the ecosystem is rich.

Golang and Python: Understanding the DifferencesGolang and Python: Understanding the DifferencesApr 18, 2025 am 12:21 AM

The main differences between Golang and Python are concurrency models, type systems, performance and execution speed. 1. Golang uses the CSP model, which is suitable for high concurrent tasks; Python relies on multi-threading and GIL, which is suitable for I/O-intensive tasks. 2. Golang is a static type, and Python is a dynamic type. 3. Golang compiled language execution speed is fast, and Python interpreted language development is fast.

Golang vs. C  : Assessing the Speed DifferenceGolang vs. C : Assessing the Speed DifferenceApr 18, 2025 am 12:20 AM

Golang is usually slower than C, but Golang has more advantages in concurrent programming and development efficiency: 1) Golang's garbage collection and concurrency model makes it perform well in high concurrency scenarios; 2) C obtains higher performance through manual memory management and hardware optimization, but has higher development complexity.

Golang: A Key Language for Cloud Computing and DevOpsGolang: A Key Language for Cloud Computing and DevOpsApr 18, 2025 am 12:18 AM

Golang is widely used in cloud computing and DevOps, and its advantages lie in simplicity, efficiency and concurrent programming capabilities. 1) In cloud computing, Golang efficiently handles concurrent requests through goroutine and channel mechanisms. 2) In DevOps, Golang's fast compilation and cross-platform features make it the first choice for automation tools.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function