Skip to content

Δ.Π.

Introduction to Docker and Basic commands.

Docker, Docker-cli, aws, cloud2 min read

Docker is an open-source project for automating the deployment of applications as portable, self-sufficient containers that can run on the cloud or on-premises.

Installation :

RHEL/CentOS or Linux -> We have Docker Engine download for CentOS/RHEL

MacOS/Windows -> We have Docker Desktop

to run docker container from image [the image can be available in hub or from seperate developer]

1docker run --name name-of-the-container -v host-volume-path:path-to-container-volume -d -p 8080:80 name-of-the-image-to-run

Explaination :

  1. docker run is used to run an image on a container.
  2. --name flag is used to name the container
  3. -v flag is used to mount a host volume to docker volume hostVolume:dockerVolume
  4. -d is for detached mode to run container
  5. -p is used to bind host port to open continer port for the image hostPort:containerPort

Example : if run on same folder location as the file present we can use $(pwd)

1docker run --name website -v $(pwd):/usr/share/nginx/html -d -p 8080:80 nginx

Other useful commands :

to see all containers available

1docker ps -a

to start/stop container

1docker start/stop name/id of the container

to remove all container

1docker rm $(docker ps -aq)

to execute docker shell

1docker exec -it name-of-the-container bash

Sharing data [volume] between two containers : this will create exact replica of volumne from one container to the new one

Here we are mounting volume of website container to website-copy container

1docker run --name website-copy --volumes-from website -d -p 8081:80 nginx:latest

dockerfile

  1. Build our own image [contains set of commands to create an image][doc](https://docs.docker.com/engine/reference/builder/)

Let's create a dockerfile and after that an image from it.

Docker image

  1. image is a template for creating environment of your choice
  2. Snapshot
  3. It has everything to run your Apps
  4. This include OS, Software, App code

A sample Dockerfile example is provided below assuming you are creating it in same folder where your Static website source is available

create a file name as Dockerfile [please note there is no extension to it]

Inside Dockerfile

1FROM nginx:latest
2ADD . /usr/share/nginx/html

Explanation:

  1. FROM keyword is used to specify the base image which our own image will be based on. We always create an image on top of a Base img.
  2. ADD keyword is used to add everything from one directory to the specified directory. Here, . (dot) represents current directory and it says "put everything in current directory to /usr/share/nginx/html"

This is a very small and simple Dockerfile, from this we can create our own image

To create image from Dockerfile

1docker build --tag website:latest .

Explanation:

  1. docker build is used to build image from Dockerfile
  2. --tag or -t is used to provide name:tag to the image [here it is website:latest]
  3. . (dot) is for the path of Dockerfile

you can see the images with docker image ls

Now you can run a container with this image that we have created.

1docker run --name website -d -p 8080:80 website:latest

go to localhost:8080 [or url:8080] to see the website up and running. Any number of containers can be created using that image

Now we are going to create an API and make things a little bit complax.

A sample Dockerfile is given here assuming Dockerfile is created at same place as the project (that's why we are using ADD . .)

1FROM node:latest
2WORKDIR /app
3ADD . .
4RUN npm install
5CMD node index.js

We have .dockerignore file that contains items that needed to be ignored

1node_modules
2Dockerfile
3.git