Docker is one of the most popular container platforms, letting you manage containerized applications.
Docker provides architecture and capabilities for container automation, security, customization, and enterprise support.
These three components work together to enable you to build, run, and store applications.
Find out how to use this amazing tool by containerizing a sample Django app.
First, learn to install Docker on Ubuntu, macOS, and Windows.
Install Docker on Ubuntu
Start by installing Docker on your preferred operating system.
You canfollow these notes to install Docker on Ubuntuor use the following instructions for macOS or Windows.
Install Docker on macOS
To install Docker on macOS, ensure you have macOS 11 or above.
Create Django App
Create a simple Django App.
you could see the app when you navigate to http://127.0.0.1:8000.
You will containerize this app.
A Docker image is a template of instructions on how to run containers.
You will use aDockerfileto create an image for the app.
TheFROMkeyword identifies the base image you want to build the image with.
The Python image from Docker has the necessary components to execute the Django app.
Docker will use the same image in subsequent builds.
TheWORKDIRkeyword creates a directory inside the container.
The example command identifies the/appdirectory as the root folder.
TheADDcommand adds everything in the current folder into the/appfolder.
TheEXPOSEkeyword exposes a port inside the Docker image to the outside world.
it’s possible for you to view the containerized App on this port using a web client.
TheCOPYkeyword copies content from one folder and places it into another.
In your case, it will copy all contents of the App from the current directory.
The contents will go into theAppfolder in the container.
TheRUNkeyword executes any commands in a new layer of the current image and commits the results.
The next step in the Dockerfile will use the resulting committed image.
TheENTRYPOINTkeyword defines a container as an executable.
In this case, it’s Python3.
you’ve got the option to use it with, or in place of, theCMDkeyword.
ADockerfilemust specify either one or both theCMDorENTRYPOINTkeywords.
Docker defines how the two instructions cooperate.
TheCMDkeyword runs a Linux command when the image starts.
The instruction defines what command runs when you run a container.
Build the Docker Image
Now that theDockerfileis complete, go ahead and build the Docker image.
The layers will be according to the number of instructions given in theDockerfile.
In this case, you will have nine layers.
Nine steps will represent these layers.
execute the Docker Container
Next, you better execute the image in the container.
This is where the app will live.
In this case, you want to use the–nametag to name the containerdocker-djangoapp.
Then trigger the container on port8000:80with-p. Next, specify the image you want to create the container.
This is thedocker-django-app:latestimage you created earlier.
The docker run command creates a container layer over the image.
It then starts it using the specified command.
When you rundocker psIt should appear as follows:
Congratulations!
You have containerized your first App.
Next, you’ve got the option to push the container to the Docker registry for storage.
you might access your system whenever you wish from any machine.
you might also share it with others online.
The registry offers security for images and allows extra privileges on private accounts.
Why Dockerize an App?
More and more developers are using Docker to optimize the building and managing of containers in any virtual environment.
Docker comes with productive features, including a client that issues build commands to Docker daemons.
The daemon builds images and runs containers.
you’ve got the option to build and store the images in the Docker registry.
This ensures the images are secure and available online.
Docker offers already configured official images that have ready-made components.
you’ve got the option to use these images to build containers for your applications quickly.