Before you start reading about this topic
Hello everyone. This is my second post on what Docker Container is and how it work. In the previous post I explained what a Docker container is, how it works, what it does, and how to install Docker. I also gave several examples and commands to start learning about this fascinating and very helpful technology to improve processes and productivity for enterprises. Please read my previous post first if you just started learning about Docker Container.
Learning more about Docker Containers
Now that you know how to install Docker and use basic commands let's investigate more beginner content using containers to run more processes since this is one of Docker’s most compelling value propositions.
To start running processes with Docker
In the command line, type "docker run" to start running a main process in the container. When that process exits then the container stops. Docker containers have one main process and Docker assigns a random name if you don't assign one.
Create a Self-Terminating Process Using the Remove Argument
When the remove argument is added to the run command, it removes the process after it is executed. This argument is very helpful, and people often use it when they run processes and in a container but want to remove the container afterwards. This can save time by ensuring the container doesn’t need to be manually removed later.
Example of running a container and remove it afterwards:
This command will run the container in the Ubuntu machine, execute the command "ls" in terminal and after it will be removed.
--rm = remove
-ti = Terminal Interactive
Now, let's try another example.
Example of running a container with different processes:
With this command we are running a Bash command in the terminal. Specifically the command and arguments will do the following:
- The machine will sleep for 3 seconds
- It will display using echo "What is my name?",
- It will execute the command "whoami" that displays the current username from that container.
Create a Container that Runs Indefinitely
Example of run a container and leave it running:
-d = detach
The above command and arguments will do the following: the argument -d (for detach) starts a running container, leaving it active in the background. Following the command, an container identifier is displayed so you can go back and find it. You can use the the command "docker ps" to find all the information from the container, such as the container ID, Image, command, created, status, its name and, ports.
Attaching an Argument
Example of attach a container:
Using the same container before, with the command "docker attach" you can go back to the container that is currently running. If you would like to exit this container you can press control + p, and after control + q then you will be escaping the sequence but this container will still running. Therefore, you can always go back using the attach command to access to the container again.
Running more Processes in an existing Container
With the command "docker exec" you can add more processes in an existing container and enable debugging to troubleshoot processes or complete other administrative activities.
An example of the exec command:
Now with docker exec you can run more things in your container. As you can see in the above image, I used “exec” following by “-ti” (terminal interactive), and gallant_nash (the name of the container I want to access to).
Next, I used the “ls” command to display the local folders.
Then, “touch MYFOLDER” is used to create a folder in the same directory.
In the second command line window, I used the "attach" command, to gain access to the container. Then I used “ls” to see the current directory and the folder I have created previously using the exec command to run more processes.
To exit the container, press control + D. This will close your attached process along with the original container.
Managing Multiple Containers
When you have several containers running at the same time and some of them are not working, you’ll want to go back and debug them, as necessary. When a container does not work, it can be frustrating: you will want to know what is the issue so you can fix it. To do this in Docker you can access to the logs files to debug. If the container is active, docker will keep the logs available.
Example on how to find the logs for debugging:
In the first command, I am running a new container named "ErrorMade". An identifier is created. To show information on whether the container is running or not I ran the command "docker logs" to find a possible error. The logs then displayed in the last line showing the command error on the specific line (“bash: line 1: eechoo: command not found”). With that information I was able to find the error and change "eechoo" to "echo", solving the problem.
Removing and Stopping Containers
You can stop and remove any container you created by killing the process then removing the process. The command "docker kill" will finish the process from running and the "docker rm" remove the container forever.
Example for killing the process and removing the container:
If you want to stop the process, the command “docker kill” will help to finish it. In the first window terminal, I ran a new container. In the second terminal window I typed "docker kill unruffled_blackburn" (“docker kill” followed by the name of the container). This stopped the process and kicked the terminal out of the container.
Example for removing the container:
Killing a process does not mean that you removed the container. This can cause problems—for instance, sometimes you want to create a container with the same name an existing container. In such cases you will receive an error stating "the container already exits". This is because the container still exists in your machine and you will have to remove it. In the command line above, I used the command "docker rm unruffled_blackburn” (docker rm, followed by the name of the container). I then re-entered the command to confirm removal, and an error message showed the container no longer exists.
Connecting Containers to a Network
Docker is typically used to run different services on a network, including ones found in DHCP servers, web servers, database servers, or others. Docker makes this easy by offering different network options to connect the containers and to connect them to the internet. For example, you can use a private network to connect them together so they can
talk to one to another but still remain isolated from the rest of your network. To get data in and out of the system, Docker allows for you to expose ports, making them accessible on one machine by another hosted Docker machine. By using both private networking and ports you can customize your network however you would like.
With Docker you can expose a specific port inside the container and outside, you can also expose as many ports as you need. Coordination is required between containers and it is easy to find the exposed ports.
Example on how to connect containers in the network:
In the server I used “-p” to open port 777 and port 888 and named this container myServer. This container is of an Ubuntu operating system. In the third line, we can see nc is not installed in this Ubuntu machine, I had to install it using the command "apt-get update && apt-get install -y netcat".
In the server I ran the "nc" command to listen to port 777 and 888:
Now that "nc" is installed in the Ubuntu container I can run the netcat command to listen to ports 777 and 888 using the parameter -lp that stands for "listen port". The data enter the machine from port 777 and leave the machine on port 888.
Let's have a look on client one and client two:
As mentioned, the first machine is listening to port 777 and sending to port 888. In the second terminal in client one, I used the command "nc localhost 777" to listen to the port 777. Client two in the third terminal window will listen to port 888 using the command "nc localhost 888". After this, I used client one to write the message "Hello this is client one!" and pass to client two. I then used client two to write "Hi, this is client two!", "sending more information from client two" to the local host container.
Checking which container ports are open:
In case you want to check which ports are open and available in the Docker container, you can use the command "docker port". This command is very useful when Docker assigns the ports.
I have created a demo of all of the above Docker functionality. Take a look!
Thank you for watching! :)
References:













Comments
Post a Comment