Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 modeExit 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 modeExit fullscreen mode

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

CMD

CMD ["sh, "-c", "echo", "$HOME"]
Enter fullscreen modeExit 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 modeExit fullscreen mode

LABEL

LABEL Description="Bla bla bla giropops"
Enter fullscreen modeExit 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 modeExit fullscreen mode

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

ENTRYPOINT

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

It's likeCMD, 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 modeExit fullscreen mode

Sets environment variables to the container.

EXPOSE

EXPOSE 80
Enter fullscreen modeExit fullscreen mode

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

RUN

RUNapt-get update&& apt-getinstallapache2&& apt-get clean
Enter fullscreen modeExit 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 posthere to undestand more about image's layers.

USER

USER igor
Enter fullscreen modeExit fullscreen mode

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

WORKDIR

WORKDIR /mydir
Enter fullscreen modeExit 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 modeExit 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 modeExit fullscreen mode

Set the container owner.

Build container

docker build-t first_image:1.0.
Enter fullscreen modeExit 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A Software Engineer with a focus on frontend development.
  • Location
    São Paulo, Brazil
  • Work
    Frontend Engineer at chess.com
  • Joined

More fromIgor Souto

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp