— Docker, Docker-cli, aws, cloud — 2 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 :
docker run
is used to run an image on a container.--name
flag is used to name the container-v
flag is used to mount a host volume to docker volume hostVolume:dockerVolume
-d
is for detached mode to run container-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
Let's create a dockerfile and after that an image from it.
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:latest2ADD . /usr/share/nginx/html
Explanation:
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.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:
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:latest2WORKDIR /app3ADD . .4RUN npm install5CMD node index.js
We have .dockerignore file that contains items that needed to be ignored
1node_modules2Dockerfile3.git