🔥Your First Docker Container¶
In this project, we will containerize and run an application using Docker. The main goal of using Docker is to make an app portable and reproducible.
🎯 What you'll learn¶
- Writing a Dockerfile
- Creating your first Docker image
🛑 Prerequisites¶
- Docker installed on your machine.
- Python and pip installed. Don't worry, you don't need to know how to code in Python.
Introduction¶
We will use a simple Python application as the base project and containerize it using Docker. This application gets a meme from Reddit and displays it as shown below.
Initial Setup¶
Download the repo with our code and switch to hello-devops-project/code/meme-generator/
Run Python application locally¶
Before we jump into containerizing the app, let's see how we can run it locally on your machine. This will help us understand the required steps to run a python app.
The following commands, install required packages and starts the application.
Navigate to http://localhost:5000 on your browser to see the app exposed on port 5000
.
Create the Dockerfile¶
A Dockerfile contains instructions to containerize an application. In this case, we will write the instructions to build and containerize our python app.
To do this, we will follow these steps
- Get a Docker image with python preinstalled
- Copy the code from the directory into the Docker image.
- Install required packages using pip
- Give the command that's needs to be run when the container is created. Here we are running
main.py
file.
This is the result of the previous steps.
Dockerfile | |
---|---|
Build the Docker image¶
A Docker image is a binary package with your app, dependencies and startup command baked into it.
Anyone with the Docker image can run your application without any issues.
Run the following command to build the docker image
-
What are these arguments for?
-t
- Used to tag the image, in this casev1
. You can create multiple versions of the same image with different tags..
- The period specifies that the Dockerfile in the current directory be used to build the image. You can also provide a path to the Dockerfile if its in a different location.
Run the container¶
The image we created is stored on our system. Let's run the image and create your first container
-
What is this argument for?
-p
- Lets you specifyPortToBeExposed:ContainerPort
, in this case5000:5000
.If you use
docker run -p 6000:5000 python-app:v1
, you will be able to access the application using http://localhost:6000
Navigate to http://localhost:5000 to see the running app.
Note
The application ends as soon as you use CTRL+C
or close your terminal. If you want the application on the background use docker run -d -p 5000:5000 python-app:v1
.
-d
- This argument lets you run your container in the background.
Voilà!!🎉 There we have your first Docker container. Well, done.👏
❓3 Questions to check your Docker understanding¶
- If Dockerfile is a cake recipe, the image is a _____.
- In the command
docker run -p 8000:5000
, 8000 refers to _____ and 5000 refers to _____. - The point of containerizing an app is _____.
🥷3 Steps to master Dockerfile¶
- Redo the exercise.
- Deploy a similar app using this guide from Docker docs.
- Containerize this application.