Docker 101

In this tutorial we will learn about Docker basics like how to create Docker Image, push image to registry and run Docker container.

Technology

Let us start with the important building blocks.

What is Docker?

Docker is an open source containerization software platform. It helps us to build, run, deploy and manage our application.

How to install Docker?

Download the latest version from the official website https://www.docker.com.

What is a Dockerfile?

It is a text base document that contains instructions to create Docker image.

A Dockerfile contains information like the base image, application code files to include, environment variables etc.

Following is a sample Dockerfile.

FROM node:alpine
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

This is a Dockerfile from one of my Node projects simple-restapi-nodejs.

Now let us go through the above file line by line.

Line 1: This is the base image. We create our Docker image using some base image. In this example I am using node:alpine as the base image.

Line 2: WORKDIR this create a working directory. In this example we are creating a directory /app. This work directory will be created inside the Docker Container.

Line 3: COPY this copies package.json andpackage-lock.json files into the working directory we created in Line 2.

Line 4: RUN this is to run some script. In this example we are installing Node packages.

Line 5: COPY . . now we are copying all the files from our source (denoted by first dot) and pasting it to working directory (denoted by second dot).

Line 6: EXPOSE this instruction tells Docker that our container listens on port 3000.

Line 7: CMD this defines the default executable of our Docker image.

Alright, we have our Dockerfile ready, next we have to build our Docker image.

What is a Docker image?

It is an immutable template file that helps in creating Docker Container.

What is a .dockerignore file?

It is a configuration file that helps us to exclude files and directories that we don't want to put inside our Docker Image.

Following is a sample .dockerignore file.

node_modules
npm-debug.log
test
coverage
docs
.idea
.env
.gitignore
.vscode
.git

What is a Docker container?

It is a running instance of the Docker Image.

Create a Docker Image

To create a Docker Image we run the following command in our project.

docker build -t YOUR_NAME/YOUR_DOCKER_IMAGE_NAME .

For simple-restapi-nodejs project I have used the following to create the Docker Image.

docker build -t yusufshakeel/simple-restapi-nodejs .

This creates the image by the name yusufshakeel/simple-restapi-nodejs which we can now push to an image registry like DockerHub.

Push image to DockerHub

We use the following command to push the image to DockerHub.

docker push YOUR_DOCKER_IMAGE_NAME

Example

docker push yusufshakeel/simple-restapi-nodejs

Running the container

We run the following command to run a Docker Container using a Docker Image.

docker run IMAGE_NAME

Example

docker run -d -p 8080:3000 yusufshakeel/simple-restapi-nodejs

This will run a container using yusufshakeel/simple-restapi-nodejsdocker image in detached mode -d and we are publishing -p host port 8080 to container port 3000.

Tutorial video: Build and Push Docker image to DockerHub.