Episode 5. Understanding Docker Images and Containers in Depth
→ There are two core concepts in Docker — Images and Containers
→ We use the pre-built images for our application eg python, node.js or organization customized base image.
→ Companies may have their explicit SSO for application securities.
→ We create a number of instances from the image (i.e. containers). The best part is no code duplication.
→ Additional code is just a layer on top of images.
Image vs Containers:
Containers: Here software is always running i.e. containers are running instances of images.
i.e. Image: Code plus template (Eg: node.js + app code)
→ We can think of images as a class and containers as instances of the class.
→ There can be one image. It can run multiple containers.
Getting Pre-built Image:
Docker Hub: https://hub.docker.com/
→ We can choose any technology as per project requirements. Let us use node.js. Search Node
→ We do not have to download the image (i.e. node), we just copy the image name
Commands:
Let us now go to the terminal (or command prompt) for executing commands:
- Check available containers locally:
docker ps -a
Here we see all the latest containers available.
2. Import Image from Docker Hub:
Let us now import image node image from Docker
docker run <image-name>eg. docker run node
→ Let us re-run the command to get list of containers:
docker ps -a
→ This time we see a new node image is created and ready to be used. Also, we see automatically received names to the image.
→ To get the image + run in a container, we can use interactive mode.
3. Interactive mode:
→ By interactive mode we simply mean, the terminal can ask questions or show some message
docker run -it <package name> // container interactive modeeg. docker run -it node
→ We get a welcome message from the node (v 17.4.0)
→ It is important to understand, we have installed node.js in docker and not in our local system.
→ If we have installed node.js in our system previously, we see differences in versions.
→ We even do not need node.js locally
→ If we run the command again, we see multiple containers running to the same image.
docker ps -a
→ In the last module, we ran our first Dockerfile, and our application was running.
→ There we ran Dockerfile and ran the server, we will understand DockerFile configurations in detail (Check the video here)
FROM node // Use base image from Docker HubWORKDIR /app // Paste code insode /app directory of containerCOPY . /app // first parameter= copy all file from current directory, paste inside container’s /app directoryRUN npm install. // run npm install. (in container)EXPOSE 80. // Use port 80CMD ["node", "server"]. // Run node server (in container and not image)
4. Build the image from Dockerfile:
docker build .
4. Run the container from Build ID
docker run <image id>eg. docker run 0364ae96bd20f5d9361564e907d9f1f1656fca2d9f2996c0496ca713afdae76
→ We see a progressive terminal, which means our server is running
NOTE:
Docker Build — This command checks Dockerfile commands (returns ID)
Docker Run — Executes the image and generates the container
→ Let us go to the browser and run our server
http://localhost:80
→ Our application does not run 😳
œ Check all running containers:
This command gives only running containers
docker ps
NOTE:
docker ps: Checks our running containers
docker ps -a: Checks all the containers (running plus stopped ones)
6. Stop containers:
→ First, we see all running containers using the above command (point 5).
→ Copy-paste names or IDs of the container
→ Stop the container (can be single or multiple spaces separated)
docker stop <containers assigned name>eg. docker stop silly_neumann awesome_ardinghelli nifty_lederberg
→ If we run again, a container running command, we see no containers running
docker ps
7. Running with published port:
→ It is important to understand, there are two spaces — one inside docker and the other exposed outside (say browser).
→ The reason why our application did not run was that port 80 was of docker and not browser.
→ For this, we will run command, with published port
docker -p <localport:dockerport> <container ID>eg. docker run -p 3000:80 20364ae96bd20f5d9361564e907d9f1f1656fca2d9f2996c0496ca713afdae76
So inside docker port 80 is used to interact with browser port 3000 will be used.
→ Let us go to the browser and run our server
http://localhost:3000
→ Our application runs successfully 😀
NOTE:
1. We did not run npm install in our code, docker handles it automatically.
2. We can also just use the first (few) character(s) — just enough to have a unique identifier.
docker run abcdefg. // will work
docker run abc // will also work
Video:
Closing Thoughts:
In this article, we have learnt core docker commands — build the docker image, run containers, check active and all containers, stop containers, publish port, interactive mode.
Practice these commands, it will become our second nature soon.
Thank you for reading till the end 🙌 . If you enjoyed this article or learned something new, support me by clicking the share button below to reach more people and/or give me a follow on Twitter and subscribe Happy Learnings !! to see some other tips, articles, and things I learn about and share there.