12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

dockerコンテナ起動時にシェルを実行する

Last updated at Posted at 2020-04-28

dockerコンテナ起動時にシェルを実行する方法

Dockerfileの最後に以下を追記する

Dokerfile.
省略
〜〜〜〜〜
ADD  start.sh  /
RUN  chmod +x /start.sh
CMD  ["/start.sh"]

start.shに実行させたい内容を記述する

start.sh
#!/bin/bash
echo "test" > test.txt

docker-compose up -dしてbuildしたところ・・
#####イメージ作成してコンテナは起動されたが、エラーが発生!!!すぐにコンテナが終了してしまう。

Creating sls_sls_1 ... done
Attaching to sls_sls_1
sls_sls_1 exited with code 0

###解決方法
起動時のシェルの最後にtail -f /dev/nullを追記することでコンテナを起動し続けて、コマンドを終了させない

start.sh
#!/bin/bash
echo "test" > test.txt

#コンテナを起動し続ける
tail -f /dev/null
12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?