Docker Essentials for Everyone!

Malindu Sasanga
4 min readJun 27, 2021

--

docker image
image via docker

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.

Docker image and containers
Fig 1. Docker image with multiple containers (image by author)

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.

Fig 2. Folder structure(image by author)

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.

Fig 3. Dockerfile content(image by author)

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.

How to open cmd inside the project folder

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.

docker image id
Fig 4. Docker image id(image by author)

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.

docker container created
Fig 5. Run docker container(image by author)

Congratulations!
You have dockerized your Nodejs application successfully :)

Navigate to localhost:3000 to see the running application.

output
Fig 5. Output in browser(image by author)

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.

Fig 6. Docker desktop screenshot(image by author)

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

stopped container
Fig 7. Stopped container(Image by author)

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!

--

--

No responses yet