Hello everyone I am Goutham here I would like to share basic mostly used docker commands and how to run react application in a container. As Iam beginner to docker I have written what I understood from various sources if you feel any thing wrong please let me know .
Basic Commands
1. docker pull
Used to to pull any non-existing image in local machine from docker registry.
Usage : docker pull node ( docker pull Image_Name)
2.docker run
Used to run the specified image
Usage : docker run node (docker run Image_Name)
docker run -d node runs image in detached mode which means terminal allows to type other command
3. docker ps
Used to display all running containers
Usage : docker ps
docker ps -a used to display all running as well as stopped containers
4. docker start
Used to start the terminated container
Usage: docker start container_id or container name
5. docker stop
Used to stop the running containers
Usage: docker stop container_id or container name
6. docker rm
Used to remove the container
Usage : docker rm container_id or container_name
docker rm -f container_id or container_name used to remove running container
To remove multiple containers use docker rm CId1,Cid2,Cid3 .... .
To display only running, stopped containers we use docker ps -a right but if we want only container Ids of running and stopped containers we use docker ps -aq
By using this we can remove multiple containers :
docker rm $( docker ps -aq )
7. docker exec
Used to run terminal inside a container
Usage: docker exec -it GouthamApp bash (docker exec -it container_name shell_name)
Docker Volumes
Docker volumes are basically used to share files/folders among containers or with host and containers.
Share folders from host to container
Usage : docker run -v hostfolderlocation:/index.html Image_Name
docker run -v --name website d:/clones/linkedin-clone:/usr/share/nginx/html nginx
Share folders among containers
Usage : docker run --name webcopy --volumes-from website nginx
webcopy is a newly created container and website is already existing container and folders from webite container will be copied to webcopy container.
--name is used to rename container instead of some random names.
Port forwading
Application in a container run in a isolated area not knowing anything about out side world but as a developer we need to know how the app works in a container from outside.So App runs in some port in a container so we need to map host port to container port.
By this we can examine how app works in a container from host.
Usage: docker run --name GouthamApp -d -p 19531:3000 node
Here we are mapping container's 3000 port to host 19531 port and naming container as GouthamApp.
Images
Images are just artifacts for any app which contains pretty much everything to run app such as OS,Software, Appcode etc..
1.docker images
Used to list all existing images
Usage : docker images
We can build our own Images aswell through command:
docker build --tag webapp :latest .
'.' dot there means Docker file location images are built using docker file which contains instructions to generate docker image.
We build any image by taking some other image as base image ,mostly alpine a linux distro is used because of its lite weight.
Running React App in docker
We are actually interested in how our app works no matter what environment one has we need node to run react and some dependencies every thing that our app requires is present in docker file which in turn used in generating image.
We only run image not our react app by the commands inside the docker file it automatically runs react app . This is how docker file looks like.
In deployment phase they pull image and run that
If we run the image that our app by default runs in port 3000 then we have to map containers 3000 port to host 3000 port.
Then we can view our app in host ๐
In docker file
Meaning of 1st line is base image as I have already said we don't build image from scratch node:alpine means base image alpine is used to reduce image size.
Meaning of 2nd line is we want all our react app folders to be in app folder inside container
3rd line : Copy the package.json file
4th line : Installing all dependencies that we have used in our app
5th line :Copying our application code to app folder inside container
6th line : Start our app inside container.
These steps create layers for image.
That's it we have now deployed our app with docker
.dockerignore
We don't want some folders to be pushed to container like node_modules as they will be installed while running image and we dont want dockerfile as it is of no use in container.So add them in .dockerignore file.
Thanks for reading my blog hope this helps ...๐