June 12, 2018

Entrypoint vs Cmd

^ Ad space to help offset hosting costs :D

We can use ENTRYPOINT instead of CMD within our Dockerfile's in order to setup a script that is always run (no matter what!) when a container is spun up from the image we make.

Entrypoint Script

First, we make a script to act as our ENTRYPOINT:

#!/usr/bin/env bash

# Ensure /.composer exists and is writable
if [ ! -d /.composer ]; then
    mkdir /.composer

chmod -R ugo+rw /.composer

# Run a command or start supervisord
if [ $# -gt 0 ];then
    # If we passed a command, run it
    exec "$@"
    # Otherwise start supervisord

This script allows us to do some pre-processing whenever a container is spun up. It defaults to running supervisord for us, but we also allow ourselves to run any command, just like we could before, by using the exec "$@" line if we pass any commands/arguments to the container.


Then we use that in the Dockerfile by adding it to the image and setting it as our ENTRYPOINT script:

FROM ubuntu:18.04

LABEL maintainer="Chris Fidao"

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update \
    && apt-get install -y gnupg tzdata \
    && echo "UTC" > /etc/timezone \
    && dpkg-reconfigure -f noninteractive tzdata

RUN apt-get update \
    && apt-get install -y curl zip unzip git supervisor sqlite3 \
       nginx php7.2-fpm php7.2-cli \
       php7.2-pgsql php7.2-sqlite3 php7.2-gd \
       php7.2-curl php7.2-memcached \
       php7.2-imap php7.2-mysql php7.2-mbstring \
       php7.2-xml php7.2-zip php7.2-bcmath php7.2-soap \
       php7.2-intl php7.2-readline php7.2-xdebug \
       php-msgpack php-igbinary \
    && php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
    && mkdir /run/php \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
    && echo "daemon off;" >> /etc/nginx/nginx.conf

RUN ln -sf /dev/stdout /var/log/nginx/access.log \
    && ln -sf /dev/stderr /var/log/nginx/error.log

ADD default /etc/nginx/sites-available/default
ADD supervisord.conf /etc/supervisor/conf.d/supervisord.conf
ADD php-fpm.conf /etc/php/7.2/fpm/php-fpm.conf
ADD start-container.sh /usr/bin/start-container
RUN chmod +x /usr/bin/start-container

ENTRYPOINT ["start-container"]

And of course, we need to rebuild the image:

docker build -t shippingdocker/app:latest \
    -f docker/app/Dockerfile \

Looking for a deeper dive into Docker?

Sign up here to get a preview of the Shipping Docker course! Learn how to integrate Docker into your applications and develop a workflow to make using Docker a breeze!

All Topics