- Docker Install Redistricting
- Php Docker Install Redis
- Docker Install Redis Server
- Docker Install Redistributable
- Docker Install Linux
As part of adding integration tests to an app on CircleCI I ran into the following issues:
Feb 28, 2020 Updated on February 28th, 2020 in #docker, #flask. Dockerize a Flask, Celery, and Redis Application with Docker Compose Learn how to install and use Docker to run a multi-service Flask, Celery and Redis application in development with Docker Compose. This tutorial shows how to install RedisInsight on Docker so you can use RedisInsight in development. We have a separate guide for installing RedisInsight on AWS. The first step is to install docker for your operating system. Run the docker version command in a terminal window to make sure that docker is installed correctly.
redis-cli
's API has changed from Redis CLI versions 2 to 3 to 4- ie. this works in v4
redis-cli -u ${REDIS_URL}
but doesn't in v2
- ie. this works in v4
- the 'only way' to install
redis-cli
is through aredis-tools
orredis-server
install and I only need the Redis CLI not the server or any other tools.
What follows is how not to install redis-cli
and then how to install redis-cli
latest, properly.
This was sent out on the Code with Hugo newsletter last Monday.Subscribe to get the latest posts right in your inbox (before anyone else).
Bad: install outdated Redis CLI version
This installs an outdated version, 2.8.x
where stable is 4.x.x
.
Better: install latest Redis CLI as part of redis-server
Maybe we don't need the full redis-server
install if we only need the Redis CLI.Sometimes it also installs the old redis-cli
… not the best.
Best: install just Redis CLI with redis-cli binary from tarball
You'll need libjemalloc1 libjemalloc-dev gcc make
most of which should already be installed. We're building from source… which takes about a minute on the CircleCI containers (so I would expect less everywhere else), which is fine.
Credit: DevOps Zone, install redis-cli without installing server. I shamelessly took the snippet from there, because hey, it works.
Installing redis-cli latest on CircleCI
Same as above except:
CircleCI runs the jobs with a non-root user by default, and kudos to them for that, more tools should make you think about what privileges you have.
It does however mean that we need an extra command for the Redis CLI to be runnable.
Installing redis-cli latest on Alpine in Docker
apk
seems to keep a more recent version of Redis CLI in its repositories.
Get The Jest Handbook (100 pages)
Take your JavaScript testing to the next level by learning the ins and outs of Jest, the top JavaScript testing library.
orJoin 1000s of developers learning about Enterprise-grade Node.js & JavaScript
Learn how to install and use Docker to run a multi-service Flask, Celery and Redis application in development with Docker Compose.
Quick Jump: What Is Docker and Why Is It Useful?|Installing Docker|Creating the Flask Application|Dockerize the Flask Application|Run the Flask Application
After this tutorial, you'll understand what the benefits of using Docker are and will be able to:
- Install Docker on all major platforms in 5 minutes or less
- Clone and run an example Flask app that uses Celery and Redis
- Know how to write a Dockerfile
- Run multiple Docker containers with Docker Compose
Also, there's a free email course to learn a bit about Docker at the bottom of this post.
What Is Docker and Why Is It Useful?
Docker allows you to package up an application or service with all of its dependencies into a standardized unit. This unit is typically labeled as a Docker image.
Everything the application needs to run is included. The Docker image contains the code, runtime, system libraries and anything else you would install on a server to make it run if you weren't using Docker.
To get a better idea of how Docker will affect you on a day to day basis as a software developer I highly recommend you read one of my previous blog posts which will save you from years of turmoil by using Docker.
There's also another post I wrote which directly compares setting up a Python development environment with and without Docker. It focuses on developing a web app specifically.
Installing Docker
The code base we'll be working with is compatible with all modern versions of Docker and Docker Compose. Feel free to install the latest stable release of Docker.
This guide expects you to have Docker already installed. If you're at ground 0 then you may want to sign up for the free Docker email course at the bottom of this post, because it covers a myriad of ways to install Docker on Mac, Windows and Linux.
Otherwise, feel free to check out Docker's documentation on installing Docker if you want to get going right now. To help you out, I've also written a comparison guide on Docker for Mac / Windows vs Docker Toolbox.
Ensure Docker and Docker Compose Are Working
Before continuing on you should see what I see, or something very similar:
Creating the Flask Application
We're going to be using the open source version of the application in my Build a SAAS App with Flask course.
The open source version only covers a tiny fraction of what the course covers, but it will be more than enough to exercise how to use Docker in development.
Clone the Project
The reason we're checking out that commit is because I've done many free updates to this course and the current release is way newer than this blog post.
So much has changed since then, and I wanted to make sure you can still follow along in this post. You can always upgrade afterwards.
Open the Project in Your Favorite Code Editor
Feel free to use whatever editor you want, but if you like Sublime Text 3 and you want to configure it for Python, Docker and more then check out my post on 25 Sublime Text 3 Packages for Polyglot Programmers.
Dockerize the Flask Application
There's a few things we need to do to Dockerize the application.
Logging
In order for logs to function properly, Docker expects your application or process to log to STDOUT. Lucky for us, Flask does this by default.
Docker Specific Files
The root of the project has a few files that are related to Docker:
The only file that's necessary to add is the Dockerfile
but you'll find that most web applications that are Docker-enabled will have the others.
Dockerfile
Let's start off with the Dockerfile
because to talk about the other files will require having a little bit of knowledge about how Docker images get built.
You can think of this file as your Docker image blueprint or recipe. When you run the docker build
command it will execute each line from top to bottom.
It's going to run all of these commands in the context of the Docker image.
Docker Install Redistricting
To get a better understanding of this file, then check out my shiny new Dive into Docker course (which is even more up to date than this article).
At this point we could build the image and you'd be able to access the Flask app, but let's avoid doing that for now.
.dockerignore
Let's first look at the next file which is the .dockerignore
file.
When we copied in all of the files from our current directory into the Docker image with the COPY . .
command, it's going to copy literally everything.
That's not the best idea in the world because if your project is a git repo you're going to have a TON of extra data. You should strive to have the smallest Docker images you can within reason.
The .dockerignore
file is very similar to a .gitignore
file. It lets you black list certain folders or files from being included.
In our case, we're ignoring the git folder but we're also excluding the .dockerignore
file itself because it's not part of our Flask application.
docker-compose.yml
Docker Compose is an official tool supplied by Docker. At its core, it's a utility that lets you 'compose' Docker commands and manage multiple containers in an easy way.
Let's take a glance at the docker-compose.yml
file:
Install mysql macos catalina. It looks a lot more complicated than it is. All of those variables wrapped in ${}
are coming in from the .env
file which we'll see below.
Dive Into Docker covers everything in great detail if you want to see how all of this ties together. Often times knowing the 'why' is more important than seeing how it's done. That is what enables you to apply things on your own.
.env
This file isn't technically part of Docker, but it's used by Docker Compose.
By default Docker Compose will look for an .env
file in the same directory as your docker-compose.yml
file.
We can set various environment variables here, and you can even add your custom environment variables here too if your application uses ENV variables.
By setting the COMPOSE_PROJECT_NAME
to snakeeyes
, Docker Compose will automatically prefix our Docker images, containers, volumes and networks with snakeeyes
.
Php Docker Install Redis
In addition to COMPOSE_PROJECT_NAME
you'll notice many other env variables. Each of them are commented, so feel free to check them out on your own.
Run the Flask Application
You can run everything by typing: docker-compose up --build
. Docker Compose has many different sub-commands and flags. You'll definitely want to check them out on your own.
Docker Install Redis Server
After the up
command finishes, open up a new terminal tab and check out what was created on your behalf.
Docker Images
Run docker images
:
Docker Compose automatically pulled down Redis and Python for you, and then built the Flask (web) and Celery (worker) images for you.
Docker Containers
Run docker-compose ps
:
Docker Compose automatically named the containers for you, and it appended a _1
because it's running 1 instance of the Docker image. Docker Compose supports scaling but that goes beyond the scope of this tutorial.
Feb 28, 2020 Updated on February 28th, 2020 in #docker, #flask. Dockerize a Flask, Celery, and Redis Application with Docker Compose Learn how to install and use Docker to run a multi-service Flask, Celery and Redis application in development with Docker Compose. This tutorial shows how to install RedisInsight on Docker so you can use RedisInsight in development. We have a separate guide for installing RedisInsight on AWS. The first step is to install docker for your operating system. Run the docker version command in a terminal window to make sure that docker is installed correctly.
redis-cli
's API has changed from Redis CLI versions 2 to 3 to 4- ie. this works in v4
redis-cli -u ${REDIS_URL}
but doesn't in v2
- ie. this works in v4
- the 'only way' to install
redis-cli
is through aredis-tools
orredis-server
install and I only need the Redis CLI not the server or any other tools.
What follows is how not to install redis-cli
and then how to install redis-cli
latest, properly.
This was sent out on the Code with Hugo newsletter last Monday.Subscribe to get the latest posts right in your inbox (before anyone else).
Bad: install outdated Redis CLI version
This installs an outdated version, 2.8.x
where stable is 4.x.x
.
Better: install latest Redis CLI as part of redis-server
Maybe we don't need the full redis-server
install if we only need the Redis CLI.Sometimes it also installs the old redis-cli
… not the best.
Best: install just Redis CLI with redis-cli binary from tarball
You'll need libjemalloc1 libjemalloc-dev gcc make
most of which should already be installed. We're building from source… which takes about a minute on the CircleCI containers (so I would expect less everywhere else), which is fine.
Credit: DevOps Zone, install redis-cli without installing server. I shamelessly took the snippet from there, because hey, it works.
Installing redis-cli latest on CircleCI
Same as above except:
CircleCI runs the jobs with a non-root user by default, and kudos to them for that, more tools should make you think about what privileges you have.
It does however mean that we need an extra command for the Redis CLI to be runnable.
Installing redis-cli latest on Alpine in Docker
apk
seems to keep a more recent version of Redis CLI in its repositories.
Get The Jest Handbook (100 pages)
Take your JavaScript testing to the next level by learning the ins and outs of Jest, the top JavaScript testing library.
orJoin 1000s of developers learning about Enterprise-grade Node.js & JavaScript
Learn how to install and use Docker to run a multi-service Flask, Celery and Redis application in development with Docker Compose.
Quick Jump: What Is Docker and Why Is It Useful?|Installing Docker|Creating the Flask Application|Dockerize the Flask Application|Run the Flask Application
After this tutorial, you'll understand what the benefits of using Docker are and will be able to:
- Install Docker on all major platforms in 5 minutes or less
- Clone and run an example Flask app that uses Celery and Redis
- Know how to write a Dockerfile
- Run multiple Docker containers with Docker Compose
Also, there's a free email course to learn a bit about Docker at the bottom of this post.
What Is Docker and Why Is It Useful?
Docker allows you to package up an application or service with all of its dependencies into a standardized unit. This unit is typically labeled as a Docker image.
Everything the application needs to run is included. The Docker image contains the code, runtime, system libraries and anything else you would install on a server to make it run if you weren't using Docker.
To get a better idea of how Docker will affect you on a day to day basis as a software developer I highly recommend you read one of my previous blog posts which will save you from years of turmoil by using Docker.
There's also another post I wrote which directly compares setting up a Python development environment with and without Docker. It focuses on developing a web app specifically.
Installing Docker
The code base we'll be working with is compatible with all modern versions of Docker and Docker Compose. Feel free to install the latest stable release of Docker.
This guide expects you to have Docker already installed. If you're at ground 0 then you may want to sign up for the free Docker email course at the bottom of this post, because it covers a myriad of ways to install Docker on Mac, Windows and Linux.
Otherwise, feel free to check out Docker's documentation on installing Docker if you want to get going right now. To help you out, I've also written a comparison guide on Docker for Mac / Windows vs Docker Toolbox.
Ensure Docker and Docker Compose Are Working
Before continuing on you should see what I see, or something very similar:
Creating the Flask Application
We're going to be using the open source version of the application in my Build a SAAS App with Flask course.
The open source version only covers a tiny fraction of what the course covers, but it will be more than enough to exercise how to use Docker in development.
Clone the Project
The reason we're checking out that commit is because I've done many free updates to this course and the current release is way newer than this blog post.
So much has changed since then, and I wanted to make sure you can still follow along in this post. You can always upgrade afterwards.
Open the Project in Your Favorite Code Editor
Feel free to use whatever editor you want, but if you like Sublime Text 3 and you want to configure it for Python, Docker and more then check out my post on 25 Sublime Text 3 Packages for Polyglot Programmers.
Dockerize the Flask Application
There's a few things we need to do to Dockerize the application.
Logging
In order for logs to function properly, Docker expects your application or process to log to STDOUT. Lucky for us, Flask does this by default.
Docker Specific Files
The root of the project has a few files that are related to Docker:
The only file that's necessary to add is the Dockerfile
but you'll find that most web applications that are Docker-enabled will have the others.
Dockerfile
Let's start off with the Dockerfile
because to talk about the other files will require having a little bit of knowledge about how Docker images get built.
You can think of this file as your Docker image blueprint or recipe. When you run the docker build
command it will execute each line from top to bottom.
It's going to run all of these commands in the context of the Docker image.
Docker Install Redistricting
To get a better understanding of this file, then check out my shiny new Dive into Docker course (which is even more up to date than this article).
At this point we could build the image and you'd be able to access the Flask app, but let's avoid doing that for now.
.dockerignore
Let's first look at the next file which is the .dockerignore
file.
When we copied in all of the files from our current directory into the Docker image with the COPY . .
command, it's going to copy literally everything.
That's not the best idea in the world because if your project is a git repo you're going to have a TON of extra data. You should strive to have the smallest Docker images you can within reason.
The .dockerignore
file is very similar to a .gitignore
file. It lets you black list certain folders or files from being included.
In our case, we're ignoring the git folder but we're also excluding the .dockerignore
file itself because it's not part of our Flask application.
docker-compose.yml
Docker Compose is an official tool supplied by Docker. At its core, it's a utility that lets you 'compose' Docker commands and manage multiple containers in an easy way.
Let's take a glance at the docker-compose.yml
file:
Install mysql macos catalina. It looks a lot more complicated than it is. All of those variables wrapped in ${}
are coming in from the .env
file which we'll see below.
Dive Into Docker covers everything in great detail if you want to see how all of this ties together. Often times knowing the 'why' is more important than seeing how it's done. That is what enables you to apply things on your own.
.env
This file isn't technically part of Docker, but it's used by Docker Compose.
By default Docker Compose will look for an .env
file in the same directory as your docker-compose.yml
file.
We can set various environment variables here, and you can even add your custom environment variables here too if your application uses ENV variables.
By setting the COMPOSE_PROJECT_NAME
to snakeeyes
, Docker Compose will automatically prefix our Docker images, containers, volumes and networks with snakeeyes
.
Php Docker Install Redis
In addition to COMPOSE_PROJECT_NAME
you'll notice many other env variables. Each of them are commented, so feel free to check them out on your own.
Run the Flask Application
You can run everything by typing: docker-compose up --build
. Docker Compose has many different sub-commands and flags. You'll definitely want to check them out on your own.
Docker Install Redis Server
After the up
command finishes, open up a new terminal tab and check out what was created on your behalf.
Docker Images
Run docker images
:
Docker Compose automatically pulled down Redis and Python for you, and then built the Flask (web) and Celery (worker) images for you.
Docker Containers
Run docker-compose ps
:
Docker Compose automatically named the containers for you, and it appended a _1
because it's running 1 instance of the Docker image. Docker Compose supports scaling but that goes beyond the scope of this tutorial.
We can also see which ports the services are using. Only the web service has port 8000 published in such a way that you can access it in a browser. The other ports you see for Redis and the worker are acting as documenting for which ports it could technically publish if you wanted to.
There's a lot more to go over but the above is enough to get rolling.
Viewing the Site
If you're using Docker Desktop (Mac / Windows) or native Linux you can visit http://localhost:8000 in your browser and you should see the home page.
At this point you have a Dockerized Flask application running. Congrats!
If you installed Docker through the Docker Toolbox then you'll need to make 1 change to the .env
file. Check out the SERVER_NAME=localhost:8000
value.
You will need to change localhost
to your Docker Machine IP address instead. Chances are that will be 192.168.99.100
but if it's not, you can find your Docker Machine IP by running docker-machine ip
and then visit http://192.168.99.100:8000 in your browser instead of localhost.
By the way, if you're using Docker Toolbox and you tried to submit the contact form and you received a CSRF token error then check out how to fix this problem. Spoiler alert: it's a bug with Chrome.
Docker install linux mint. This example uses the script at get.docker.com to install the latest release of Docker Engine - Community on Linux. To install the latest testing version, use test.docker.com instead. In each of the commands below, replace each occurrence of get with test. Warning: Always examine scripts downloaded from the internet before running them locally.
Shutting Things Down
You'll want to goto your Docker Compose terminal tab and press CTRL+C
. Most of the time that works but sometimes you get an ABORT
error. If that happens you can run docker-compose stop
to stop everything.
Conclusion
Docker is awesome. Now you can run your projects on other platforms without having to worry about dependencies and platform specific gotchas.
Docker Install Redistributable
You can even deploy your projects to production with minimal fuss.
You can learn much more about Flask by checking out the Build a SAAS App with Flask course. I keep the course updated regularly so it's never out of date.
Docker Install Linux
Or, if you're ready to master Docker, then check out the Dive Into Docker course.