DevOps Training Material

«Back

Docker RUN vs CMD vs ENTRYPOINT

RUN apt-get install python3
CMD echo "Hello world"
ENTRYPOINT echo "Hello world"

Docker COPY vs ADD

Chain Your Docker RUN Instructions to Shrink Your Images

RUN wget -O myfile.tar.gz http://example.com/myfile.tar.gz \
    && tar -xvf myfile.tar.gz -C /usr/src/myapp \
    && rm myfile.tar.gz

Difference between an Array and String Based CMD

CMD ["gunicorn", "-c", "python:config.gunicorn", "hello.app:create_app()"]
CMD gunicorn -c "python:config.gunicorn" "hello.app:create_app()"

Shell form sounds better in theory, but it can mess with signal processing. It also means the shell process ends up being PID 1 instead of whatever binary you’re running in your CMD.

Showing what PID 1 is using both methods:
# The output of `ps` when you use exec form:
PID   USER     TIME   COMMAND
  1   root     0:00   {gunicorn} /usr/local/bin/python /usr/local/bin/gunicorn
 15   root     0:02   {gunicorn} /usr/local/bin/python /usr/local/bin/gunicorn
188   root     0:00   ps

# The output of `ps` when you use shell form:
PID   USER     TIME   COMMAND
  1   root     0:00   /bin/sh -c gunicorn -c python:config.gunicorn hello.app:create_app()
  6   root     0:00   {gunicorn} /usr/local/bin/python /usr/local/bin/gunicorn
 16   root     0:00   {gunicorn} /usr/local/bin/python /usr/local/bin/gunicorn
 29   root     0:00   ps

Docker (and I) both recommend that you use exec form whenever possible, which really is most of the time. If you need to do complicated shell scripting when a container starts, you should probably use an ENTRYPOINT script.

Output All of Your Container’s ENV Variables

docker container run --rm alpine:3.7 env

# The above command produces this output:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=157baf151103
HOME=/root

Show Total Disk Space Used by Docker

image

image