18.104.22.168 Deploying MySQL on Windows and Other Non-Linux Platforms with Docker The Docker deployment framework supports easy installation and configuration of MySQL Server. This section explains how to use a MySQL Server Docker image. Get started with Docker Compose. Estimated reading time: 11 minutes. On this page you build a simple Python web application running on Docker Compose. The application uses the Flask framework and maintains a hit counter in Redis. While the sample uses Python, the concepts demonstrated here should be understandable even if you’re not familiar. Aug 28, 2020 To get started developing apps using Docker with WSL 2, we recommend using VS Code, along with the Remote-WSL extension and Docker extension. Install the VS Code Remote-WSL extension. This extension enables you to open your Linux project running on WSL in VS Code (no need to worry about pathing issues, binary compatibility, or other cross-OS. Docker also provides a convenience script at test.docker.com to install pre-releases of Docker on Linux. This script is equivalent to the script at get.docker.com, but configures your package manager to enable the “test” channel from our package repository, which includes both stable and pre-releases (beta versions, release-candidates) of. May 21, 2015 On Linux Mint docker-ce is not availible: sudo apt-get install docker-ce Reading package lists. Done Building dependency tree Reading state information. Done Package docker-ce is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source.
Estimated reading time: 11 minutes
On this page you build a simple Python web application running on DockerCompose. The application uses the Flask framework and maintains a hit counter inRedis. While the sample uses Python, the concepts demonstrated here should beunderstandable even if you’re not familiar with it.
Make sure you have already installed both Docker Engineand Docker Compose. You don’t need to install Python or Redis, asboth are provided by Docker images.
Define the application dependencies.
Create a directory for the project:
Create a file called
app.py in your project directory and paste this in:
In this example,
redis is the hostname of the redis container on theapplication’s network. We use the default port for Redis,
Handling transient errors
Note the way the
get_hit_countfunction is written. This basic retryloop lets us attempt our request multiple times if the redis service isnot available. This is useful at startup while the application comesonline, but also makes our application more resilient if the Redisservice needs to be restarted anytime during the app’s lifetime. In acluster, this also helps handling momentary connection drops betweennodes.
Create another file called
requirements.txt in your project directory andpaste this in:
In this step, you write a Dockerfile that builds a Docker image. The imagecontains all the dependencies the Python application requires, including Pythonitself.
In your project directory, create a file named
Dockerfile and paste thefollowing:
This tells Docker to:
requirements.txtand install the Python dependencies.
.in the project to the workdir
.in the image.
For more information on how to write Dockerfiles, see theDocker user guideand the Dockerfile reference.
Create a file called
docker-compose.yml in your project directory and pastethe following:
This Compose file defines two services:
web service uses an image that’s built from the
Dockerfile in the current directory.It then binds the container and the host machine to the exposed port,
5000. This example service uses the default port for the Flask web server,
redis service uses a public Redis image pulled from the Docker Hub registry.
From your project directory, start up your application by running
Compose pulls a Redis image, builds an image for your code, and starts theservices you defined. In this case, the code is statically copied into the image at build time.
Enter http://localhost:5000/ in a browser to see the application running.
If you’re using Docker natively on Linux, Docker Desktop for Mac, or Docker Desktop forWindows, then the web app should now be listening on port 5000 on yourDocker daemon host. Point your web browser to http://localhost:5000 tofind the
Hello World message. If this doesn’t resolve, you can also tryhttp://127.0.0.1:5000.
If you’re using Docker Machine on a Mac or Windows, use
docker-machine ipMACHINE_VM to get the IP address of your Docker host. Then, open
http://MACHINE_VM_IP:5000 in a browser.
You should see a message in your browser saying:
Refresh the page.
The number should increment.
Switch to another terminal window, and type
docker image ls to list local images.
Listing images at this point should return
You can inspect images with
docker inspect <tag or id>.
Stop the application, either by running
docker-compose downfrom within your project directory in the second terminal, or byhitting CTRL+C in the original terminal where you started the app.
docker-compose.yml in your project directory to add abind mount for the
volumes key mounts the project directory (current directory) on thehost to
/code inside the container, allowing you to modify the code on thefly, without having to rebuild the image. The
environment key sets the
FLASK_ENV environment variable, which tells
flask run to run in developmentmode and reload the code on change. This mode should only be used in development.
From your project directory, type
docker-compose up to build the app with the updated Compose file, and run it.
Hello World message in a web browser again, and refresh to see thecount increment.
Shared folders, volumes, and bind mounts
If your project is outside of the
Users directory (
cd ~), then youneed to share the drive or location of the Dockerfile and volume you are using.If you get runtime errors indicating an application file is not found, a volumemount is denied, or a service cannot start, try enabling file or drive sharing.Volume mounting requires shared drives for projects that live outside of
C:Users (Windows) or
/Users (Mac), and is required for any project onDocker Desktop for Windows that uses Linux containers.For more information, see File sharing on Dockerfor Mac, and the general examples on how toManage data in containers.
If you are using Oracle VirtualBox on an older Windows OS, you might encounter an issue with shared folders as described in this VB troubleticket. Newer Windows systems meet therequirements for Docker Desktop for Windows and do notneed VirtualBox.
Because the application code is now mounted into the container using a volume,you can make changes to its code and see the changes instantly, without havingto rebuild the image.
Change the greeting in
app.py and save it. For example, change the
Hello World!message to
Hello from Docker!:
Refresh the app in your browser. The greeting should be updated, and thecounter should still be incrementing.
If you want to run your services in the background, you can pass the
-d flag(for “detached” mode) to
docker-compose up and use
docker-compose ps tosee what is currently running:
docker-compose run command allows you to run one-off commands for yourservices. For example, to see what environment variables are available to the
docker-compose --help to see other available commands. You can also install command completion for the bash and zsh shell, which also shows you available commands.
If you started Compose with
docker-compose up -d, stopyour services once you’ve finished with them:
You can bring everything down, removing the containers entirely, with the
--volumes to also remove the data volume used by the Rediscontainer:
At this point, you have seen the basics of how Compose works.
This step-by-step guide will help you get started developing with remote containers by setting up Docker Desktop for Windows with WSL 2 (Windows Subsystem for Linux, version 2).
Docker Desktop for Windows provides a development environment for building, shipping, and running dockerized apps. By enabling the WSL 2 based engine, you can run both Linux and Windows containers in Docker Desktop on the same machine. (Docker Desktop is free for personal use and small businesses, for info on Pro, Team, or Business pricing, see the Docker site FAQs).
Docker is a tool used to create, deploy, and run applications using containers. Containers enable developers to package an app with all of the parts it needs (libraries, frameworks, dependencies, etc) and ship it all out as one package. Using a container ensures that the app will run the same regardless of any customized settings or previously installed libraries on the computer running it that could differ from the machine that was used to write and test the app's code. This permits developers to focus on writing code without worrying about the system that code will be run on.
Docker containers are similar to virtual machines, but don't create an entire virtual operating system. Instead, Docker enables the app to use the same Linux kernel as the system that it's running on. This allows the app package to only require parts not already on the host computer, reducing the package size and improving performance.
Continuous availability, using Docker containers with tools like Kubernetes, is another reason for the popularity of containers. This enables multiple versions of your app container to be created at different times. Rather than needing to take down an entire system for updates or maintenance, each container (and it's specific microservices) can be replaced on the fly. You can prepare a new container with all of your updates, set up the container for production, and just point to the new container once it's ready. You can also archive different versions of your app using containers and keep them running as a safety fallback if needed.
To learn more, checkout the Introduction to Docker containers on Microsoft Learn.
WSL can run distributions in both WSL version 1 or WSL 2 mode. You can check this by opening PowerShell and entering:
wsl -l -v. Ensure that the your distribution is set to use WSL 2 by entering:
wsl --set-version <distro> 2. Replace
<distro> with the distro name (e.g. Ubuntu 18.04).
In WSL version 1, due to fundamental differences between Windows and Linux, the Docker Engine couldn't run directly inside WSL, so the Docker team developed an alternative solution using Hyper-V VMs and LinuxKit. However, since WSL 2 now runs on a Linux kernel with full system call capacity, Docker can fully run in WSL 2. This means that Linux containers can run natively without emulation, resulting in better performance and interoperability between your Windows and Linux tools.
With the WSL 2 backend supported in Docker Desktop for Windows, you can work in a Linux-based development environment and build Linux-based containers, while using Visual Studio Code for code editing and debugging, and running your container in the Microsoft Edge browser on Windows.
To install Docker (after already installing WSL):
Download Docker Desktop and follow the installation instructions.
Once installed, start Docker Desktop from the Windows Start menu, then select the Docker icon from the hidden icons menu of your taskbar. Right-click the icon to display the Docker commands menu and select 'Settings'.
Ensure that 'Use the WSL 2 based engine' is checked in Settings > General.
Select from your installed WSL 2 distributions which you want to enable Docker integration on by going to: Settings > Resources > WSL Integration.
To confirm that Docker has been installed, open a WSL distribution (e.g. Ubuntu) and display the version and build number by entering:
Test that your installation works correctly by running a simple built-in Docker image using:
docker run hello-world
Here are a few helpful Docker commands to know:
docker <COMMAND> --help
docker image ls --all
docker container ls --allor
docker ps -a(without the -a show all flag, only running containers will be displayed)
To get started developing apps using Docker with WSL 2, we recommend using VS Code, along with the Remote-WSL extension and Docker extension.
Install the VS Code Remote-WSL extension. This extension enables you to open your Linux project running on WSL in VS Code (no need to worry about pathing issues, binary compatibility, or other cross-OS challenges).
Install the VS code Remote-Containers extension. This extension enables you to open your project folder or repo inside of a container, taking advantage of Visual Studio Code's full feature set to do your development work within the container.
Install the VS Code Docker extension. This extension adds the functionality to build, manage, and deploy containerized applications from inside VS Code. (You need the Remote-Container extension to actually use the container as your dev environment.)
Let's use Docker to create a development container for an existing app project.
For this example, I'll use the source code from my Hello World tutorial for Django in the Python development environment set up docs. You can skip this step if you prefer to use your own project source code. To download my HelloWorld-Django web app from GitHub, open a WSL terminal (Ubuntu for example) and enter:
git clone https://github.com/mattwojo/helloworld-django.git
Always store your code in the same file system that you're using tools in. This will result in faster file access performance. In this example, we are using a Linux distro (Ubuntu) and want to store our project files on the WSL file system
wsl. Storing project files on the Windows file system would significantly slow things down when using Linux tools in WSL to access those files.
From your WSL terminal, change directories to the source code folder for this project:
Open the project in VS Code running on the local Remote-WSL extension server by entering:
Confirm that you are connected to your WSL Linux distro by checking the green remote indicator in the bottom-left corner of your VS Code instance.
From the VS Code command pallette (Ctrl + Shift + P), enter: Remote-Containers: Open Folder in Container... If this command doesn't display as you begin to type it, check to ensure that you've installed the Remote Container extension linked above.
Select the project folder that you wish to containerize. In my case, this is
A list of container definitions will appear, since there is no DevContainer configuration in the project folder (repo) yet. The list of container configuration definitions that appears is filtered based on your project type. For my Django project, I'll select Python 3.
A new instance of VS Code will open, begin building our new image, and once the build completed, will start our container. You will see that a new
.devcontainer folder has appeared with container configuration information inside a
To confirm that your project is still connected to both WSL and within a container, open the VS Code integrated terminal (Ctrl + Shift + ~). Check the operating system by entering:
uname and the Python version with:
python3 --version. You can see that the uname came back as 'Linux', so you are still connected to the WSL 2 engine, and Python version number will be based on the container config that may differ from the Python version installed on your WSL distribution.
To run and debug your app inside of the container using Visual Studio Code, first open the Run menu (Ctrl+Shift+D or select the tab on the far left menu bar). Then select Run and Debug to select a debug configuration and choose the configuration that best suites your project (in my example, this will be 'Django'). This will create a
launch.json file in the
.vscode folder of your project with instructions on how to run your app.
From inside VS Code, select Run > Start debugging (or just press the F5 key). This will open a terminal inside VS Code and you should see a result saying something like: 'Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.' Hold down the Control key and select the address displayed to open your app in your default web browser and see your project running inside of its container.
You have now successfully configured a remote development container using Docker Desktop, powered by the WSL 2 backend, that you can code in, build, run, deploy, or debug using VS Code!
If you were using an early Tech Preview of Docker for WSL, you may have a Docker context called 'wsl' that is now deprecated and no longer used. You can check with the command:
docker context ls. You can remove this 'wsl' context to avoid errors with the command:
docker context rm wsl as you want to use the default context for both Windows and WSL2.
Possible errors you might encounter with this deprecated wsl context include:
docker wsl open //./pipe/docker_wsl: The system cannot find the file specified. or
error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_wsl/v1.40/images/json?all=1: open //./pipe/docker_wsl: The system cannot find the file specified.
For more on this issue, see How to set up Docker within Windows System for Linux (WSL2) on Windows 10.
Docker creates two distro folders to store data:
You can find these folders by opening your WSL Linux distribution and entering:
explorer.exe . to view the folder in Windows File Explorer. Enter:
wsl<distro name>mntwsl replacing
<distro name> with the name of your distribution (ie. Ubuntu-20.04) to see these folders.
Find more on locating docker storage locations in WSL, see this issue from the WSL repo or this StackOverlow post.
For more help with general troubleshooting issues in WSL, see the Troubleshooting doc.