** Docker ** is a technology that makes it easy to create, deploy, and execute applications using containers.
Docker is a technology that makes it easy to create, deploy, and run applications using containers. Containers allow developers to package all the components they need for their application and later ship them as packages. It also allows you to run more applications on the same server.
With Docker, applications running on a container are isolated from each other, providing a higher level of security. In addition, Docker guarantees that each container has its own resources, so your application will only use the allocated resources.
Before you start this guide, you need the following:
--Alibaba Cloud ECS Linux instance. If you haven't set up your Linux instance yet, this article I will introduce various setup methods in.
Log in to the server using the ssh command.
$ ssh [email protected]
Update the Ubuntu package.
$ sudo apt-get update
Install the latest version of Docker with the following command.
$ sudo apt-get install docker
To verify that Docker is installed correctly, run the following command.
$ sudo docker run hello-world
If done correctly, the above command should allow the instance to download the test image and run it inside the container.
In the Alibaba Cloud ECS (https://www.alibabacloud.com/en/product/ecs) instance, you can use the image to create an ECS cluster with the same configuration. Similarly, the Docker container has an image. Conceptually, the two are very similar. It is based on the official Docker documentation.
A container image is a lightweight, standalone, executable package of software that contains everything you need to run it: code, runtimes, system tools, system libraries, settings.
You can see the running container by running $ sudo docker ps
.
An image, on the other hand, is an inactive, immutable file, such as a snapshot of a container. The image is created with the build command, and when started with the run command, it creates a container.
Images can be viewed by running $ sudo docker images
.
First, let's install Django and create a Django application.
$ sudo pip install django==1.9
$ django-admin startproject djangoapp
Create a requirements file in the djangoapp directory to define the dependencies your application needs.
$ cd djangoapp
$ nano requirements.txt
Add the following dependency.
#requirements.txt
Django==1.9
gunicorn==19.6.0
Docker has the ability to read instructions from Docker files and build images automatically. The Docker file contains all the commands and instructions that Docker uses to build the image.
Let's define some basic commands used in the Dockerfile.
--FROM --Initializes a new build stage and sets the base image for subsequent instructions. Therefore, a valid Dockerfile must start with a FROM instruction. --RUN --Executes the specified command. --ADD --Copy the file to the container. --EXPOSE --Notifies Docker at run time that the container is listening on the specified network port. --CMD --Provides defaults for running containers. Now let's create a file called Dockerfile.
$ nano Dockerfile
First, let's define all the necessary properties in the Dockerfile. Defines the base image and maintainer name.
# base image
FROM python:2.7
# File Author / Maintainer
MAINTAINER Esther
Then copy the application folder inside the container and define the directory where CMD will be executed.
# Copy the application folder inside the container
ADD . /usr/src/app
# set the default directory where CMD will execute
WORKDIR /usr/src/app
Finally, set the default command and execute it.
CMD exec gunicorn djangoapp.wsgi:application --bind 0.0.0.0:8000 --workers 3
The final Dockerfile should look like this:
# set the base image
FROM python:2.7
# File Author / Maintainer
MAINTAINER Esther
#add project files to the usr/src/app folder
ADD . /usr/src/app
#set directoty where CMD will execute
WORKDIR /usr/src/app
COPY requirements.txt ./
# Get pip to download and install requirements:
RUN pip install --no-cache-dir -r requirements.txt
# Expose ports
EXPOSE 8000
# default command to execute
CMD exec gunicorn djangoapp.wsgi:application --bind 0.0.0.0:8000 --workers 3
Run the following command to build the docker image.
$ sudo docker build -t django_application_image .
Sending build context to Docker daemon 12.8kB
Step 1/7 : FROM python:2.7
---> 2863c80c418c
Step 2/7 : ADD . /usr/src/app
---> 09b03ff8466e
Step 3/7 : WORKDIR /usr/src/app
Removing intermediate container a71a3bf6af90
---> 3186c92adc85
Step 4/7 : COPY requirements.txt ./
---> 701c0be5e039
Step 5/7 : RUN pip install --no-cache-dir -r requirements.txt
---> Running in ed034f98db74
Collecting Django==1.9 (from -r requirements.txt (line 1))
Downloading Django-1.9-py2.py3-none-any.whl (6.6MB)
Collecting gunicorn==19.6.0 (from -r requirements.txt (line 2))
Downloading gunicorn-19.6.0-py2.py3-none-any.whl (114kB)
Installing collected packages: Django, gunicorn
Successfully installed Django-1.9 gunicorn-19.6.0
Removing intermediate container ed034f98db74
---> 1ffd08204a07
Step 6/7 : EXPOSE 8000
---> Running in 987b48e1a4ef
Removing intermediate container 987b48e1a4ef
---> ef889d6e8fcb
Step 7/7 : CMD exec gunicorn djangoapp.wsgi:application --bind 0.0.0.0:8000 --workers 3
---> Running in 4d929e361d0f
Removing intermediate container 4d929e361d0f
---> c6baca437c64
Successfully built c6baca437c64
Successfully tagged django_application_image:latest
The built image is in the machine's local Docker image registry. To see the images, run $ sudo docker images
.
REPOSITORY TAG IMAGE ID CREATED SIZE
django_application_image latest c6baca437c64 34 minutes ago 702MB
$ sudo docker run -p 8000:8000 -i -t django_application_image
[2018-03-25 12:29:08 +0000] [1] [INFO] Starting gunicorn 19.6.0
[2018-03-25 12:29:08 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000
[2018-03-25 12:29:08 +0000] [1] [INFO] Using worker: sync
[2018-03-25 12:29:08 +0000] [8] [INFO] Booting worker with pid: 8
[2018-03-25 12:29:08 +0000] [9] [INFO] Booting worker with pid: 9
[2018-03-25 12:29:08 +0000] [10] [INFO] Booting worker with pid: 10
A message appears at http://0.0.0.0:8000 stating that ** gunicorn ** is servicing the app. You should be taken to your server's IP (ip_address: 8000) and you should see the Django welcome page.
To see a running container
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
100695b41a0a django_application_image "/bin/sh -c 'exec gu…" 13 seconds ago Exited (0) 4 seconds ago hopeful_easley
When using Docker, you sometimes run into some problems. The first thing to do when an error occurs is to check the Docker log files.
Docker and other containers are powerful alternatives to traditional virtual machines for application development. For more information on how to run containers on Alibaba Cloud, please visit the Container Services page (https://www.alibabacloud.com/en/product/container-service).
Recommended Posts