Difficulty: intermediate
Estimated Time: 15 minutes

Docker Images are one of the main components of Docker.

Images are composed of layers with files and binaries and information about the main process to run, exposed ports, etc.

The main public docker image repository is dockerhub hub.docker.com, where you can explore available images (official and community).

This scenario provides basic notions to build first-class docker images, with a small footprint and following the best-practices.

¡Ahora puedes crear tus propias imagenes docker!

Don’t stop now! The next scenario will only take about 10 minutes to complete.

Dockerfile

Step 1 of 3

Dockerizing a command

We are going to dockerize curl command.

For building the docker image we should run docker build -t curl .

For running the curl container we should run docker run --rm curl

Give this commands now a try... It does not work as expected! :/

Don´t worry, we will go back to them after working on our Dockerfile!


FROM (Dockerfile Best-Practice)

Whenever possible, use current official repositories as the basis for your images. We recommend the Alpine image as it is tightly controlled and small in size (currently under 5 MB), while still being a full Linux distribution.

LABEL (Dockerfile Best-Practice)

You can add labels to your image to help organize images by project, record licensing information, to aid in automation, or for other reasons. For each label, add a line beginning with LABEL and with one or more key-value pairs. The following examples show the different acceptable formats.

FROM alpine:3.8
LABEL maintainer="[email protected]"
LABEL organization="gradiant.org"

Rebuild our docker image! docker build -t curl ..

Now It should work. The new curl docker image should be now part of our docker local images. Let's check it:

docker image inspect curl

Alpine linux's package manager is apk, equivalent to debian/ubuntu apt or redhat/centos yum.

RUN apk add --no-cache curl

Dockerfile Best-Practice

The best use for ENTRYPOINT is to set the image’s main command, allowing that image to be run as though it was that command (and then use CMD as the default flags).

We set the entrypoint to curl and we set --help as the default parameter.

ENTRYPOINT ["curl"]
CMD ["--help"]

Now It´s time to rebuild our docker image! docker build -t curl .

Remember, we can run a curl container with: docker run --rm curl

Let's use our brand-new curl container to gather useful information from the web:

docker run --rm curl -s wttr.in/~"vigo"

Nice!

Dockerfile