Docker - Getting Started
What is Docker?
Docker allows us to configure environments to be shared on any platform. It is an open source tool used to deploy, package and run applications in what is called containers.
What are Containers?
Containers are heavily based on shared operating systems and generally outperforms hypervisors. Because instead of emulating virtual hardware, containers will house the application in a much smaller package, without all the excessities of a whole virtual machine. Also, when the time comes where the application requires more resources, Docker will create more instances of what it needs in order to provide the necessary resources. Simply put, if you are in need of a light application packaging system and do not have time setting up virtual machines, Docker is for you!
Setup Docker for Windows
In the modern IT world, we've found that more and more companies are now utilizing Docker. One main reason why is because of its simple setup process. Let's begin!
- To get started, head on over to https://hub.docker.com/editions/community/docker-ce-desktop-windows/
- press the Get Docker button on the right hand side of the page.
- Run through the installation process, making sure that Enable Hyper-V Windows Features is enabled.
- Restart your computer.
To check that the installation was successful...
- Open Windows Powershell.
- Type docker -v to check your version of docker.
To run docker...
- Search for and select Docker Desktop in your toolbar. You may need to run it as an administrator. If for any reason you are not able to, try this method: https://avidosguy.wordpress.com/2019/10/10/solved-you-are-not-allowed-to-use-docker-you-must-be-in-the-docker-users-group/.
Docker Run
Now that we have Docker successfully installed in our computer, let's get back to our powershell terminal so we can go over an essential Docker command and its variables.
- docker run <image-name> - This command both creates and starts a container over the image based on the image name that we specified in the required command argument. Docker will automatically detect whether or not the image is stored locally. If Docker cannot locate the image, it will download it from the registry/library.
- docker run --help - Displays the complete list of variables for the command in the terminal.
- docker run -it <container-name> - A combination of variables -i and -t. This command creates an interactive shell and a pseudo terminal and installs the container specified by its name.
And just like that, we are now able to input Ubuntu commands into this toned down lightweight version of Ubuntu!
Docker with Ubuntu
Now that we have gone through the basics of Docker run, let's move on to working more with containers under the Ubuntu image.
- First, we need to keep track of our running containers. Not only will we have a good understanding as to what Docker currently has in its hands, but we can also free up extra resources by stopping the containers that we don't need.
- Type docker ps in the terminal to display a list of running containers(add the -a variable to list all containers).
- Now, let's create a container with the help of the --name variable. This allows us to create a container under a desired name by specifying it after the variable. A good common practice to note in container naming is calling it somewhat in relation to what it does. For the sake of this tutorial, let's name it test. Ex. docker run -it --name test ubuntu.
- Open a new terminal and type docker ps to see that our newly added container is now added to the list of running containers.
- To exit a container, we can either use Ctrl + P + Q or exit. Using the Ctrl + P + Q will return us to where we were before we ran the Ubuntu container with the exception of keeping that container running, which is what typing exit will not do.
- To go back to our test container, type docker attach test. This will only work for containers that are already running. Otherwise, we must start the container by typing docker start test before typing in the attach command.
- And finally, go ahead and type docker rm test. This command will remove a container in Docker. We will then get an error response saying that we cannot remove running containers, which we can resolve by force removing the container using the -f variable before the container name or simply stopping the container by typing docker stop test followed by docker rm test, it all comes down to personal preference.
Conclusion
Docker is a useful modern tool that is free to use for every level of developer. We've covered the basics, delved into its operations and went over how it interacts with other familiar technologies like Ubuntu, which gives us a sneak peak of its potential in what it can do for us. Hopefully this post has answered most of your questions enough to propel you to learn more about Docker, its intricacies, and how it can make your coding life easier!
Comments
Post a Comment