Getting started with Docker
Setting up a python environment and launching our first web app in a docker container.
But, what is Docker?
- Docker is an open-source containerization tool that provides PaaS(Platform as a Service).
- Docker enables developers to package applications into small individual containers which use OS-level virtualization.
- A docker container can be imagined as a mini operating system launched using a container image.
- These container images are easy to pull, customize and then save for future use. We can launch any number of containers using them in a matter of seconds, and these containers are easy to ship and deploy.
Installing Docker
note: I’ll be using Redhat Enterprise Linux-8 as the host machine, you’re free to choose whatever you like.
Docker installation in “yum configured” Redhat/Centos/Fedora OS:
- Open your terminal as the root user and type the following commands.
echo "[Docker]" >> etc/yum.repos.d/docker.repo ;
echo "baseurl=https://download.docker.com/linux/centos/7/x86_64/stable/" >> etc/yum.repos.d/docker.repo;
echo "gpgcheck = 0" >> etc/yum.repos.d/docker.repo;
After the successful execution of the above commands your /etc/yum.repos.d/docker.repo file should have the same content as shown in the screenshot.
systemctl stop firewalld
systemctl disable firewalld
Note: You need to stop and/or disable your firewall, as it might create some issues afterward.
yum repolist;
yum install -y docker-ce --nobest
It’ll take some time to download docker according to your internet speed, after the installation you can verify by
docker --version
It’s not enough to just install the docker software, we need to ‘start’ it to use docker.
systemctl start docker
systemctl enable docker
Once the installation is done successfully, we can start working on our projects.
Setting up python environment and installing a web server
But, before we move any further we require an image to work on, you can select an image(operating system) of your choice from here and pull it on your host machine.
docker pull image:tag
Note: I pulled centos image of tag latest(by default tag is latest if no tag is provided)
Now, Once we have an image ready, we are ready to build containers from it.
docker run -i -t --name name_of_the_container image_name:image_tag
The upper command launches a docker container with interactive (-i) terminal (-t) having name (name_of_the_container) from image (image_name) and of (image_tag) version
Wow! Our virtual mini Centos Linux OS is launched in our host, now we can simply install the packages and work on it just like how we work on an actual Centos machine.
Setting up Python Environment:
yum install python3 -y
python3 --version
Since our python environment is set up, we can now execute python programs in our container irrespective of the fact whether our host machine has python installed or not.
Installing WebServer and launching our first web app:
Note: It is always good practice to launch another container for our web app, but since we won’t be doing any python stuff in this container, so I’ll be sticking to this container for our web server too.
yum install httpd -y
Note: For httpd (apache web server) to host our web application, we need the source code of our application to be stored inside the default directory i.e /var/www/html/
In order to store the source code there, you can either copy it from somewhere or write a new code and save it there.
Since I already have a git repository for a sample webpage, I’ll be using that you’re free to choose whichever method you like.
If you’re following me, you can use the source code for the webpage from here.
yum install git -y
git clone https://github.com/pewxh/testpage /var/www/html/
Once your code is stored in the required directory, we can now start the webserver
Note: systemctl start httpd won't work here
/usr/sbin/httpd
Now let's try connecting to 172.17.0.2 from our web browser
We can now confirm our setup was successful.
Congrats on your first docker project, hope you liked it.