Docker in real life environment
Download and install docker from Docker
Verify the download using following command docker version should display in console
docker -v
It is a blueprint of container, like class and objects where Image acts as a class and containers are objects
contains all the applications and resources that are needed for a project.
let's say we're using following modules
- mongodb v3
- node v19 and what not !
Let's say I am using node v19 and my teammate is using node v18. That's where docker kicks in and make the environment consistent, a dockerfile works as a package.json to setup the project environment
It eliminates "It works on my machine problem"
- They're portable
- They're light-weight
- They're isolated from local environment
docker pull IMAGE_NAME
Example:
docker pull hello-world
docker pull IMAGE_NAME:version
Example:
docker pull mysql:8.0
docker images
docker run IMAGE_NAME
Example
docker run hello-world
Example: Detached mode
docker run -d hello-world
Note: if you need to monitor the output of a container or interact with it, attached mode is more appropriate. The attached mode actively occupy the terminal.
docker run -it IMAGE_NAME
Example: This will start a ubuntu console
docker run -it ubuntu
NOTE: Docker run is used to create new containers out of the images Docker start is used to run existing containers with name or ID.
docker run [-it] --name name IMAGE_NAME
exit
docker ps -a
docker ps
docker start ( CONTAINER_NAME || CONTAINER_ID )
Example: starting a container with name
docker start quirky_sanderson
Example: starting a container with ID
docker start 6cb01cd7814f (Initial 6-7 bits of long ID)
Example: starting a container in interative mode
docker start -i 6cb01cd7814f (Initial 6-7 bits of long ID)
docker stop ( CONTAINER_NAME || CONTAINER_ID )
Example: stopping a container with name
docker stop quirky_sanderson
Example: stopping a container with ID
docker stop 6cb01cd7814f (Initial 6-7 bits of long ID)
Note: Containers with the image must be removed first
docker rm container_name
docker rmi image_name
Note: PORT in containers differ from the host machine. To map the container ports with host machine we can use following commands
docker run -p HOST_PORT:CONTAINER_PORT IMAGE NAME
Example:
docker run -p5173:5174 reactENV
docker run -e VAR_NAME=VAR_VALUE CONTAINER_NAME (or container_id)
Example:
docker run -e PORT=5000 reactENV
Note: you have to start the container before creating the bash
docker exec -it CONTAINER bin/bash
Example:
docker run -e node:latest bin/bash
Docker network is automatically made out of compose.yaml
docker compose -f compose.yaml up -d
docker compose -f compose.yaml down
