Dockerfile syntax: Difference between revisions

From wikinotes
 
No edit summary
 
Line 3: Line 3:
{| class="wikitable"
{| class="wikitable"
|-
|-
| official docs || https://docs.docker.com/engine/reference/builder/  
| official docs || https://docs.docker.com/engine/reference/builder/
|-
|-
|}
|}
Line 47: Line 47:
<source lang="bash">
<source lang="bash">
RUN apt-get update && \
RUN apt-get update && \
apt-get install python3-pip
    apt-get install python3-pip
</source>
</source>
</blockquote><!-- run -->
</blockquote><!-- run -->
Line 117: Line 117:
== EXPOSE ==
== EXPOSE ==
<blockquote>
<blockquote>
Exposes a port inside the container to the host.  
Exposes a port inside the container to the host.
(I am still unclear what interface the port is bound to... 127.0.0.1 ?)
(I am still unclear what interface the port is bound to... 127.0.0.1 ?)


Line 155: Line 155:


Volumes can be shared between containers.
Volumes can be shared between containers.
</blockquote>
</blockquote><!-- VOLUME -->
</blockquote><!-- Syntax -->

Latest revision as of 16:17, 2 July 2022

Documentation

official docs https://docs.docker.com/engine/reference/builder/

Usage

Images are created from dockerfiles using the build command.

docker build \
  -f /path/to/Dockerfile \
  -t <container_name> \
  /path/for/container

docker build .  # if dockerfile in cwd

# or you can use a directory
# image-name/
#    Dockerfile

docker build  image-name

Syntax

FROM

Every container starts with a from line. This indicates the base image that your container is built on top of.

FROM ubuntu:16.04

RUN

Commands that run when your container is being built for the first time.

RUN apt-get update && \
    apt-get install python3-pip

CMD

commands run every time your container is started.

CMD  echo "welcome!" >> /home/user/welcome.txt

ADD

copy a file into your container.
Supports glob-style matching.

ADD                    /path/file  /path/file
ADD                    Gemfile*    /path/
ADD                    .           /path/
ADD --chmod=user:user  /path/file  /path/file

ENTRYPOINT

If you are creating a container to run a specific application, set that application to the ENTRYPOINT. This means that every time that your container is started, that application will run.

ENTRYPOINT ['/usr/bin/firefox', '-private-window']   ## exec-style (best practice)
ENTRYPOINT /usr/bin/firefox -private-window          ## shell-style (discouraged, unix signals will be unavailable, your program will not be PID #1)

If your entrypoint is a script, you can ensure that it is able to properly handle unix signals by using gosu and exec:

#!/bin/bash
set -e

if [ "$1" = 'postgres' ]; then
    chown -R postgres "$PGDATA"

    if [ -z "$(ls -A "$PGDATA")" ]; then
        gosu postgres initdb
    fi

    exec gosu postgres "$@"
fi

exec "$@"

ENV

Set an environment variable in the container.
Once defined, they can be referred to as $VAR

ENV PYTHONPATH /home/dev/python
ENV blah=$PYTHONPATH

EXPOSE

Exposes a port inside the container to the host. (I am still unclear what interface the port is bound to... 127.0.0.1 ?)

Section Marked as Requiring Revision:

The official documentation says that expose does not actually forward container's ports, it only indicates that the container is listening on these ports. I need to test this to see what is actually happening when this command is used..

EXPOSE 8080        ## expose container's 8080 on host's 8080
EXPOSE 8080 9000   ## expose container's 8080 on host's 9000

ARG

Arg allows you to submit arguments at build time.
It can be accessed later similarly to an environment variable.

ARG VERSION
ARG VERSION=DEFAULT_VALUE
RUN echo $VERSION
docker build --build-arg VERSION=1.1.1

VOLUME

Defines mountpoint for a volume inside the container.
This is optionally bound within docker run -v /src:/dst.
If not defined, then a location will be created automatically.

Volumes can be shared between containers.