Creating your first Dockerfile

creating your first dockerfile

1. Create the Dockerfile

Create an empty directory for this task and within this create an empty file with the name Dockerfile. You can do this by issuing the command touch Dockerfile from within the directory.

Congratulations, that’s it you just created your first Dockerfile…

Seriously though let’s create one that actually does soemthing đŸ˜‰

We will create an image using the Alpine base image which by default does not have vim and curl installed. So let’s create a custom image from Alpine that has vim and curl included and this will constitute your first custom Docker image build from your first (useful) Dockerfile.:

2. Define the base image with FROM

Every Dockerfile must start with the FROM instruction. The idea behind is that you need a starting point to build your image. You can start FROM scratch, scratch is an explicitly empty image on the Docker store that is used to build base images like Alpine, Debian and so on.

You can start you Docker images from any valid image that you pull from public registries. The image you start from is called the base image. In our case let’s add FROM alpine:3.4 to the Dockerfile.

Right now your Dockerfile should look like this:

FROM alpine:3.4

3. Add the lines to install packages

Please add the lines to install vim and curl like this:

FROM alpine:3.4

RUN apk update
RUN apk add vim
RUN apk add curl

4. Build your image

Please run the following in terminal: docker build <name of your directory> -t alpinetest

This command is structured as follows:

  • docker build is the command to build a Docker image from a Dockerfile
  • -t alpinetest defines the tag (hence -t) of the image, which will be basically the name of the image.

5. Enjoy the results

Docker created an image from your Dockerfile. You should see a new image in your image list issuing docker images

Let’s check what’s inside our new image, let’s run the following command and check out vim and curl: docker run -it --rm --pid=host alpinetest

Right now you should be in the shell of your running container, so let issue the following commands: vim --v and curl --version. You should be seeing the version of vim and curl in your terminal.

You have successfully built an image for a Dockerfile

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.