Skip to main content

Dockerfile

· 3 min read

Hello, this post will explain how to create a Dockerfile for a Ruby container that already has MDL installed. Please check my previous post to better understand Docker.

What is t?

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

Much like Vagrant that uses a vagrantfile to create and customize a virtual machine to your needs, Docker uses a dockerfile to create and customize a container! They may be similiar but they are very different! (you can learn how to use Vagrant to create a Windows Server vm here)

As meantion earlier, for this example, we're going to create a ruby container with mdl installed. So we need to start our dockerfile:

FROM ruby:latest

FROM creates a layer from the ruby latest Docker image.

Next:

FROM ruby:latest

WORDIR /mdl

WORDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.

Next:

FROM ruby:latest

WORDIR /mdl
RUN gem install mdl

RUN has 2 forms:

  • RUN command (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
  • RUN ["executable", "param1", "param2"] (exec form)

In our example, RUN executes a command that installs MDL directly in the ruby container

Next:

FROM ruby:latest

WORDIR /mdl
RUN gem install mdl

ENTRYPOINT [ "/bin/bash" ]

ENTRYPOINT, like RUN, has 2 forms! Shell form or exec form.

The exec form, which is the preferred form:

ENTRYPOINT ["executable", "param1", "param2"]

The shell form:

ENTRYPOINT command param1 param2

In our case, we're using it in exec form so we can execute our bash. This will leave our container open because bash is running in the background.

So, now that we have our dockerfile we have to build our container in order to run it!

docker build --tag user/mdl

The ---tag command names the image of the container in question. And now that the container is build we can run it!

For this will use the following command:

docker run -tid --name mdl -v /home/vagrant:/mdlfiles user/mdl

To be sure if your container is running properly do a simple docker ps -a just to have confirmation. Doing so will show you all of your containers details and status.

After confirming that the container is up and running we want to execute mdl to check our markdown file. To do so we'll need to run the mdl command inside a bash that will get the .md file inside the folder that we shared and named "mdlfiles"

docker exec mdl bash -c "mdl /mdfile/myfile.md"

If you have some inconsistencies regarding the rules of the mdl tool you'll get a result like this:

/mdfile/docker.md:5: MD009 Trailing spaces
/mdfile/docker.md:77: MD009 Trailing spaces
/mdfile/docker.md:25: MD012 Multiple consecutive blank lines
/mdfile/docker.md:51: MD012 Multiple consecutive blank lines
/mdfile/docker.md:13: MD013 Line length
/mdfile/docker.md:52: MD013 Line length
/mdfile/docker.md:54: MD013 Line length
/mdfile/docker.md:89: MD024 Multiple headers with the same content
/mdfile/docker.md:19: MD026 Trailing punctuation in header
/mdfile/docker.md:26: MD026 Trailing punctuation in header

After correcting all of them you should have no return after your command! :)

NOTE: if these command lines are confusing to you or you don't know what something means, please check my previous post.