Docker has three important components: the Docker client (a command-line tool), Docker Host, and Docker Registry. 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 can follow these notes to install Docker on Ubuntu or use the following instructions for macOS or Windows.
Install Docker on macOS
To install Docker on macOS, ensure you have macOS 11 or above. Then follow these steps:
Download Docker from the Docker hub website. Double-click on Docker. dmg to open the installer. Drag the Docker icon to the Applications folder. To start Docker, double-click on the Docker. app. The Docker whale logo on the status bar indicates that Docker is running.
You can also install Docker on macOS via the command line by following the Docker Mac installation instructions.
Install Docker on Windows
Follow the following steps to install Docker desktop on Windows OS:
Download Docker from the Docker hub website. Double-click on Docker Desktop Installer. exe to run the installer. Enable the Hyper-V Windows Feature on the Configuration page when installing. Follow the process and allow all permissions until the installation completes. After installing, click Close to complete the process. To start the Docker, search and select Docker Desktop in your desktop search results.
Alternatively, you can install Docker on Windows OS using a command line by following the Docker Windows installation instructions.
Create Django App
Create a simple Django App. You can see the app when you navigate to http://127.0.0.1:8000. You will containerize this app.
Create a Dockerfile
To containerize the app, you must create a Docker image to build and run containers. A Docker image is a template of instructions on how to run containers.
You will use a Dockerfile to create an image for the app. Create a file in the root folder called Dockerfile. Then, add the following instructions to the file:
Dockerfiles use unique Docker keywords to specify instructions.
The FROM keyword identifies the base image you want to build the image with. The Python image from Docker has the necessary components to run the Django app. Docker will use the same image in subsequent builds.
The WORKDIR keyword creates a directory inside the container. The example command identifies the /app directory as the root folder.
The ADD command adds everything in the current folder into the /app folder.
The EXPOSE keyword exposes a port inside the Docker image to the outside world. You can view the containerized App on this port using a browser.
The COPY keyword 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 the App folder in the container.
The RUN keyword 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.
The ENTRYPOINT keyword defines a container as an executable. In this case, it’s Python3. You can use it with, or in place of, the CMD keyword. A Dockerfile must specify either one or both the CMD or ENTRYPOINT keywords. Docker defines how the two instructions cooperate.
The CMD keyword 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 the Dockerfile is complete, go ahead and build the Docker image. Run the following command to build the image:
When it starts building, Docker will build the image in layers. The layers will be according to the number of instructions given in the Dockerfile. In this case, you will have nine layers. Nine steps will represent these layers.
A successful build will appear like so:
Now, if you check your docker images, you should see the new image.
Run the Docker Container
Next, you need to run the image in the container. This is where the app will live. Run the following command to run the image:
It should return a serial number for the container like so:
The Docker run command allows options tags for images. In this case, you want to use the –name tag to name the container docker-djangoapp. Then run the container on port 8000:80 with -p. Next, specify the image you want to create the container. This is the docker-django-app:latest image you created earlier.
The docker run command creates a container layer over the image. It then starts it using the specified command. The syntax for running containers is as follows:
Check the Docker containers to see whether the container is running. When you run docker ps It should appear as follows:
Congratulations! You have containerized your first App. Next, you can push the container to the Docker registry for storage. You can access your application whenever you wish from any machine.
You can 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 can 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 can use these images to build containers for your applications quickly.