Basics of Docker
A prerequisite for container deployment is that it should be packaged as a container image.
Container is a light weight deployment mechanism based on the OS virtualization.
Docker engine is a platform to run container across various platforms.
Docker engine can be installed as Linux and Windows container engine.
Docker Engine Installation
Docker engine for various platform can be downloaded from Docker website: Install Docker Engine | Docker Documentation
Verify docker installation
Docker installation can be verified with running a bare minimum container: Hello world container
docker run hello-world
- Logs show image doesn’t exists in the local docker repository and hence docker service downloaded the image from the docker hub repository to run the container.
Run docker container with startup command
We will run
busybox image and override the startup command of the docker container.
Busybox image: Busybox is a container image that provides basic Linux commands.
docker run busybox echo "Running busybox container"
Docker Execution Flow
Let’s try to understand what actually happened in the above scenario.
There are following major components involved when running the docker container:
- Docker client
- Docker service
- Docker registry ( local repository and remote repository: docker hub)
- Container image
Execution flow ( let’s take example of busy box )
- Docker client exposes docker service APIs with commands.
- When we executed the docker run command on the Docker client, it invokes the docker service API for running the container.
- Docker service can be on the same machine as the docker client or can be on the remote machine.
- Docker service will check if the docker busy box image already exists in the local repository. You can consider a local repository as a cache.
- If the local docker repository doesn’t contain the busybox image then the docker service will download the image from the docker hub.
- Docker service will create a container out of the busy box image.
- As soon as the container is ready, the container will run a start-up command.
Run any docker image
docker run <docker_image_name>
When you specify an image name without a tag, the docker engine by default considers the tag as default.
In case you want to explicitly specify a tag
docker run <image_name>:<tag>
Let’s try a hands on exercise by containerizing a node application.
Create a node app
- Create a root directory :
- Follow below steps to create a sample node applications: Node.js Example Application — Understand the basics of Node.js Application (tutorialkart.com)
- Modify the port in above app to run it on port 8080
- Run your app and verify it in browser by accessing URL :
Package app into image
To build and package your app into a docker container image, you need a docker file.
Go to the
sampleApp directory and create a file with name
FROM node: 10
ADD node-js-example.js /app.js
ENTRYPOINT ["node", "app.js"]
Anatomy of file
- FROM defines the base image.
- ADD will add file
node-js-example.jsto container image with name
- ENTRYPOINT specifies the command that will be executed when container starts up.
docker build -t mynodeapp .
As you specify the current directory ( directory `.`) as the working directory. Docker engine will look for the docker file in the current directory and build the container as per the instructions being mentioned in the docker file.
- Docker client uploads complete build directory to the docker service.
- Docker service build image by executing the instructions mentioned in the docker file.
- It stores the generated image in the local docker repository.
Docker images are layered architecture. We will cover this in details in the subsequent articles.
List images which are stored in local docker repository
Your image will be listed here once you build it.
Run docker container
docker run --name mynodeapp-container -p 8080:8080 -d mynodeapp
name: Name of the container
p: Map application port to the container port
d: Name of the container image
You can list all running containers
In case you want stopped containers as well.
docker ps -a
Detail view of a running container
docker inspect mynodeapp-container
Look into running container
Sometimes to debug a container, you have to login inside the container.
docker exec -it mynodeapp-container bash
i stands for keeping standard input open and
t stands for pseudo terminal.
Check the number of running processes inside the container
Stop and Remove container
docker stop mynodeapp-container
docker rm mynodeapp-container