DEV Community

Cover image for Dockerfile: main commands and instructions
Igor Souto
Igor Souto

Posted on

Dockerfile: main commands and instructions

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
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

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

CMD

CMD ["sh, "-c", "echo", "$HOME"]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

LABEL

LABEL Description="Bla bla bla giropops"
Enter fullscreen mode Exit fullscreen mode

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

COPY

COPY opa.txt /directory/
Enter fullscreen mode Exit fullscreen mode

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

ENTRYPOINT

ENTRYPOINT ["npm", "run", "dev"]
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Sets environment variables to the container.

EXPOSE

EXPOSE 80
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

WORKDIR

WORKDIR /mydir
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Set the container owner.

Build container

docker build -t first_image:1.0 .
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)