We need to deploy an application similar to CMS in the Intranet environment, including CRUD of some forms, data export, personnel permission management and other functions. I decided to use Django as the basis for development because Django is good at doing this kind of work and doesn’t require much development. The development function is relatively simple, almost is to use plug-ins such as Xadmin to achieve the above functions. However, there is a problem that we can’t get around, that is to deploy to an Intranet environment, tools such as PIP cannot be used on the Intranet, but fortunately there is a YUM server on the Intranet that can be used, so we decided to install Docker on the Intranet server, and then copy the container of the development environment to the production environment for deployment. Here are the main steps:

  1. Install the docker-CE for the development environment
  2. Install docker-compose for your development environment
  3. Configuring the Development Environment
  4. Save the container
  5. Install docker-CE and Docker-compose for production
  6. Send the container file and run it

Note: my development environment here is Ubuntu18.04 and my production environment is Centos7.2. If you are in another environment check for discrepancies and use commands that are appropriate for your system.

Install the docker-CE for the development environment

Docker and Docker-compose are the focus of this deployment. I will try to minimize the Django application. Docker is responsible for the bottom part of container virtualization, docker-compose is a container orchestration tool, with which we can realize the connection between containers without handwritten shell. We first install Docker-CE. Here we mainly refer to the official Docker document. If MY writing is not detailed enough or is out of date, you can go to the official to view the more authoritative and updated document.

Uninstall the previous version

You need to uninstall the old version of Docker before installing it. If you are new, you can skip this step.

$ sudo apt remove docker docker-engine docker.io containerd runc
Copy the code

Install apt repository for use

  1. Update apt package index
$ sudo apt update
Copy the code
  1. Allow APT to access the repository via HTTPS
$ sudo apt install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
Copy the code
  1. Added Docker’s official GPG key
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Copy the code
  1. Add Docker warehouse
$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
Copy the code

Install the Docker – ce

It’s easy to install Docker-CE once you’re ready, but if you’re familiar with Ubuntu, it won’t take long to install.

$ sudo apt update
$ sudo apt install -y docker-ce
Copy the code

Once the installation is complete, start the Docker service and enable it to start every time the system boots.

$ sudo systemctl start docker
$ sudo systemctl enable docker
Copy the code

Install docker-compose for your development environment

Docker-compose is now ready for docker-CE installation. Docker-compose can be used if you download docker-compose’s compiled binaries directly on a Linux or other platform.

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Copy the code

After downloading, modify permission to add executable

$ sudo chmod +x /usr/local/bin/docker-compose
Copy the code

Check docker-compose’s version number to see if the docker-compose installation is successful

$docker-compose --version docker-compose version 1.24.0-rc1, build 0f3d4ddaCopy the code

Configuring the Development Environment

The development environment here is Django, and the demo project I’ll try to use a new Django project for the sake of the demonstration.

Creating a New Django Project

To create a Django project, start by creating an upper folder to put the project files in. The directory structure is roughly as follows:

--project
  --Dockerfile
  --docker-compose.yml
  --mysite
    --manage.py
    --requirements.txt
Copy the code

Start by creating the project folder

$ mkdir project
Copy the code

Then create a new Django project, or you can copy an existing one.

$ django-admin.py startproject mysite
Copy the code

Generate the requirements.txt file

In the previous step, we had a Django project called mysite. Suppose we put requirements.txt in this folder, which looks something like this:

$ cat requirements.txt
Copy the code
Defusedxml ==0.5.0 diff-match-patch==20181111 Django==2.1.7 Django-Crispy-forms ==1.7.2 Django-formTools ==2.1 Django-import-export ==1.2.0 Django-reversion == 3.0.3et-xmlfile ==1.0.1 future==0.15.2 httplib2==0.9.2 jdcal==1.4 Odfpy ==1.4.0 OpenPyXL ==2.6.0 PyTZ ==2018.9 PyYAML==3.13 six==1.10.0 tablib==0.12.1 unicodecSV ==0.14.1 xadmin==0.6.1 XLRD = = 1.2.0 XLWT = = 1.3.0 mysqlclient = = 1.4.2Copy the code

Of course, this is the dependency that my project needs, and yours may be different from mine.

New Dockerfile

The next step is to create a docker image of your Django project’s runtime environment. First, create a Dockerfile to build the image. Create a new Dockerfile in the project folder with the following contents:

$ cat Dockerfile
Copy the code
FROM python:3.6.8 ENV PYTHONUNBUFFERED 1 RUN mkdir /config ADD /mysite/requirements. TXT /config/ RUN PIP install -r /config/requirements.txt RUN mkdir /src WORKDIR /src/mysiteCopy the code

Let me explain this file very briefly

The FROM python: 3.6.8Copy the code

The base image I’m using here is Python: 3.6.8, and the base image is Ubuntu, which I’m familiar with, but you can use Alpine if you’re familiar with Alpine, which is much smaller.

ENV PYTHONUNBUFFERED 1
Copy the code

You can create any operating system environment variable using the Env keyword

ENV PYTHONUNBUFFERED 1
Copy the code

For example, if you use it to store your Django keys, you can write:

ENV DJANGO_SECRET_KEY l! fafmjcqyn+j+zz1@2@wt$o8w8k(_dhgub%41l#k3zi2m-b%m
Copy the code

Use this in your code:

import os
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
Copy the code

RUN, as the name implies, runs the command inside the container, which creates two folders, /config and/SRC, as well as Python dependencies.

RUN mkdir /config
RUN mkdir /src
RUN pip install -r /config/requirements.txt
Copy the code

ADD

ADD /mysite/requirements.txt /config/
Copy the code

Add a local file to the WORKDIR container

WORKDIR /src/mysite
Copy the code

Is the default path to specify all subsequent commands to run in the container, which can be seen later in the docker-compose file.

Create a new docker-compose script

Docker-compose is composed for handling multiple containers. Docker-compose is composed for handling multiple containers manually with a large number of parameters. My docker-comemage. yml content is roughly as follows:

$ cat docker-compose.yml
Copy the code
version: '3'Services: db: image: mysql:5.7 container_name: mysite_db ports: -"3306:3306"
      environment:
        MYSQL_ROOT_PASSWORD: mysite
        MYSQL_DATABASE: mysite
        LANG: C.UTF-8
      command: ['mysqld'.'--character-set-server=utf8mb4'.'--collation-server=utf8mb4_unicode_ci']
    web:
      build: .
      container_name: mysite_web
      command: bash -c "Python Manage.py Makemigbar && Python manage.py Migrate && Python manage.py runServer 0.0.0.0:8000"
      depends_on:
        - db
      volumes:
        - ./mysite:/src
      restart: always
      ports:
        - "8002:8000"
Copy the code

A quick explanation of the docker-compose file.

version: '3'
Copy the code

Docker-compose is a version of Docker-compose that supports slightly different configuration items. Services manages services, in our case two services: DB and Web. The configuration items in the two services are explained separately by DB:

MySQL5.7 container_name specifies the name of the container. Ports specifies the port mapping of the container to the host. The container port environment specifies the environment in which the service is currently running. For details about the environment, refer to the description of the current image. In this example we specify the root password for MySQL, the default database, and the character set of the database. Mysql > alter table mysql > alter table mysql > alter table mysql > alter table mysql > alter table mysql > alter table mysql Dockerfile command (Dockerfile command) depends_on Service (Dockerfile command) command (Dockerfile command) service (Dockerfile command) Restart Specifies the restart policy of the container. In this case, if an error occurs, the container will continue to restart. Port 8000 of the container is mapped to port 8002 of the host, from which the Web service is accessed.

Configuring the Django Project

Now modify the Settings. Py file of the mysite project for the current container environment.

$ vim mysite/mysite/settings.py
Copy the code

Go to the ALLOW_HOSTS section of the file and add “web” to it as follows:

ALLOW_HOSTS = [
    ...
    'web'
]
Copy the code

Modify Settings. Py to modify DATABASES to MySQL server DB.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql'.'NAME': 'mysite'.'USER': 'root'.'PASSWORD': 'mysite'.'HOST': 'db'}}Copy the code

The MySQL connection parameters are defined in the environment of the DB section of the docker-comemage. yml file. Docker-compose can ping each other with the service name, just like using a domain name. Docker-compose can ping each other with the service name, just like using a domain name.

Build the project using Docker-compose

After the above efforts, we are basically ready, we can construct our image, there are two services, db just need to download or use the local image at run time, the Web also need to use Dockerfile to build.

$ docker-compose build
Copy the code

After a few downloads or builds, you can see the message that the image was successfully built.

Run the project and test it

Once the build is complete, we have an image of the Web service, and we now use Docker-compose to start the service.

$ docker-compose up -d
Copy the code

This process may also take a while, depending on your network speed, to download the MySQL image, construct the container from the DB and Web images, and run the container. When you’re done, you can use Docker-compose PS and Docker-compose images to view our generated containers and images

$ docker-compose ps
Copy the code
Name Command State Ports --------------------------------------------------------------------------------------- Mysite_db docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/ TCP, 33060/ TCP mysite_web bash -c python manage.py m... The Up 0.0.0.0:8002 - > 8000 / TCPCopy the code
$ docker-compose images
Copy the code
The Container Repository Tag Image Id Size -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - mysite_db mysql 5.7 e47e309f72c8 355 MB mysite_web mysite_web latest 3989acbcc3c9 938 MBCopy the code

Docker-compose can also be used to stop and start the service. For more details, please refer to the official documentation.

$ docker-compose start
Starting db  ... done
Starting web ... done
Copy the code
$ docker-compose stop
Stopping mysite_web ... done
Stopping mysite_db  ... done
Copy the code

You can see that there is a regular sequence of services stopping and starting in which the dependent service is started first and then the dependent service is started, and the reverse is true for the value service. After the service is running properly, you can access the browser to test whether the service is running properly.

Save the container

If all is well with the service, we will save the current container in preparation for deployment to the new platform. Note: Save is used to save the image, which includes the connection state between containers and other information. Docker-compose cannot be used to restore the service if the image is exported to the production environment using export.

$docker save -o mysql.tar mysql:5.7 $docker save -o mysite.tar mysite_web:latestCopy the code

After the above command is executed successfully, two tar files will be generated in the current directory, and Dockerfile and docker-comemage. yml files in the project directory will be put together for migration to the production machine.

Install docker-CE and Docker-compose for production

Since the production environment is CentOS, you can directly use yum installation

$ sudo yum install docker-ce
Copy the code

After the installation is successful, docker-compose is deployed to the production server by referring to the development environment.

Send the container file and run it

Use SCP or other tools to send the mysql.tar, mysite.tar, docker-comemage. yml and project folders to the production server and find a suitable folder to store these files, keeping the original directory structure. Let’s start by restoring two images to the production server

$ docker load -i mysql.tar
$ docker load -i mysite_web.tar
Copy the code

After a while, you can see that the current server already has both images.

REPOSITORY TAG IMAGE ID CREATED SIZE mysite_web latest 3989ACbCC3C9 2 days ago 983MB mysql 5.7e47e309f72C8 3 weeks ago 372MBCopy the code

We also need to make a simple change to docker-comemage.yml before we start building the container. As you will notice, the production server does not have the Internet, so we cannot build the image any more, and we have also copied the image of the development environment, so this time the Web service will run from the image, which is roughly as follows:

version: '3'
services:
    db:
      ...
    web:
      image: mysite_web:latest
      ...
Copy the code

Just change the build TAB in the Web and delete it and add an image TAB, which is the image we copied from it. Later we can build the container and start the service.

$ docker-compose up -d
Copy the code

The results of

Name Command State Ports ---------------------------------------------------------------------------------------- mysite_web bash -c python manage.py m ... Up 0.0.0.0:8002->8000/ TCP mysite_db docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/ TCP, 33060/ TCPCopy the code

Open your browser again and see if it starts properly.

Afterword.

Docker-compose has many more uses, which I will cover in more depth in a future project. Thank you all for watching my work, I hope you can help you a little.

Reference documentation

  • Get Docker CE for Ubuntu
  • Install Docker Compose
  • How to change the default character set of mysql using docker-compose