loading...
Cover image for Dockerfile: main commands and instructions

Dockerfile: main commands and instructions

soutoigor profile image Igor Souto ・2 min read

The Dockerfile what we use to create an image.

It works like a recipe, we pass an image as base, commands and all the step by step.

When building this Dockerfile, we create an image, and from this image, we create a container.

Useful Dockerfile commands

FROM

FROM debian

Must be the first command in a Dockerfile, with this command we set the image that will be used as base in our image.

ADD

ADD opa.txt /directory/

To move a file into a container directory, it can be .tar files.

CMD

CMD ["sh, "-c", "echo", "$HOME"]

It defines a command to be executed when a container with this image initializes.

There can be only one CMD instruction in a Dockerfile. If you add more than one, only the last will take effect.

It can be overwritten when using if we pass another command in command line, like:

docker run -d my-image /bin/bash

LABEL

LABEL Description="Bla bla bla giropops"

Use it to set a container's description and keep it easy to manage all containers.

COPY

COPY opa.txt /directory/

Similar to ADD, but you can copy normal files and directories.

ENTRYPOINT

ENTRYPOINT ["npm", "run", "dev"]

It's like CMD, but this can not be overwritten, it will always execute, the container will run as an executable.

When this command dies, the container dies too.

ENV

ENV API_KEY="Igor Souto"

Sets environment variables to the container.

EXPOSE

EXPOSE 80

Sets ports that container will expose, so that container will be accessible by this ports.

RUN

RUN apt-get update && apt-get install apache2 && apt-get clean

Used to run commands in the container, generally used to install packages.
Each RUN creates a new layer in our container, so we need to avoid creating too many RUN, to create fewer layers and don't let all too messy, after all, we only have read-write access in the last layer, so depending on what we want to do, we can't (e.g. apt-get clean in another RUN).
You can read the Docker post here to undestand more about image's layers.

USER

USER igor

To define a user inside the container, the default is the ROOT user.

WORKDIR

WORKDIR /mydir

Defines the directory of work. As the container gets executed, this is the directory that we will get inside when we access the container.

VOLUME

VOLUME /mydir

Creates a volume, a directory that will have a copy in our machine/host-docker.

Changing something in that volume in our machine will reflect the container, and vice-versa.

MAINTAINER

MAINTAINER Igor myemail@provider.com

Set the container owner.

Build container

docker build -t first_image:1.0 .

Now to build the image, we use the Docker build command. we pass the -t parameter to name this image, a colon, and the version. And we use the ".", To say that our Dockerfile is at the same directory level. We don't point to the Dockerfile, but the directory it is in.

Discussion

pic
Editor guide