Dockerfile: main commands and instructions
Tags: docker, dockerfile, commands, infrastructure
Published: 2020-06-24 23:39:53

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

```docker
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

```docker
ADD opa.txt /directory/
```

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

### CMD

```docker
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:

```bash
docker run -d my-image /bin/bash
```

### LABEL

```docker
LABEL Description="Bla bla bla giropops"
```

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

### COPY

```docker
COPY opa.txt /directory/
```

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

### ENTRYPOINT

```docker
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

```docker
ENV API_KEY="Igor Souto"
```

Sets environment variables to the container.

### EXPOSE

```docker
EXPOSE 80
```

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

### RUN

```docker
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](https://dev.to/soutoigor/docker-images-containers-and-its-main-commands-hgn) to undestand more about image's layers.

### USER

```docker
USER igor
```

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

### WORKDIR

```docker
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

```docker
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

```docker
MAINTAINER Igor myemail@provider.com
```

Set the container owner.

### Build container

```bash
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.