Mastering Docker: Unleash the Full Potential of Containerization, Part-2:
“Everything you need to know about Docker”.
Introduction:
Hey there, Am Vishwa! DevOps and OpenSource Enthusiast. Currently learning Docker with the help of OpenSource resources. I completed certain tutorials to know about Containerization and also did some Hands-On too. I already released Part-1 of this Mastering Docker article. Here, we are going to learn about How the Docker CLI works, Various Docker commands, Accessing a Container locally, How to Create Docker Images, Container Port vs Host Port, Debugging Containers, and Docker Networking. Let’s start reading.
Docker CLI and How it Works?
- It is nothing but Containers that are isolated environments where we can run our applications. We don’t want to install any applications, we can just spin off a container.
- $docker run hello-world -> Here “run” is a keyword that runs an Image to create a container, and “hello-world” is an Image.
- $docker run -it ubuntu -> Here, “-it” is an interactive environment that runs Ubuntu OS in it. Type “exit” to come out of the entered image, eg. Ubuntu.
- Here, the Docker run command will run the image if it is available in the local system, if it is not there means, it goes to the Docker Hub registry & start downloading from there.
This is How it works, let’s discuss more about Docker commands.
How does the Docker image work?
It has a smaller version of the OS like OS files and the application that we want to run as well as the dependencies. It is pulled from the Hub then it runs to create a Container.
Docker Commands:
1. We saw about the docker run command above itself. Now let’s see various commands in it.
2. $docker images -> It lists the downloaded images in the local system.
3. $docker pull image_name -> It directly downloads the image from the Hub.
4. $docker ps or $docker container ls -> To see the currently running containers.
5. $docker ps -a -> It shows all the running/stopped containers.
6. $docker container exec -it container_id bash -> Here, “exec -it” is used to run the container in the “bash” of the Host OS that is outside of the container.
7. $docker stop container_id ->Used to stop the container.
8. $docker start container_id -> Used to start the stopped container.
9. $docker rm container_id -> It removes the container from the list.
10. $docker inspect container_id -> It gives the entire information about the container that is including image info, network info & so on.
11. $docker rmi image_name -> It deletes the image. Use “f” at last with space to delete forcefully.
12. $docker container prune -f -> Here, “prune” indicates stopped ones, and “-f” indicates all. It deletes all the stopped containers.
13. $docker images -q -> Will display all the ids of the downloaded images.
14. $docker run -d image_name -> Here, “-d” is used to run the container in the detached mode which is nothing but runs in the background & gives us the container id.
Accessing a Container locally:
- $docker run -d -p 8080:80 nginx -> Here, “-p” indicates port, “8080” is the local port whereas “80” is the containers port.
- Port forwarding is happening in that the application will run inside the container but we can access it from the local host.
Note: Will see in detail about Ports & Port forwarding in this blog itself.
How to create a docker image?
- By using the docker file, we can create the docker images.
Example: DockerfileFROM ubuntu
CMD ["echo", "Hello-World"]
- In the above code, “FROM” is the base image and “CMD” is an entry point that runs the echo cmd in the Ubuntu container & gives the output Hello-World.
Note: Will see the syntax of a Dockerfile in the upcoming part.
- $docker build -t myimage_name:1.0 . -> Here, “build” is a keyword with a build tag “-t” to build the docker image, then give any name to the created image with the version tag, and at last “.” is nothing but mentions the current directory.
Container Port vs Host Port:
- Multiple containers can run on your host machine.
- Your laptop has only certain ports available.
- To avoid conflicts, use different ports of the host machine.
- If we run the same applications but with different versions both run at the same port.
Example:
Container1(Port 3000) -> Host Machine(Port 5000),
Container2(Port 3000) -> Host Machine(Port 5001),
Container3(Port 3001) -> Host Machine(Port 5002).
Here, Containers 1 & 2 are running on the same ports so we bonded those with different ports of the host machine to avoid conflicts. This is called port forwarding.
- $docker run -p 5000:3000 redis, Here, 5000(Host Port):3000(Container Port),
- $docker run -p 5001:3000 -d redis:4.0 -> (Runs in detached mode).
Note: If we run the same application image multiple times it will create different containers.
Debugging Containers:
- $docker logs container-id/container_name -> To see the logs of the container.
- All the containers will be given a random name automatically. If we want to give names to it,
- $docker run -d -p5000:3000 — name redis-older redis:4.0 -> Here, “ — name” is a keyword to set the name of the container and “redis-older” is the name.
- $docker exec -it container_id/container_name /bin/bash -> It opens a terminal of the running container, and it helps us to do a lot of stuff there.
Docker Network:
- Docker will create an isolated network where we can run many containers & it will connect without port numbers because it’s in the same network only.
- $docker network ls -> Used to list existing networks
1. Creating own Network:
- $docker network create mongo-network-> Here, “mongo-network” is the network_name.
2. Setting created network:
- $docker run -p 27017:27017 -d -e MONGO_INITDB_ROOT_USERNAME=admin MONGO_INITDB_ROOT_PASSWORD=password — name mongodb — net mongo-network mongo
- Here, “-e” indicates the environment, the “credentials” are found from the documentation of the docker, and “ — net” specifies to which network the container should be connected.
That’s it for now folks! Let’s discuss more about How to create Docker Compose files, What is Docker private registry and Docker Volumes in the next Part.
Thanks for the read guys, do like, comment & share guys.