This exercise does not require installing anything, we will use virtual online environment called PWD ‘Play with Docker’. Play with Docker is an interactive playground that allows you to run Docker commands on a linux terminal, no downloads required.
- To access PWD terminal , Log into https://labs.play-with-docker.com/
- We also need to Push(register) our images to central registry to make it accessible from everywhere. For this you require a repository. This can be created using DockerHub.
- To Navigate to Docker Hub and create a free account login to https://hub.docker.com/signup
1. Create a Python app (without using Docker)
First will create a file named newapp.py
Step1.1 – Copy and paste this entire command into the terminal
echo 'from flask import Flask
newapp = Flask(__name__)
@newapp.route("/")
def hello():
return "hello world!"
if __name__ == "__main__":
newapp.run(host="0.0.0.0")' > newapp.py
Step 1.2– Click on editor to check that newapp.py file has been created
2. Create a Dockerfile
The Dockerfile is used to create reproducible builds for your application. A common workflow is to have your CI/CD automation run docker image build as part of its build process. After images are built, they will be sent to a central registry where they can be accessed by all environments (such as a test environment) that need to run instances of that application.
Later we will push our custom image to the public Docker registry, which is the Docker Hub, where it can be consumed by other developers and operators.
A Dockerfile lists the instructions needed to build a Docker image. Contents of Dockerfile are as follows
FROM python:3.6.1-alpine RUN pip install flask CMD ["python","newapp.py"] COPY newapp.py /newapp.py
Step 2.1 run “vi Dockerfile”, [type this command on terminal] Step 2.2 press “i” to switch to “Insert Mode”, Step 2.3 copy/paste the contents of Dockerfile here Step 2.4 press “Esc” to exit “Insert Mode” Step 2.5 save+exit by typing “😡” Step 2.6 Click on editor to check that Dockerfile file has been created
3 Build the new image
syntaxdocker image build <path>. $ docker image build ./ -t python-hello-world <Path> - refers to the directory containing the Dockerfile.-tparameter to name your image python-hello-world ((here ./t refers to current directory)
4. Verify that image is included in your image list
$ docker image ls
5. Run the Docker image
$ docker run -p 5001:5000 -d python-hello-world
here
-p flag maps a port running inside the container to your host.
5001:5000 map port 5001 of the host to port 5000 in the container (Note that if port 5001 is already being used by another application on your host, you might need to replace 5001 with another value, such as 5002)
-d run the container in detached mode (in the background)
python-hello-world the image to use
Navigate to http://localhost:5001 in a browser to see the results. You should see "hello world!" in your browser.
6. Find the ID for your running container
$ docker container ls [-a]
7. Check the log output of the container.
$ docker container logs [container id]