
{"id":4833,"date":"2023-01-23T16:56:29","date_gmt":"2023-01-23T21:56:29","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=4833"},"modified":"2023-01-23T16:56:29","modified_gmt":"2023-01-23T21:56:29","slug":"setting-up-django-in-a-docker-container-on-windows-10","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=4833","title":{"rendered":"Setting up Django in a Docker Container on Windows 10"},"content":{"rendered":"<p>Setting up Django in a docker container proved to be harder than I expected, so I am recording the steps here.<\/p>\n<p>Oerview:<\/p>\n<ol>\n<li>Get Docker Desktop<\/li>\n<li>Get Ubuntu 20.04 on Windows Subsystem for Linux (WSL)<\/li>\n<li>Install Django<\/li>\n<li>Create Django project<\/li>\n<li>Install Docker inside Ubuntu<\/li>\n<li>Create Docker project<\/li>\n<li>Run Docker container<\/li>\n<\/ol>\n<p><strong>Get Docker Desktop<\/strong><\/p>\n<p>Install docker desktop from <a href=\"https:\/\/docs.docker.com\/desktop\/install\/windows-install\/\">Docker desktop web site<\/a>.<\/p>\n<p><strong>Get Ubuntu 20.04 on WSL<\/strong><\/p>\n<ol>\n<li>Open command prompt.<\/li>\n<li>Run <code>wsl --install -d Ubuntu-20.04<\/code>. If you did not have any Linuxes installed, restart (it&#8217;s still Windows), the installation will continue after restart.<\/li>\n<li>Run <code>wsl -l -v<\/code> to ensure you have Ubuntu 20.04 and WSL 2.<\/li>\n<li>If you have more than one Linux installed, run <code>wsl --set-default Ubuntu-20.04<\/code><\/li>\n<li>Run <code>bash<\/code>.<\/li>\n<li>Run <code>lsb_release -a<\/code> to check Ubuntu version.<\/li>\n<\/ol>\n<p><strong>Install Python and Django<\/strong><\/p>\n<p>In bash run the following:<\/p>\n<pre>python3 --version                  # ensure it's at least Python 3.8\r\nsudo apt update                    # get available apt package versions\r\nsudo apt -y install python3.8-venv # you don't want python3-venv, it leads to <a href=\"https:\/\/askubuntu.com\/questions\/1268833\/error-command-path-to-env-bin-python3-7-im-ensurepip-upgrade\">trouble<\/a>\r\nsudo apt -y install python3-pip    # install PIP\r\nmkdir myproj &amp;&amp; cd mproj           # create project dir\r\npython -m venv .venv               # create a virtual environment\r\n. .venv\/bin\/activate               # enter the virtual environment\r\npip install django                 # install Django into the venv<\/pre>\n<p><strong>Create Django Project<\/strong><\/p>\n<p>Run in bash:<\/p>\n<pre>django-admin startproject django_web . # create Django project named django_web in the current dir\r\npython3 manage.py migrate              # prepare the project\r\npython3 manage.py runserver            # run server locally<\/pre>\n<p>Open localhost:8000\/ in the browser and make sure Django start page appears.<\/p>\n<p>Go back to bash and press Ctrl+C to stop Django server.<\/p>\n<p><strong>Install Docker inside Ubuntu<\/strong><\/p>\n<p><code>sudo apt -y install docker.io docker-compose<\/code><\/p>\n<p>NOTE: I was not able to make Docker daemon run inside WSL. Instead, docker process running inside Ubuntu connects to the Docker Deskop running on the host Windows OS. It is still needed to install the docker packages inside Ubuntu.<\/p>\n<p><strong>Create docker project<\/strong><\/p>\n<p>Run in bash, while still in the venv:<\/p>\n<pre>pip freeze &gt; requirements.txt\r\ndeactivate<\/pre>\n<p>Create file named <code>Dockerfile<\/code> with the following contents:<\/p>\n<pre># Pull base image\r\nFROM ubuntu:20.04\r\n\r\n# Set environment variables\r\nENV PIP_DISABLE_PIP_VERSION_CHECK 1\r\nENV PYTHONDONTWRITEBYTECODE 1\r\nENV PYTHONUNBUFFERED 1\r\n\r\nRUN apt update\r\nRUN apt -y install software-properties-common\r\nRUN add-apt-repository ppa:deadsnakes\/ppa -y\r\nRUN apt update\r\nRUN apt -y install python3.8 python3-pip \r\n\r\n# Set work directory\r\nWORKDIR \/code\r\n\r\n# Install dependencies\r\nCOPY .\/requirements.txt .\r\nRUN pip install -r requirements.txt\r\n\r\n# Copy project\r\nCOPY . .\r\n<\/pre>\n<p>Create file named <code>docker-compose.yml<\/code> with the following contents:<\/p>\n<pre>version: \"3.\"\r\nservices:\r\n  web:\r\n    build: .\r\n    ports:\r\n      - \"8000:8000\"\r\n    command: python3 manage.py runserver 0.0.0.0:8000\r\n    volumes:\r\n      - .:\/code\r\n<\/pre>\n<p>Create file named <code>.dockerignore<\/code> with the folliwing contents:<\/p>\n<pre>.venv\r\n.git\r\n.gitignore\r\n<\/pre>\n<p><strong>Run Docker container<\/strong><\/p>\n<p><code>sudo docker-compose up<\/code><\/p>\n<p>This should build the image and start the container. You may see Windows Defender warning about Docker trying to listen on port 8000. After the container has started, open the browser again on <code>http:\/\/localhost:8000\/<\/code>, and you should see Django hello window.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Setting up Django in a docker container proved to be harder than I expected, so I am recording the steps here. Oerview: Get Docker Desktop Get Ubuntu 20.04 on Windows <a href=\"https:\/\/ikriv.com\/blog\/?p=4833\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[1],"tags":[],"class_list":["entry","author-ikriv","post-4833","post","type-post","status-publish","format-standard","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4833"}],"version-history":[{"count":3,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4833\/revisions"}],"predecessor-version":[{"id":4836,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4833\/revisions\/4836"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}