Introduction to Docker container deployment :
In this article we shall explore how we can achieve docker container deployement using series commands and we use a small piece of python code for deployment experiment.
Therefore, we shall start from downloading an ubuntu docker image and configure it. Click the link here to move directly to ubuntu image page.
Configuring Ubuntu docker container deployement:
Downloading ubuntu image from docker hub:
docker pull ubuntu:22.04
Shows the list of docker images:
docker image ls
Runs the docker image with the name ubuntu2024:
docker run -it -d --name ubuntu2204 --privileged -p 52000:22 -p 3307:3307 ubuntu:22.04
The port 52000 of host machine is mapped with 22 port of docker contianer.
Shows the list of containers and its state(runnning/exited):
docker ps -a
Gets into docker container bash shell :
docker exec -it ubuntu2204 /bin/bash
Sets the root password to 123
passwd root
Adding a new user ‘user1’ and set the password to 456
adduser user1
To start docker shell using root user, use either of the two commands given below:
docker exec --it --user root ubuntu2204 /bin/bash docker exec -it -u 0 ubuntu2204 /bin/bash
Installing all the required packages inside the docker container. Here opessh server wil be used for remote connection and vim is an editor that will be used to write Python code.
apt-get update;apt-get upgrade -y;apt-get install openssh-server -y;apt-get install vim -y
The command below will check and start the ssh server.
/etc/init.d/ssh status;/etc/init.d/ssh start;/etc/init.d/ssh start
IP address of a container can be extracted from the command below:
docker inspect ubuntu2204 | grep IPAddress
To get inside docker container use 127.0.0.1 with port 5200 from host machine:
ssh -X user1@127.0.0.1 -p 52000
Ssh to container specific IP address using port 22.
ssh -X user1@172.17.0.2 -p 22
Note: The Container is communicating with the host machine and other containers through the configured port number. Therefore, use port 22 when you use the container’s specific IP address, or use port 52000 when you use localhost.
Always remember ufw must be inactive @ 192.168.1.201
ssh -X user1@192.168.1.201 -p 52000
For Docker container deployment, we have to create a new image from an existing docker contianer. So that we can move the docker image from one machine to another.
docker commit -a "authorname" -m "testmessage" ubuntu2204 pythonprojectimagefromubuntu
To check the existance of the newly created image use :
docker image ls
To make an image transferable, we have to convert it into tar file. Therefore, the command below is used to convert a docker image to tar file.
docker save -o /home/pallab/Documents/myImages/pallabimageubuntu.tar pallabimageubuntu
The following command creates an docker image from the given tar file.
docker load -i pallabimageubuntu.tar
We can also convert a container to a tarballl file for moving to other location.
docker export ubuntu2204 > backUpUbuntu2024.tar
Conclusion:
The steps of Docker container deployment will help use understand the process by with we can, at last move our docker images or containers from one machine to another. The docker container we have used in this article is bare bone Ubuntu but I have published an article about configuring mysql docker container. You can read it here.
Hence, knowing docker images and contianers will give you ample of room to try out different crazy experiments, which otherwise would not have been possible. Similary, I have written an aticle that describes the process of connecting visual studio code with a docker container so that we can take advantages.
Follow the youtube channel for more updates.