If you'd prefer to watch the youtube video explaining and going over these commands, please click here.
What Is Docker?
Have you ever tried to use someone else's code/project, only to find that something fails because you don't have a particular library set up on your environment?
Docker is a lightweight container that lets someone set up an environment that's suited for their project with all the dependencies required. Then, anyone who uses that container has the environment set up so that they don't have to do anything else. In more concrete terms, we basically create a tiny virtual machine with low resources, it runs an operating system, we can install the required project dependencies/libraries, and whoever uses our project runs the code on top of that docker container. Installing Docker
Let's install the latest edition of Docker on our computer. For the purposes of this tutorial, we're going to be assuming that we're going to be using Ubuntu linux; if you're using a different version of Linux or Mac, then change commands 2 and 3 to the appropriate urls. You can find the appropriate download link here. (Shout out to Digital Ocean for having an awesome article teaching about these commands.)
Run all of the following commands in the terminal.
sudo apt-get install curl
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
apt-cache policy docker-ce
sudo apt-get install -y docker-ce
We install curl, and get the latest version of docker available for Ubuntu (the OS we're using). We update the package, make sure that the version we downloaded is the one we're using (as opposed to the version that comes by default with our Ubuntu), and finally we install it. To make sure the installation was correct, let's run a simple Hello World docker command.
sudo docker run hello-world
You'll know if it works or not. And with that, we've installed docker on our computer! Using Docker
Okay so we have docker on our computer. How do we actually use it?
Well, the way docker works is we create our own image of our machine state. Basically, we need to pick an OS, and then we can install some dependencies on it to set it up the way we'd like. (And then, we can create or get a project onto the machine.) Let's start by finding an image. In the terminal, type:
sudo docker search ubuntu
This searches for all ubuntu docker images available online. The result will be something like: The image named "ubuntu" has a lot of stars, so let's download that one. Let's download that image:
sudo docker pull ubuntu
Now we have that ubuntu image downloaded onto our computer. Let's look at all the images we have available locally, by typing the following command:
sudo docker images
This will show the images that we have on our computer. We can see that we have the hello-world image from before, and the ubuntu image that we just pulled. Let's run the ubuntu image. Use the following command.
sudo docker run -it ubuntu
This will take us into the terminal for the ubuntu image. We're basically in a virtual machine now! Now you have a docker image that's running ubuntu, but you can install any dependencies you need too (node, python, etc.) and if you want, you can even create/pull any project that you want to run on that virtual machine. Take a note of the container id while you're in the image.
(Pro-tip: No need to use sudo inside the docker image terminal, since you're already the root user.)
As you can see in the image above, to leave the docker image and go back to your own terminal, just type exit inside of the image terminal:
exit
Okay, so we were inside of our image and set it up the way we like. Why don't we save the state of this docker container as a separate image. Use the following command:
sudo docker commit -m "nothing" -a "srcmake" 4b1c0c8872cc nodejsubuntu
We commit our image, specifying the commit message "nothing" (which isn't descriptive...), saying that the author is "srcmake", giving the container id (see above for getting this), and the specific name of the new image we're creating. If we run the "docker images" command once again, we can see our newly created image on our list of local images. To view the number of containers that we have running, use this command:
sudo docker ps -a
It'll show our containers, as seen in the following pictures. To stop a container from running, use this command:
sudo docker stop {containerid}
And with that, we know how to use Docker! How To Save and Share Your Docker Image
Okay, so we can make a super cool container that runs our project. How do we give this docker image to other people so that they can use it?
First, we'll save our docker image to a file. Use the following command:
sudo docker image save -o myimage dcbe1d3b9b6e
We save the image whose id is dcbe1d3b9b6e to a file named "myimage".
There isn't some fancy "it worked!" message, but the file shows up in our directory meaning it's a success. And that's it! Give that image file to whoever you want too, and they have your docker image. They can load your docker image using the following command:
sudo docker image load -i myimage
You'll see a tiny message for the loaded image. And with two simple commands, we can share images. When To Use Docker
When should we use Docker? Basically, any time that a project's dependencies are a little special. In a professional setting, it's good to have an entire project not just be about the code, but also having the dependencies accounted for by having a docker to run the code.
To watch the youtube video that goes over these commands, please click the video embedded below:
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake Comments are closed.
|
AuthorHi, I'm srcmake. I play video games and develop software. Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.
|