Hi Habr! For several years I was in readonly mode on the Habrahabr website, occasionally leaving comments on other people's publications, and several weeks before the new year, I wanted to change this state of affairs. This was my recent assignment at work. And when I began to look for information, I realized that, as always, there was a lot of it, it was written in a complicated way, and not what I would like to find. And what if this person is new to this issue? So I decided to try myself in this field. Make a tutorial that would be useful to me first.
Go.
For several years now, Docker has been on my ear, but I have never had a chance to work with him. And just then the management wanted to change its stack. Words like Docker, containers and clouds began to speak. What is not a reason to learn something new? I work as a DevOps radio engineer. My list of technologies is very simple: Octopus Deploy + TeamCity + and a self-written application cart. It works flawlessly.
In this article I want to share information in the form in which I myself personally would like to find it. The goal is to launch Docker on Vagrant, create an image with Django project by default, initialize the container and get to the site from the main OS.
')
At work, I work under Windows. First we need
Chocolatey to install the necessary software.
Install a convenient terminal (cmder), VirtualBox and Vagrant:
C:\> choco install cmder C:\> choco install vagrant C:\> choco install virtualbox
If you do not want to use Chocolatey, you can put everything manually.
Create a folder for our project, and add the Vagrantfile file to it:
Usually, this file can be generated by a single command
vagrant init ubuntu/xenial64
. I added one line for port forwarding between the main and guest OS on the virtual machine. This will be needed later when we want to visit our website running in a container.
Start the virtual machine and connect to the terminal with the following two Vagrant commands:
vagrant up vagrant ssh
We must successfully connect to the virtual machine terminal.
Let's proceed to the Docker installation:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" sudo apt-get update sudo apt-get install -y docker-ce
Let's verify that Docker is installed successfully and the service is running. Run the following command:
sudo systemctl status docker
Create a Dockerfile file with the following content:
FROM python:latest ENV PYTHONUNBUFFERED 1 RUN apt-get update RUN mkdir /code WORKDIR /code RUN pip install django==1.11.8 RUN django-admin startproject mysite RUN python mysite/manage.py migrate RUN echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin@example.com', 'pass')" | python mysite/manage.py shell ADD . /code/ CMD python mysite/manage.py runserver 0.0.0.0:5000
We have created a description of the future image with the default Django application. In this description, we use the Python image, and add our seasonings, namely: installing Django, creating a default project, running scripts to initialize the database, and creating an admin user.
Let's build an image by running:
sudo docker build -t my/app:latest .
This command pulls all the necessary images from the Docker registry, and adds our configuration to the final with the name my / app.
Run the command and you will see your assembled image locally:
sudo docker images
This is just an image as a class that we need to initialize. During initialization, we create a container (object). A container is a running image in Docker.
Create a container based on our image:
sudo docker run -d -p 5000:5000 my/app
Docker will return you the generated unique identifier (guid) of the running container. We have just launched our application in a container. In the team, we told Docker to map the ports inside the container and the external one on the guest OS.
Minimize the remote terminal and launch the browser on the main system. Go to:
http: // localhost: 8080 /You will see the main page of the Django application. You can go to the admin page, and enter the control panel with
admin/pass
data.
Why Vagrant? For experiments, I never install anything on the main system, and work with virtual machines. If something went wrong, you can always delete and start over. The best documentation for me is the code.
Let's go back to the console. To view the logs, run the command:
sudo docker logs <guid > | head
The command will output the console output of the internal server running Django. Everything you see is usually running
python manage.py runserver
locally.
Turn off the container with the following command:
sudo docker stop <guid >
Again we return to our running browser in the main system, and overload the website. It is unavailable.
When the container is stopped, take it as a server off. The state of the container is saved as if you did a hibeirnet on your desktop.
This command will list all initialized containers:
sudo docker ps -a
Let's run our container again:
sudo docker start <guid >
The browser returns to us all the same website. You can log in to the control panel, create a user, and then turn off and turn on the container. You will see that the user is saved and available when the container is started again.
I specifically used the old version of Django to show the next moment. Open the Dockerfile, and edit it. Remove the Django version when we install it using the
pip
command.
Build a new image with the command:
sudo docker build -t my/app:latest .
Note that we did not stop the running container. With the command above, we updated the image that is stored locally.
Let's remove the current container with the command:
sudo docker kill <guid>
And we will launch a new container:
sudo docker run -d -p 5000:5000 my/app
Every time you launch a new container based on the image, Docker generates a unique identifier for it, which you can use with it (turn off, turn on, connect to the terminal, etc.)
That's all, let's clean up after ourselves and remove all unnecessary.
Log out of the SSH session in the terminal by typing:
exit
And you are again in your home directory of the main system. Vagrant is still running, Docker and your container respectively. You can check this again through the browser, to make sure that the site is still running.
To remove a running virtual machine in Vagrant, run:
vagrant halt vagrant destroy
The first command turns off the virtual machine and saves its state, and the second removes the virtual machine from VirtualBox.