Getting your Django app on the internet is - pardon my language - a pain in the ass.
By default the Flask app only listens on localhost, but since it's in a container, we need to listen on all ports (hence the 0.0.0.0). When I did that, assuming I'd exposed the port 5000 in my Dockerfile and forwarded port 5000 in my docker run command, this worked for me. First, install docker using homebrew: $ brew install docker. The next step is installing VirtualBox for Mac OS, because the Docker Container are loaded using a VirtualBox driver. Additionally the Docker App for Mac is necessary (e.g. For accessing “docker ps”) $ brew install cask docker. Start the App after installation and it will appear.
How to install Docker on your Mac 1. Create a Docker Hub account. Docker Hub keeps track of the containers you're running (or creating) and provides a one-stop shop to find new ones. Sign up for a Docker account with a unique Docker ID, your email, and a strong password. Download Docker Desktop for Mac. The first couple of volumes start quickly then it gets slower and slower, and then I get timeouts, etc starting the containers. If I leave docker-sync off (after a Docker restart) then the containers start quite quickly. I am going to refactor my docker-compose to not use docker-sync and see if that fixes the issue for me. Docker Desktop includes a standalone Kubernetes server that runs on your Mac, so that you can test deploying your Docker workloads on Kubernetes. The Kubernetes client command, kubectl, is included and configured to connect to the local Kubernetes server.
You need to daemonize your app with something like Supervisor, then you need to setup a production ready database, then you need to setup a web-server using the likes of Nginx or Apache. And when it's all done, you need to setup files and directories to collect and maintain logs from your app.
You could do all that, or you could use Docker.
Here's what you'll learn
In this tutorial you'll learn to deploy a Django app with Docker and docker-compose. By the end of the tutorial, you'll have a single command that you can use with any Django app to deploy it to a server.
The prerequisites
To complete this tutorial, you will need:
Step 0 — Installing Dependencies
To follow this tutorial, you will need both Docker and Docker-Compose on your machine. Follow the steps below depending on your operating system.
Installing on Ubuntu
You can install Docker Engine on your system by using the convenience script provided by Docker.
To install Docker-Compose, first download the binaries from the Github repo. Then apply executable permissions to the binary.
Installing on a Mac
Docker Desktop for Mac already includes Docker-Compose. To install Docker Desktop on Mac, download the package from here and run the installer.
Note: If the above steps don't work for you, you can go to this link and find more information depending on your operating system.
Step 1 — Setting Up The Django Project
For following along, you can make a sample Django project or follow the steps with your existing project. I suggest making a sample app and then implementing it on your existing project so that you can tweak things to suit your purpose.
Create a new virtual environment.
Install Django and make a new Django project.
![]()
List out the requirements in a
requirements.txt file.
In the
djangoproject/settings.py file, change the ALLOWED_HOSTS to the following:
Doing this allows us to access the Django app from outside the container.
Step 1 — Writing The Dockerfile
The first step to containerising our Django app is making a Dockerfile. In the
djangoproject directory, make a new file named Dockerfile and put the following in it:
This Dockerfile is used to build the container for your Django app. Each line is a step of the build. We will make a lot of changes in this, but for now, it works.
Why you should copy the requirements.txt file before copying the code Step 2 — Writing the Docker-Compose File
To use Docker-Compose, we need to make a
docker-compose.yaml file. In this file, we will define three different containers as services and then connect them with each other. Then we can use a single command to run our containers together. These services are:
We will add each of these services to the
docker-compose.yaml file one by one, starting with the web app. Make a new docker-compose.yaml file in the deployment-project directory with the following contents:
Above, we defined a service for the web app. Let's it breakdown and see what each option does:
Let's test if everything works until now. Run the following command.
You should see the Django app running inside the container. The
--build flag is used to tell Docker to build the container.
Step 3 — Setting Up The Database
Let's set up the database now. We will be using a PostgreSQL database for our app. To set this up, we will have to complete the following steps:
Change the
docker-compose.yaml file to look like the following:
Again, a breakdown of the changes:
Now, lets create the
project.env file we mentioned above. Make a new file called project.env in the deployment-project directory with the following contents. We'll add more to this file later.
Next, we will configure the Django app to connect to the database. To connect to the database, Django needs to have a driver installed. For PostgreSQL, that driver is
psycopg2 .
You should install
psycopg2-binary instead of psycopg2 if you want to install psycopg2 without install PostgreSQL on your system. Now, update the requirements.txt file
Now we will configure the Django settings. First, make a new file called
keyconfig.py in the djangoproject/djangoproject directory. We will save credentials and other sensitive information here. The purpose of using a keyconfig instead of directly loading environment variables in settings.py is that you can separate parts of your config using classes. Add the following contents to the keyconfig.py file:
Open
djangoproject/djangoproject/settings.py and import the keyconfig.py file like this:
Now, set the Secret key:
And the database:
Now, we want to
makemigrations and migrate everytime the container starts up. But the problem is that the database container can take longer to initialise and if we run the above commands before the database is up and running, it can result in an error. So we need a way to make sure the database has started. For this, we use an entrypoint script. In the djangoproject directory, make a new file called entrypoint.sh with the following contents.
The above script first waits for the database to start up. We do this by using
netcat to ping the server in a while loop. Then it makes migrations and performs the migrations. The --noinput flag is used when we are using the command inside a script so that it does not prompt the user for input. Next, we need to make the file executable so that Docker can run it.
Now change the Dockerfile to use this script. Best free email app mac. Add the following lines at the end of the Dockerfile:
We need to install netcat as it is not installed by default. Once again, let's test if everything is running. Go to the
deployment-project directory and spin up the containers.
You can use the
-d flag to make the containers run in the background.
Step 4 — Using Nginx to Serve the Django app
In this step, we will set up another container to use Nginx to serve our django project. For this, we will do the following:
Gunicorn is a production level WSGI server we will use to serve our Django project. To begin let's, install gunicorn.
To use gunicorn as our WSGI server, we use the following command:
In the above command,
djangoproject.wsgi is the name of the WSGI module to use. The --bind takes address of the socker to bind to. In this case, we tell it to bind to port 8000 of all available interfaces. The --workers flag takes the number of workers to be initialized by gunicorn, we set it to 4 here.
Once again, update the
requirements.txt file.
Next, let's make a configuration file for the
nginx server.
Inside the
deployment-project directory, make a directory called nginx .
Open the
nginx.conf file and add the following:
Next, we add the
nginx service to the docker-compose file and also use gunicorn for the Django app. Open the docker-compose.yaml file and change it to the following:
The difference between ports and expose
Again, test that everything is working by spinning up the containers.
Test that you can see the webserver by going to
localhost:1337 in your webbrowser.
Step 5 — The Dockerfile, Revisited
Before configuring the staticfiles, we will take another look at the Dockerfile and restructure it. Previously, we were running the processes inside the Django container as a root user. But, according to the Docker documentation, the best practice is to run your processes as non-privileged user within the containers. So, in this step, we will restructure the Dockerfile and also make a new user to run our app inside the container.
Make a new Dockerfile with the following contents.
Copy and install the requirements.
Copy the code and make the app user the owner of the directory.
Change the user to
app and run the entrypoint.
Test that everything works by spinning up the containers.
Verify that the server is running by going to
localhost:1337 in your browser.
Step 6 — Serving Static Files with Nginx
We need to configure nginx to serve our staticfiles because Django does not serve staticfiles in production. First, we configure Django to use a
staticfiles directory. Open settings.py and add the following in the staticfiles section:
We tell Django that the staticfiles are going to be served at the
/static/ path. And that the files are going to be in the staticfiles directory.
Next, open the
nginx.conf file and add the following:
We tell Nginx to serve the contents of the
staticfiles directory at the /static/ path.
Next, we need to edit the entrypoint script to collect the static files. Add the following at the end of the
entrypoint.sh file.
To let Nginx access the staticfiles collected by Django, we will store them in a volume and add this volume to both the
web and nginx services.
Android sms app mac. And add this volume to the volumes section:
Check that the staticfiles have loaded by spinning up the containers and going to the admin page at
localhost:1337/admin/ you should see the CSS loaded.
Conclusion
If you reached until here, you will have a ready to deploy Django app which will be up and running in just one command. You can use this configuration for your own Django apps and deploy them to a server with only a few extra steps.
The full code for this guide is available on Github here.
Estimated reading time: 4 minutes
Welcome! We are excited that you want to learn Docker.
Docker App Store
This page contains step-by-step instructions on how to get started with Docker. We also recommend the video walkthrough from Dockercon 2020.
The Docker Quickstart training module teaches you how to:
Docker concepts
Docker is a platform for developers and sysadmins to build, run, and shareapplications with containers. The use of containers to deploy applicationsis called containerization. Containers are not new, but their use for easilydeploying applications is.
Containerization is increasingly popular because containers are:
Images and containers
Fundamentally, a container is nothing but a running process,with some added encapsulation features applied to it in order to keep it isolated from the host and from other containers.One of the most important aspects of container isolation is that each container interacts with its own private filesystem; this filesystem is provided by a Docker image.An image includes everything needed to run an application - the code or binary,runtimes, dependencies, and any other filesystem objects required.
Containers and virtual machines
A container runs natively on Linux and shares the kernel of the hostmachine with other containers. It runs a discrete process, taking no more memorythan any other executable, making it lightweight.
By contrast, a virtual machine (VM) runs a full-blown “guest” operatingsystem with virtual access to host resources through a hypervisor. In general,VMs incur a lot of overhead beyond what is being consumed by your application logic.
What Port Is My Docker App On Mac OsSet up your Docker environmentDownload and install Docker Desktop
Docker Desktop is an easy-to-install application for your Mac or Windows environment that enables you to start coding and containerizing in minutes. Docker Desktop includes everything you need to build, run, and share containerized applications right from your machine.
Follow the instructions appropriate for your operating system to download and install Docker Desktop.
Test Docker version
After you’ve successfully installed Docker Desktop, open a terminal and run
docker --version to check the version of Docker installed on your machine.
Test Docker installation
Conclusion
At this point, you’ve installed Docker Desktop on your development machine, and ran a quick test to ensure you are set up to build and run your first containerized application.
What Port Is My Docker App On Mac Computer
For information on how to build and run your first containerized application using Node.js, go to Build your Node.js image.
What Port Is My Docker App On Mac CatalinaCLI referencesWhat Port Is My Docker App On Macbook
Refer to the following topics for further documentation on all CLI commands used in this article:
get started, setup, orientation, quickstart, intro, concepts, containers, docker desktop
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |