<p>I have developed a simple rails app that uses Redis, sidekiq, and mysql2. I'm trying to run the app using docker-compose. I wrote a docker-compose.yml which is working fine. I've made a few changes to the docker file which isn't working and when I see the logs the webapp container is exiting with exit code 1.</p>
<p>These are my files</p>
<p>Dockerfile</p>
<pre><code>FROM ubuntu:16.04

ENV RUBY_MAJOR=&quot;2.6&quot; \
    RUBY_VERSION=&quot;2.6.3&quot; \
    RUBYGEMS_VERSION=&quot;3.0.8&quot; \
    BUNDLER_VERSION=&quot;1.17.3&quot; \
    RAILS_VERSION=&quot;5.2.1&quot; \
    RAILS_ENV=&quot;production&quot; \
    GEM_HOME=&quot;/usr/local/bundle&quot;
ENV BUNDLE_PATH=&quot;$GEM_HOME&quot; \
    BUNDLE_BIN=&quot;$GEM_HOME/bin&quot; \
    BUNDLE_SILENCE_ROOT_WARNING=1 \
    BUNDLE_APP_CONFIG=&quot;$GEM_HOME&quot;

ENV PATH=&quot;$BUNDLE_BIN:$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH&quot;

USER root
RUN apt-get update &amp;&amp; \
      apt-get -y install sudo
RUN echo &quot;%sudo ALL=(ALL) NOPASSWD: ALL&quot; &gt;&gt; /etc/sudoers &amp;&amp; \
    addgroup --gid 1024 stars &amp;&amp; \
    useradd -G stars,sudo -d /home/user --shell /bin/bash -m user
RUN mkdir -p /usr/local/etc \
    &amp;&amp; echo 'install: --no-document' &gt;&gt; /usr/local/etc/gemrc \
    &amp;&amp; echo 'update: --no-document' &gt;&gt; /usr/local/etc/gemrc

USER user
RUN sudo apt-get -y install --no-install-recommends vim make gcc zlib1g-dev autoconf build-essential libssl-dev libsqlite3-dev \
    curl htop unzip mc openssh-server openssl bison libgdbm-dev ruby git libmysqlclient-dev tzdata mysql-client
    
RUN sudo rm -rf /var/lib/apt/lists/* \
    &amp;&amp; sudo curl -fSL -o ruby.tar.gz &quot;http://cache.ruby-lang.org/pub/ruby/$RUBY_MAJOR/ruby-$RUBY_VERSION.tar.gz&quot; \
    &amp;&amp; sudo mkdir -p /usr/src/ruby \
    &amp;&amp; sudo tar -xzf ruby.tar.gz -C /usr/src/ruby --strip-components=1 \
    &amp;&amp; sudo rm ruby.tar.gz

USER root
RUN cd /usr/src/ruby \
    &amp;&amp; { sudo echo '#define ENABLE_PATH_CHECK 0'; echo; cat file.c; } &gt; file.c.new &amp;&amp; mv file.c.new file.c \
    &amp;&amp; autoconf \
    &amp;&amp; ./configure --disable-install-doc

USER user
RUN cd /usr/src/ruby \
    &amp;&amp; sudo make -j&quot;$(nproc)&quot; \
    &amp;&amp; sudo make install \
    &amp;&amp; sudo gem update --system $RUBYGEMS_VERSION \
    &amp;&amp; sudo rm -r /usr/src/ruby
RUN sudo gem install bundler --version &quot;$BUNDLER_VERSION&quot;

RUN sudo mkdir -p &quot;$GEM_HOME&quot; &quot;$BUNDLE_BIN&quot; \
    &amp;&amp; sudo chmod 777 &quot;$GEM_HOME&quot; &quot;$BUNDLE_BIN&quot; \
    &amp;&amp; sudo gem install rails --version &quot;$RAILS_VERSION&quot;
RUN mkdir -p ~/.ssh &amp;&amp; \
    chmod 0700 ~/.ssh &amp;&amp; \
    ssh-keyscan github.com &gt; ~/.ssh/known_hosts
ARG ssh_pub_key
ARG ssh_prv_key
RUN echo &quot;$ssh_pub_key&quot; &gt; ~/.ssh/id_rsa.pub &amp;&amp; \
    echo &quot;$ssh_prv_key&quot; &gt; ~/.ssh/id_rsa &amp;&amp; \
    chmod 600 ~/.ssh/id_rsa.pub &amp;&amp; \
    chmod 600 ~/.ssh/id_rsa
USER root
RUN curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
RUN apt-get install -y nodejs
USER user
WORKDIR /data
RUN sudo mkdir /data/checklist
WORKDIR /data/checklist
ADD Gemfile Gemfile.lock ./
RUN sudo chown -R user /data/checklist
RUN bundle install
ADD . .
RUN sudo chown -R user /data/checklist
EXPOSE 3001
ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true
RUN chmod +x ./config/docker/compile.sh &amp;&amp; ./config/docker/compile.sh
CMD [&quot;bundle&quot;, &quot;exec&quot;, &quot;rails&quot;, &quot;s&quot;, &quot;-p&quot;, &quot;3001&quot;]
</code></pre>
<p>compile.sh</p>
<pre><code>bundle exec rake assets:precompile
bundle exec rake db:migrate 2&gt;/dev/null || bundle exec rake db:create db:migrate
echo &quot;Assets Pre-compiled!&quot;
</code></pre>
<p>This is my docker-compose.yml</p>
<pre><code>version: '3'
services: 
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: &quot;list&quot;
      MYSQL_ROOT_PASSWORD: &quot;Mission2019&quot;
      MYSQL_USERNAME: &quot;root&quot;
  webapp:
    build: .
    ports:
      - '3001:3001'
    volumes:
      - '.:/data/checklist'
    depends_on:
      - db
      - redis
    command: rails db:migrate
    environment:
      DB_USERNAME: &quot;root&quot;
      DB_PASSWORD: &quot;Mission2019&quot;
      DB_DATABASE: &quot;list&quot;
      DB_PORT: 3306
      DB_HOST: db
      RAILS_ENV: production 
      RAILS_MAX_THREADS: 5
  redis:
    image: redis:4.0-alpine
    command: redis-server
    ports:
      - '6379:6379'
  sidekiq:
    build: .
    command: bundle exec sidekiq -C config/sidekiq.yml
    depends_on:
      - &quot;db&quot;
      - &quot;redis&quot;
</code></pre>
<p>Working Dockerfile</p>
<pre><code>FROM ubuntu:16.04

ENV RUBY_MAJOR=&quot;2.6&quot; \
    RUBY_VERSION=&quot;2.6.3&quot; \
    RUBYGEMS_VERSION=&quot;3.0.8&quot; \
    BUNDLER_VERSION=&quot;1.17.3&quot; \
    RAILS_VERSION=&quot;5.2.1&quot; \
    RAILS_ENV=&quot;production&quot; \
    GEM_HOME=&quot;/usr/local/bundle&quot;
ENV BUNDLE_PATH=&quot;$GEM_HOME&quot; \
    BUNDLE_BIN=&quot;$GEM_HOME/bin&quot; \
    BUNDLE_SILENCE_ROOT_WARNING=1 \
    BUNDLE_APP_CONFIG=&quot;$GEM_HOME&quot;

ENV PATH=&quot;$BUNDLE_BIN:$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH&quot;

USER root
RUN apt-get update &amp;&amp; \
      apt-get -y install sudo
RUN echo &quot;%sudo ALL=(ALL) NOPASSWD: ALL&quot; &gt;&gt; /etc/sudoers &amp;&amp; \
    addgroup --gid 1024 stars &amp;&amp; \
    useradd -G stars,sudo -d /home/user --shell /bin/bash -m user
RUN mkdir -p /usr/local/etc \
    &amp;&amp; echo 'install: --no-document' &gt;&gt; /usr/local/etc/gemrc \
    &amp;&amp; echo 'update: --no-document' &gt;&gt; /usr/local/etc/gemrc

USER user
RUN sudo apt-get -y install --no-install-recommends vim make gcc zlib1g-dev autoconf build-essential libssl-dev libsqlite3-dev \
    curl htop unzip mc openssh-server openssl bison libgdbm-dev ruby git libmysqlclient-dev tzdata mysql-client
    
RUN sudo rm -rf /var/lib/apt/lists/* \
    &amp;&amp; sudo curl -fSL -o ruby.tar.gz &quot;http://cache.ruby-lang.org/pub/ruby/$RUBY_MAJOR/ruby-$RUBY_VERSION.tar.gz&quot; \
    &amp;&amp; sudo mkdir -p /usr/src/ruby \
    &amp;&amp; sudo tar -xzf ruby.tar.gz -C /usr/src/ruby --strip-components=1 \
    &amp;&amp; sudo rm ruby.tar.gz

USER root
RUN cd /usr/src/ruby \
    &amp;&amp; { sudo echo '#define ENABLE_PATH_CHECK 0'; echo; cat file.c; } &gt; file.c.new &amp;&amp; mv file.c.new file.c \
    &amp;&amp; autoconf \
    &amp;&amp; ./configure --disable-install-doc

USER user
RUN cd /usr/src/ruby \
    &amp;&amp; sudo make -j&quot;$(nproc)&quot; \
    &amp;&amp; sudo make install \
    &amp;&amp; sudo gem update --system $RUBYGEMS_VERSION \
    &amp;&amp; sudo rm -r /usr/src/ruby
RUN sudo gem install bundler --version &quot;$BUNDLER_VERSION&quot;

RUN sudo mkdir -p &quot;$GEM_HOME&quot; &quot;$BUNDLE_BIN&quot; \
    &amp;&amp; sudo chmod 777 &quot;$GEM_HOME&quot; &quot;$BUNDLE_BIN&quot; \
    &amp;&amp; sudo gem install rails --version &quot;$RAILS_VERSION&quot;
RUN mkdir -p ~/.ssh &amp;&amp; \
    chmod 0700 ~/.ssh &amp;&amp; \
    ssh-keyscan github.com &gt; ~/.ssh/known_hosts
ARG ssh_pub_key
ARG ssh_prv_key
RUN echo &quot;$ssh_pub_key&quot; &gt; ~/.ssh/id_rsa.pub &amp;&amp; \
    echo &quot;$ssh_prv_key&quot; &gt; ~/.ssh/id_rsa &amp;&amp; \
    chmod 600 ~/.ssh/id_rsa.pub &amp;&amp; \
    chmod 600 ~/.ssh/id_rsa
USER root
RUN curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
RUN apt-get install -y nodejs
USER user
WORKDIR /data
RUN sudo mkdir /data/checklist
WORKDIR /data/checklist
ADD Gemfile Gemfile.lock ./
RUN sudo chown -R user /data/checklist
RUN bundle install
ADD . .
RUN sudo chown -R user /data/checklist
EXPOSE 3001
ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true
ENTRYPOINT [&quot;sh&quot;, &quot;./config/docker/startup.sh&quot;]
</code></pre>
<p>startup.sh for the working Dockerfile</p>
<pre><code>kill -9 `cat /data/checklist/tmp/pids/server.pid`
bundle exec rake assets:precompile
bundle exec rake db:migrate 2&gt;/dev/null || bundle exec rake db:create db:migrate
rails s -p 3001 -b 0.0.0.0 -e PRODUCTION
echo &quot;Assets Pre-compiled!&quot;
</code></pre>
<p>The reason for changing the Dockerfile from the one which works is, since I'm using sidekiq and I want sidekiq to run in another separate container all together. If I give command option in the docker-compose.yml it's not picking up and <code>bundle exec sidekiq -C config/sidekiq.yml</code> is not running in the sidekiq container.</p>
<p>I've kubernetes yaml files for the same app. Facing same problem there as well. I'm not able to overwrite the entrypoint instruction from the YAML for sidekiq pod.</p>
<p>Please let me know if any other info is needed.</p>

Abhishek
<p>You should use <code>CMD</code> in the second Dockerfile too, and then it can be overridden by the Docker Compose <code>command:</code>.</p>
<pre class="lang-sh prettyprint-override"><code># CMD, not ENTRYPOINT
CMD [&quot;sh&quot;, &quot;./config/docker/startup.sh&quot;]
</code></pre>
<p>One common use for <code>ENTRYPOINT</code> is to be a wrapper program that does some environment or other first-time setup, then executes the <code>CMD</code> that's passed in as the remainder of the command-line arguments.  Then you can separately replace the command part, while keeping the setup part.  In a Ruby environment <code>bundle exec ...</code> has the right semantics, so you can also consider:</p>
<pre class="lang-sh prettyprint-override"><code># Note: MUST be JSON-array syntax
ENTRYPOINT [&quot;bundle&quot;, &quot;exec&quot;]
# Can be either string or JSON-array form
CMD [&quot;./config/docker/startup.sh&quot;]
</code></pre>
<pre class="lang-yaml prettyprint-override"><code>version: '3.8'
services:
  webapp:
    build: .
    # Use default CMD/ENTRYPOINT from image
  sidekiq:
    build: .
    # Overrides CMD, leaves ENTRYPOINT in place
    command: sidekiq -C config/sidekiq.yml
</code></pre>
<p>There is a separate Compose <code>entrypoint:</code> override, but you should rarely need it.</p>
<p>(Your Dockerfile can be much much simpler.  In general you do not need to configure <code>sudo</code> or user passwords, and Dockerfiles run as root by default unless you explicitly use <code>USER</code> to switch the current user IDs.  Adding ssh credentials into a Dockerfile where they can be trivially <code>docker cp</code>'d out is also not a best practice.  Also consider using the Docker Hub <code>ruby</code> image over building your own from source.)</p>
