How to create a Container Image with Docker

Creating and docker container with deno javascript framework installed

Nife Oluyemi
3 min readJun 17, 2020

In this post, I document how I created a container image, and pushed the image to docker hub with is open for the public use. I am creating a container image for hosting a deno.js application.

To follow through with this tutorial, you need a docker account. If you don’t already have a docker account, go ahead a Sign up here. Also you would need to download and install docker locally on your machine. (https://docs.docker.com/get-docker/)

Brief Introduction

Docker is a container runtime that helps developers develop and deploy applications in neatly packaged lightweight containers to enable the applications to work seamlessly in different environments.

A Dockerfile is a text file that contains all the commands arranged sequentially needed to create a Docker image. The Dockerfile is used to create your own custom Docker container image from a base image by executing the specified instructions on the container created from the base image.

Steps to create a Container Image using Docker…

We would mostly be using the terminal in this tutorial.

  • Login to Docker Desktop
  • Create a directory, and change to that directory
mkdir denojs-container;cd denojs-container
  • In the denojs-container directory, create the Dockerfile
touch Dockerfile
  • Copy and paste the following in your Dockerfile:
FROM centos:8RUN yum update -y && yum install unzip -yENV HOME="/root"WORKDIR $HOMERUN curl -fsSL https://deno.land/x/install/install.sh | shENV DENO_INSTALL="$HOME/.deno"ENV PATH="$DENO_INSTALL/bin:$PATH"RUN mkdir -p $HOME/app

Line-by-Line explanation:

1. Every valid Dockerfile starts with the FROM instruction. This sets the base image for your container. The only instruction which can come before FROM is ARG. I am using CentOS as the base image, with tag “8”, for the image version

FROM centos:8

2. We need to patch the container operating system and install unzip (required for deno install) when we build the docker image. Hence, we instruct docker to execute the commands preceding the RUN instruction. RUN is the equivalent to executing a command on the OS terminal.

RUN yum update -y && yum install unzip -y

3. Here we set an environment variable — $HOME, which is the home path for the root user, used in subsequent instructions.

ENV HOME="/root"

4. WORKDIR enable us to specify the working directory to be used by the subsequent instructions in the dockerfile till we specify another WORKDIR. The path specified will be created if it doesn’t exist. This is our installation

WORKDIR $HOME

5. Now that we have specified where we want to run the deno.js installer, we can go ahead and create an instruction to run the install script.

RUN curl -fsSL https://deno.land/x/install/install.sh | sh

6. Creating the environment variables for the path which contains the deno.js executable files. On the next line, we add the install path to the linux PATH, which tell the shell where to find the deno.js executable files

ENV DENO_INSTALL="$HOME/.deno"
ENV PATH="$DENO_INSTALL/bin:$PATH"

7. Run a command to create a directory to store your deno application files. This step is not mandatory.

RUN mkdir -p $HOME/app

Building and Pushing the Container Image

Still in the same directory, enter the following command on your terminal to create the image.

docker build -t <REPLACE_WITH_DOCKER_ID>/deno .

The period (.) at the end of the command tells docker to look in the current directory path, which contains the dockerfile. If your dockerfile is in another directory, you can build the image by specifying the path to the directory.

This image is stored on your local machine. To view the list of images:

docker images

To run the container image:

docker run --name deno -it <REPLACE_WITH_DOCKER_ID>/deno

Then finally, to push the image to docker hub, run the following command:

docker push <REPLACE_WITH_DOCKER_ID>/deno

Your image will be pushed to Docker hub, which will appear under Repositories in your Docher hub profile. Congratulations!! Anyone can pull your container image.

--

--

Nife Oluyemi

/neefeh/. Engineering @ Twitter. Seeking Focus. Passionate about Design, Development, Cloud Computing, Resume Editing, and BetterBrain. Views are mine.