Docker: Running Your Python Code on Docker or Apache Server

What Is Docker?

A container provides operating-system-level virtualization. Containers are almost same as VMs. Both isolate an application and its dependencies into a self-contained unit that can run anywhere. They have private space for processing, can execute commands as root, have a private network interface and IP address, allow custom routes and IPtable rules, can mount file systems, and etc.

Installing Docker

Running Your Python Code On Docker

docker pull python:3

~ We can Specify version accordingly

2. Save all Your Python Code in a Separate Folder
3. Run Docker Container For Our Python Code

docker run -i -v /root/app:/code python:3 python3 /code/

~ Here /root/app is the directory with our Python code and we are mounting this Directory in Python container with a directory named code.

~ This Command will Start a Python Interpreter, Run the Code and Exit.

~ Every Required Change will be saved in /root/app folder.

Apache Server On Docker

docker pull httpd

2. Save all your web-app files in a Separate Directory.
3. Start the Docker httpd Container.

docker run -d -v /root/app/:/usr/local/apache2/htdocs/ -p 80:80 httpd

~ Here, our web-app files are in /root/app/ directory and This Directory is mounted with httpd default document root i.e. /usr/local/apache2/htdocs/

~ -p is used to port forward a container’s port(s) to the host.

Done! Your WebApp is Available on Localhost.

Thanks For Reading 🙏

Tech. Explorer