Learning and playing with Docker through the browser
Having a Docker environment on your own machine can be somewhat complicated in some situations. One example is for those who use Windows 10 Home, which does not support the typical Docker installation because of a feature (Hyper-V) that is only available in the Pro version of the system. Of course, there are workarounds and alternatives to solve this, but usually at the cost of important resources for those who use more modest machines, in addition to the extra work required to get everything working. And it gets even more complicated when trying to simulate the scenario of multiple nodes (machines) working together...
The Solution
Well, there is a very simple alternative, and it costs nothing, which you can use directly in your browser! It's Play With Docker! An amazing tool sponsored by Docker Inc. itself.
Here's how PWD works:
- You log in.
- You click
Start
. - You are directed to the
Playground
and ready, you can start creating instances (machines) and do whatever you want with them.
Some Features of PWD
- Everything is ready for you to use Docker and Swarm.
- You can create multiple instances and simulate the scenario of working with multiple nodes.
- The instances run a Linux distribution called Alpine, which is well-known in the container world for being lightweight and secure, but it's a bit different from popular ones like Ubuntu, Debian, and Mint... But it's not complicated or otherworldly to use.
- Sessions last 4 hours, meaning that when you create your playground, you have 4 hours with it active, and after that, the instances will be terminated (but you can start another session and continue playing and studying without any problems).
Basic (and Extremely Simplified) Concept of Docker
Docker basically works like this: you create images of your applications, and with these images, you can create containers without worrying about the environment in which they will be running, as the containers operate in isolation and without interference from the machine they are running on, and inside the images will be described everything the containers will need for your applications to work (software versions, languages, packages, etc).
Note: This does not even come close to explaining all the capabilities and advantages of Docker, so I recommend taking a look at other articles and even the Docker documentation to get a better idea of what it can offer.
Starting a Container Using a Ready-Made Image
An image is an application that has been 'containerized' and is ready to become a new container (or several). In this example, I will use an image of Quick Express, which is a boilerplate for REST APIs in Node.js. This image is hosted on Docker Hub, which is a repository maintained by Docker Inc. itself and is free to host public images.
Starting the Container
To run a container using this image, we only need a single command:
docker run -d -p 80:3000 -e "AUTH_SECRET=mySecretKey" --name quick-express josecfreittas/quick-express
Understanding the Command:
docker run
← command to run a container-d
← 'detached' option, which tells not to enter the container's console when it starts-p 80:3000
← 'port' option, which tells Docker to expose port 80 of my instance to what is running on port 3000 of my container-e "AUTH_SECRET=mySecretKey"
← 'env' option, which sets environment variables inside the container (Quick Express needs the AUTH_SECRET variable to work properly)--name quick-express
← option to set the container's name (which can be anything)josecfreittas/quick-express
← the image that will be used for the container. As mentioned earlier, this image is hosted on Docker Hub, and Docker automatically searches and downloads images through Docker Hub
Checking if the Container is Active
You can check the list of running containers using this command:
docker ps
When you run this command, the expected result is that it lists the container we started in the previous step.
Accessing the API
Since a port has been set to expose our container (80), you can already access it through the URL provided by PWD:
And this is the result:
With just one command!