Using Dockerfile to setup environment and docker-compose to automatically configure application services.
In Part 1 of this series, we had built Automatic Tweet Liker by integrating Twitter API and Django. In this part, we will automate the build and deployment of our project using Docker. With a single click, we will be able to launch our whole project using docker-compose.
Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could execute on the command line to assemble an image. Dockerfile should be named as “Dockerfile” without any extension. This Dockerfile should exist in the project folder i.e. AutoTweetLiker/.
FROM python:3.7-alpineENV PYTHONONBUFFERED 1RUN mkdir /codeWORKDIR /codeCOPY . /code/RUN pip install -r requirements.txt
We are using a python image with version 3.7-alpine. Alpine is a light-weight python image. once, python image is downloaded, this will create a directory with the name “code” inside the docker container. After that, it will change the working directory to /code inside a docker container and copies all the code from the current directory to the /code/ directory inside a docker container. Then it will install all of the required packages from requirements.txt file. Now, our image is built and ready to launch.
Compose is a tool for defining and running multi-container Docker applications. With Compose, we use a YML/YAML file to configure our application’s services. Then, with a single command, we create and start all the services from your configuration. Docker-compose file should be named as “docker-compose” with a YML/YAML extension. This docker-compose.yml file should exist in the project folder i.e. AutoTweetLiker/.
version: '3'services: web: build: . # Exporting CONSUMER KEY and CONSUMER SECRET key from tweeter api as an environment variable command: sh -c "export CONS_KEY="" && export CONS_SECRET="" && python manage.py runserver 0.0.0.0:8000" volumes: - .:/code ports: - "8000:8000"
This file uses compose file version 3. Here, we need to export the consumer key and consumer secret key obtained from twitter API as an environment variable. This can be used while authenticating users. Refer this to know how you can find a consumer key and consumer secret key from twitter API.
Getting the project source code:
You will get the project source code from the docker branch from https://github.com/cankush625/AutoTweetLiker.git OR clone the repository by using the following command.
git clone -b docker https://github.com/cankush625/AutoTweetLiker.git
Building and Launching the project:
Open the terminal in the directory where our Dockerfile and docker-compose.yml files exist. Run the following command to build the docker image for the project.
# docker-compose build
Now, we are all set to launch our site. With a single click or command, we can launch our site. Run the following command to launch our app.
# docker-compose up
Now, our site is up and running at http://127.0.0.1:8000/
To stop the running containers run the following command
# docker-compose stop
We can start these containers again by using
# docker-compose start
To stop and remove the running containers run the following command
# docker-compose down
I hope you had got an idea of how powerful the Docker and docker-compose is, that we can launch the whole site with a single command.
Thanks a lot for reading my blog! If you liked this article, please applaud it. You can also follow me on Twitter at @cankush625 or find me on LinkedIn. I’d love to hear from you if I can help you with Django, Python, and Docker.