FrankTheDevop FrankTheDevop FrankTheDevop FrankTheDevop FrankTheDevop

What´s a Dockerfile?

150 150 Frank

Hey everyone,

after the introduction of Docker I thought I show you the recipe to create such a container. The recipe is like a recipe for your favorite lasagne and is called Dockerfile.
It looks similar to this:
FROM node:8.15.0-alpine

# Add application folder
RUN mkdir /app

# Add package.json and install deps
ADD package.json /app/package.json
RUN npm install

COPY . .

# Expose the listening port

# Start the server
CMD [“pm2-docker”, “start”, “process.yml”]

As you see it is quite short and nothing to be afraid about. So lets get through it:
FROM node:8.15.0-alpine
As mentioned in the introduction Docker uses multiple layer to form a container. With this we tell it which container in which version to use as our starting point.

# Add application folder
Lines starting with a # are comments. Use the to explain anything that isn´t clear to you

RUN mkdir /app
RUN is the command to execute a custom command. In this case create a directory app in the root of the container filesystem

WORKDIR is similar to the commandline command cd. It changes the current working directory for the following commands.
Here we change into the newly created directory /app.

ADD package.json /app/package.json
Adding a file to the container in the mentioned path (/app/package.json).

RUN npm install
Again we run a custom command, here npm install to install all dependencies for the used Nodejs project.

COPY . .
COPY copies all content in the current folder recursively into the mentioned path of the container (. translates to the last WORKDIR command, so we copy to /app).
COPY respects a .dockerignore file, with which you can filter out files you don´t want to copy over (e.g. log files).

CMD [“pm2-docker”, “start”, “process.yml”]
Docker needs a starting point / software. If you want to let the container running the software needs to run.
With CMD we define which software we want to use for this.
Here we use pm2-docker from the pm2 package to keep (nodejs) projects running (restart when necessary, etc.).

And that´s it. Basically you can now already package your own project into a docker container. Just execute “docker build -t $yourname/$your_container_name:$your_version .” and it will be build. An example can be “docker build -t frankthedevop/MyTodolistAPI:v0.0.1 .”.
You can then run it with “docker run $yourname/$your_container_name:$your_version” or in my case “docker run frankthedevop/MyTodolistAPI:v0.0.1”. More details about the commands in a later post.

Just remember that we are locker inside the container, that means that all paths are inside the container too. If you have e.g. a configuration file in your personal user directory make sure to copy it to the correct point inside the container, otherwise it doesn´t exist.

This is just the beginning, but I wanted you to see how short and easy such a recipe can be.
You find further links in the sources. And I will create a more extensive post about it soon too.

Feel free to contact me about how to create a Dockerfile for your project. I will do my best to help you :).

Yours sincerely,

(1) Docker Dockerfile Documentation
(2) Docker 101: Fundamentals & The Dockerfile
(3) Inspiration how to use Dockerfiles

Leave a Reply

Your email address will not be published.

four × four =