How to execute processing before and after docker-entrypoint.sh

TL; DR

entrypoint.sh


#!/bin/bash

#Preprocessing
echo "Do something before execution docker-entrypoint.sh"

#Add this line
source /usr/local/bin/docker-entrypoint.sh "$@"

#Post-processing
echo "Do something after execution docker-entrypoint.sh"
$ chmod +x entrypoint.sh
COPY entrypoint.sh /usr/local/bin

# ENTRYPOINT ["docker-entrypoint.sh"]
ENTRYPOINT ["entrypoint.sh"]
CMD ["apache2-foreground"]

Introduction

There were a lot of things I wanted to do with the official WordPress Docker Image. However, before launching WordPress, I did not know what to do when I wanted to execute ShellScript, such as inserting data into the DB, so I will leave it.

docker-entrypoint.sh

In the official Docker Image, by default, the startup script is saved as "docker-entrypoint.sh", and by executing the script at startup, the application you want to execute in that Container is started. It is often taken.

In many cases, it is a method that is often used for images such as applications that only need to be started after setting environment variables, but it is a little inconvenient if you want to perform other processing before and after starting.

However, if you use your own startup script, you will not be able to benefit from the latest version of docker-entrypoint.sh due to the image upgrade.

In such a case, consider having the execution of docker-entrypoint.sh in your own startup script. The solution is to use source to expand docker-entrypoint.sh and use " $ @ " to pass all the arguments.

Method

Write a script like the following. It is important that the source line inherits all the arguments it receives.

entrypoint.sh


#!/bin/bash

#Preprocessing
echo "Do something before execution docker-entrypoint.sh"

#Add this line
source /usr/local/bin/docker-entrypoint.sh "$@"

#Post-processing
echo "Do something after execution docker-entrypoint.sh"

The script you create gives you executable privileges.

$ chmod +x entrypoint.sh
COPY entrypoint.sh /usr/local/bin

# ENTRYPOINT ["docker-entrypoint.sh"]
ENTRYPOINT ["entrypoint.sh"]
CMD ["docker-entrypoint.Arguments to sh"]

Summary

I was impressed because I thought only about the source command and the command used to update zshrc. It's convenient when dealing with WordPress Images, so come on!

Recommended Posts

How to execute processing before and after docker-entrypoint.sh
How to execute and mock methods using JUnit
How to use scope and pass processing (Jakarta)
[swift5] How to execute processing when tabBar is tapped
How to create and execute method, Proc, Method class objects
[Rails] How to define macros in Rspec and standardize processing
How to use EventBus3 and ThreadMode
How to call classes and methods
How to use equality and equality (how to use equals)
[Processing × Java] How to use variables
How to connect Heroku and Sequel
How to convert LocalDate and Timestamp
[Processing × Java] How to use arrays
How to execute Ruby irb (interactive ruby)
How to prevent duplicate processing by addEventListener
How to use OrientJS and OrientDB together
[Processing × Java] How to use the loop
[Java] How to output and write files!
How to set up and use kapt
How to build SquashTM and how to support Japanese
[Processing × Java] How to use the class
How to find the tens and ones
[Java] Note how to use RecyclerView and implementation of animated swipe processing.
[Processing × Java] How to use the function
[Easy] How to upgrade Ruby and bundler
How to use substring and substr methods
How to implement asynchronous processing in Outsystems
Note: [Docker] How to start and stop
How to write and explain Dockerfile, docker-compose
How to use @Builder and @NoArgsConstructor together
How to execute multiple commands in docker-compose.yml
[Swift5] How to compare specific times to determine if it is before or after
JDBC promises and examples of how to write
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
How to disable user operations during asynchronous processing
How to output Excel and PDF using Excella
[Java] How to use FileReader class and BufferedReader class
[Java] How to get and output standard input
[Ruby] How to use gsub method and sub method
How to play audio and music using javascript
How to use Segmented Control and points to note
Git and GitHub ~ How to fix common errors ~
[Validation] rails How to specify after today's date
How to set up and operate jEnv (Mac)
How to get and study java SE8 Gold
How to build API with GraphQL and Rails
How to find the total score and average score
[Rails] How to get success and error messages
How to execute tasks in parallel in Swift in Swift Package
How to build parquet-tools and merge Parquet files
Rails scope anti-patterns and how to eliminate them
Ruby How to convert between uppercase and lowercase
How to access Java Private methods and fields
How to redirect after user login with Spring-security
[Java] How to use Calendar class and Date class
[Java] How to get the key and value stored in Map by iterative processing
How to deploy
How to make an image partially transparent in Processing
Execute arbitrary processing after Basic authentication with Spring boot.
How to make batch processing with Rails + Heroku configuration
How to implement login request processing (Rails / for beginners)