We build images based on commands we give in the Docker file.
A Docker file is the text file that builds Docker images. This text file contains particular instructions about the container. To build a Docker file, follow the steps below.
FROM node --> from official node image in docker hub(this will be the base image)
WORKDIR ./app --> this will be the working directory of all containers built on the image, i.e. all, run, copy commands will be executed in the working directory
COPY package.json /app --> From where To where
RUN npm install --> install all the packages in package.json
COPY . /app --> copy all the folders in the current directory in the local machine to the container working directory(app)
CMD ['node','filename.js'] --> this statement will only get executed when we create container. The main purpose of the CMD command is to launch the software required in a container
Docker commands: What we type in the command prompt:
To create an image --> docker build.
Any command with --help will give you all associated commands with a description
We build containers based on image --> docker run <image_name>
docker run <image_name> will create new container based on image.
What if we want to start an existing container?
Use docker start <container_name>
Use docker stop <container_name>
There are two modes - attached(console.log is displayed) and detached mode(console.log is not displayed)
docker run <image_name> --> creates new container and prints all console.logs -->default is attach mode.
docker run -d <image_name> --> Run the container in detached mode - i.e., output printed by the container is not visible, the command prompt/terminal does NOT wait for the container to stop.
docker start <container_name> --> starts already existing and stopped container with default of detach mode(no console.log printed)
docker start -a <container_name> --> for attached mode
We can make it interactive(use giving input in terminal) through the -i flag before
Deleting images and containers
docker ps --> list all running container
docker ps -a --> list all running and stopped containers
docker images --> list al locally stored images
docker rm <container_name> --> removes the container
docker rmi <image_name> --> remove the image
One thing to note in removing images is that it does not have any containers including a stopped ones
Naming Image and Container
When we name a container, it is called Name, but it is called Tag when we name an image.
Tag consist of two parts - name:tag --> node(name):14(version)