In this post i will show you how to build your first application using docker, without docker if you need to programming using language first you should install that language on your PC and test it on your development environment and for sure the production should be ready to sync and test your code again on it seems a lot of work.😥
But now with docker you just pull/grab that image, no installation needed, and run your code on that image.🎉
But how we can control what happening inside the environment, like Accessing to resources like networking interfaces and disk drives is virtualized inside this environment which is isolated from the rest of your system all of this happening by something called Dockerfile.
The Following example taken from Docker Documentation:
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
As you see from the above example the code explained in the comment part, which is done by Python programming language, the above docker file create directory, copy ,paste and check the port then run the app.py.
app.py (very simple Code )
# Use an official Python runtime as a parent image
print("Goodbye, World!")
Now you have the dockerfile under the directory and the app.py file, then run the build command. This creates a Docker image,
docker build -t test .
Check by
$ docker image ls
Run the app, mapping your machine’s port 4000 to the container’s published port 80 using -p:
docker run -p 4000:80 test
Once the above command will be run the log will indicates that you could test your code using this link http://localhost:80 but this is only from inside docker, so in case you need to test it outside the docker the port will be http://localhost:4000
Cheers 👌
Osama Mustafa