Docker in 5 Minutes
What is Docker?
- Docker is an open platform for developing, shipping, and running applications.
- It enables us to separate applications from the infrastructure so anyone can deliver software quickly.
Why Docker?
In a nutshell: Write Code Once -> Create Docker Container -> Run Anywhere
- Anyone write code locally and share their work with anyone using Docker containers.
- They can use Docker to push their applications into a test environment and can execute automated or manual tests.
- When testing is complete, getting the fix (For e.g. to the customer) is as simple as pushing the updated image to the production environment.
How Docker Works ?
- Docker provides the ability to package and run an application in a loosely isolated environment called a container.
- The isolation and security allow us to run many containers simultaneously on a single host.
Sounds Interesting but how does it differ from Virtual Machines ?
Some advantages of Docker Containers over Virtual Machines
- Fast Execution Speed
- Low RAM and Drive memory usage
- Very Fast Startup Time (generally in milliseconds 🙂
Dockerfile, Image & Container
- A Docker Image is basically a set of rules or template for creating an environment
- We can create docker images using Dockerfile
- Dockerfile is basically a text file containing a set of instructions for creating the Image
- A Docker Container is basically a running instance of an Image
Enough Talk… Lets Create some Conatiners
Python Hello World inside a Docker Container
- For this I have created a python file ‘main.py’ which contain a print statement.
- Now lets go to Docker Hub and search for python.
- Under Simple Tags section, we can see different types of versions & flavours of python image available. We will we using 3.8-alpine since alpine’s images are very less in size.
Step 1. Lets create a Dockerfile
- I have created a simple file named Dockerfile, with the following content
- WORKDIR is basically our working directory inside the image. It is similar to cd(change directory command) in Linux / Windows.
- COPY command is use to copy files/folders inside the image
- CMD is used to run any command in the container’s terminal.
Step 2. Building the Image
The command to build the above dockerfile is
docker build -t python_hello .
- -t : specifies the name of image (python_hello)
- ‘.’ : is the location of dockerfile
Step 3. Lets Run the Container
The Command is
docker run python_hello
Here is the output we got. The container starts, executed our command and exited.
Some More Examples of Dockerfile
- Creating an Apache HTTP Server
- Dockerfile (Expose 80 tell the container to listen on port 80 (default HTTP port))
- Build using docker build -t apache-httpd
- Run using docker run -p 8080:80 apache-httpd
- -p 8080:80 forwards the 80 port inside the container to 8080 port on Host machine
- Result
Congratulation on sucessfully building your docker container & Thanks for sticking till the end.
Please let me know about your views or queries in the comment section.
Image Source
Docker in 5 Minutes was originally published in The Startup on Medium, where people are continuing the conversation by highlighting and responding to this story.
Responses