Docker Essentials for Everyone!
Docker, dockerizing those words, is very common today. let’s find out what are they.
Docker is a tool which is a tool which makes it easier to create, deploy and run software applications by using a virtual environment called containers. containers include all the packages, libraries, and other dependencies.
As an example. let's assume we have to run a Nodejs application. Usually, we need to install node js, express js, and other packages as requirements of the project. The real pain begins when we have to install the specific versions of packages by updating or uninstalling previous versions.
Docker provides the solution called a container. We can install our dependencies inside the container.
Docker image is the person who holds the containers. And it executes the code in the container.
Docker container is the running unit of the software.
A single image can hold multiple containers.
Let’s dockerize a Nodejs project.
Step 1
Download the startup project using GitHub repo https://github.com/lkmSasanga/docker-101
After downloading then unzip it. Let’s check the files.
public: some CSS styles for the template
Dockerfile: Contains the guide to docker, how to run relevant to the project requirements. Let’s much dive deep into the Dockerfile. Because it is important to know how to create a docker file relevant to project requirements.
FROM node: 12
— specify the node version
WORKDIR /app
— project working directory inside docker
COPY package.json /app
— copy the package.json file into /app directory
RUN npm install
— install node modules need to run the Nodejs project
COPY . /app
— copy all the project files into /app directory
EXPOSE 80
— configure a port to run Nodejs project
CMD ["node", "server.js"]
— command that needs to run the Nodejs project
As we see Dockerfile is the heart of the dockerizing process.
package.json: contains metadata relevant to our project.
server.js: contains the script of our project
Step 2
Open the terminal inside the downloaded project folder. Type cmd and press enter.
Then type docker build -t nodejs .
in cmd and hit enter.
docker build
will create the docker image relevant to our Dockerfile. -t
is used to add a tag to the image. So we created our first docker image called nodejs.
Step 3
Now we are going to run a docker container.
docker run --name nodejs-app -p 3000:80 your_docker_image_id
Run the above command with your docker image id shown in your cmd.
Congratulations!
You have dockerized your Nodejs application successfully :)
Navigate to localhost:3000 to see the running application.
Explanation of docker run:
docker run --name nodejs-app -p 3000:80 your_docker_image_id
--name
— to add a name to the container (nodejs-app)
-p
— define the ports which are needed to run Nodejs application
3000:80
— first port 3000
is our local port and the second port 80
is the port we defined in Dockerfile for docker.
You can see your image and the container we just created using the docker desktop.
Most common docker commands you must know
docker ps
— shows actively running containers
docker ps -a
— shows all the containers
docker stop container_name
— stop a container that is actively running
docker rm container_name
— remove a container
( You can also use container_id to mention the container )
ie: docker stop container_id
docker images
— show all the docker images
docker rmi image_name
— remove an image
Now we are going to stop our running container. Remember even you closed the cmd docker container keeps running. So we need to shut it down.
docker stop nodejs-app
Important tip
Docker provides amazing help documentation via cmd. Try it when you are stuck or need to find docker commands quickly.
docker --help
Also, you can use this when the point you need help.
docker run --help
— shows what can we do with the docker run
command.
Conclusion
We discussed the docker basics. Actually, they are like ABC of Docker. There is a lot to learn. I invite all to go deep dive into Docker. https://docs.docker.com/get-started/overview/
Hope you enjoyed :)
Thank you for reading!