If you have been following along with me for the past few weeks, you will know I have recently begun my search for my first opportunity as a Full Stack Engineer. As part of that journey, I have been chatting with a ton of different people at different stages of their development journey to get advice and perspective. A little while ago I spoke with an Engineer at a large US company. He told me about his journey to get to where he is today, and I asked him what I could do as a recent bootcamp graduate to set myself apart for the crowd. His advice, and something that I heard from many people, was to continue learning new skills and building things with them. He then told me that something that would impress him coming from a new graduate was a familiarity with a technology called Docker.
I had heard of Docker before and was vaguely familiar with what it did, but wasn't too sure how it related to Web Development. In many of the job postings I was looking at as well, I had seen Docker listed in the "great to have" sections. I decided it was definitely worth a shot to get more familiar with it, and I hope you'll accompany me as I tell you about that journey and explain a bit about what I learned. Fair warning: I still have a VERY basic understanding of the technology and what it is capable of. Feel free to comment below with feedback if there is anything inaccurate about my explanations or even if you have some great tips or next steps you think I should check out.
So the first thing I needed to understand was what Docker actually is and what it is used for. I checked out Wikipedia and the Docker website to see if I could get a high level overview before diving into some practical examples.
Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers. All containers are run by a single operating system kernel and therefore use fewer resources than virtual machines. The software that hosts the containers is called Docker Engine. It was first started in 2013 and is developed by Docker, Inc.
Ok, that's definitely a lot of information there. Basically what I could glean from it though is that Docker is a service that takes software that I'm writing and packages it up into these things called containers that are similar to but less resource intensive than a virtual machine. My next step was to get a little more information as to what these containers were and why they would be helpful to me as a web developer.
So a container is similar to a virtual machine, but what makes them different or preferable? From what I could glean, a virtual machine uses a whole lot of resources. The virtual machine runs an entire guest operating system on virtual hardware which is powered by the host's OS. This is great if you need an isolated environment to run applications in, but I was beginning to see how clunky, slow, and resource intensive it would be to have to boot an entire operating system every time I wanted to launch my single web application.
Let's look at what the containers do. They create an isolated virtual environment for your application similar to VMs, but instead of running on it's own OS it runs this environment on top of the host's existing OS. So with containers, you get the isolation benefits of a VM without the massive resource cost.
The benefits to this are multiple. YOU have control over building the container, which allows you to include only what is needed to run the application - a great thing for efficiency and controlling resources. Also since it is a self contained environment, if it works in your container - it works everywhere. No need to worry about dependencies and such, as you include all of those things in the container. Finally, the application will perform the same way every time - as the environment is consistent. No need to worry that your host machine is affecting you application or vice versa. So let's take a look at how you actually get started using Docker.
Getting Docker set up and running on your machine is actually really simple. The Docker website provides a wealth of resources on how to get started. Just sign up for an account on Docker Hub, download Docker Desktop, and follow the installer to get everything going. You may encounter some hiccups during the install if your OS is not up to date, but updating and trying again should fix most issues. I am working on Windows and had to update both my OS and WSL version, but everything went smoothly after that.
After everything is installed properly pull up your terminal and run the following command:
docker run hello-world
You should be greeted with some text confirming that your installation is working properly. If all of that works for you, you're all set up and ready to start playing with Docker.
At this point, you can jump right to adding Docker to a project, or playing around with the many existing containers on Docker Hub. Running existing containers is a great way to get familiar with the docker commands, and I'll link the tutorial I used at the end of this article if you'd like to play around with it yourself. Personally, I played around a bit with an existing container before experimenting with creating my own with a sample SPA.
In this last little bit, I'm going to talk a bit about the Dockerfile and how you use it to create your own container for your own project. Keep in mind that this was my first experimentation with Docker, so the example tutorial that I used is just a basic static site.
The Dockerfile is just a simple text file. It contains a list of commands that you want to be called when the Docker client creates your container image. The commands you will use, are very similar to their equivalent Linux commands, so if you have experience with that already, you're in luck! You can check out the Dockerfile reference documentation here if you're interested. For this article though, I'm going to show the simple file that I used for the tutorial below and then explain what it is doing.
# base image FROM python:3 # set a directory for the app WORKDIR /usr/src/app # copy all files to container COPY . . #install dependencies RUN pip install --no-cache-dir -r requirements.txt EXPOSE 5000 CMD ["python", "./app.py"]
The first line of the file has the command
FROM python:3. Fortunately, Docker has a ton of official and unofficial images already built for us that we can use as the base of our own image. This frees us up from having to build an image for the language or framework we're using, and allows us to customize an existing one however we may need. In this instance we are telling docker to use the existing
python image to build ours on top of.
The next two commands are
WORKDIR /usr/src/app and
COPY . . Here we are defining a working directory, and then copying all of our application files into it. Pretty self explanatory.
Next we will want to install the dependencies for our application. We are doing that here with
RUN pip install --no-cache-dir -r requirements.txt. You can imagine if you were working with a different language or framework you can just substitute it's specific run command after the Docker
RUN command above.
EXPOSE keyword nest specifies the port to be exposed that the app runs on.
Lastly we use the
CMD command is where you tell Docker what command should be run when the container is started. This will obviously depend on the languages and frameworks for your individual application.
With all of this set up, all that is left to do is build your image by running the build command in the terminal.
docker build -t <yourusername>/<imagename> .
Now any changes that you make to the application won't be reflected, you will have to rebuild the image if you want them include. However, there are many other build options that include temporarily mounting the image, which will allow your changes to be reflected instantly. I recommend taking a look at some more comprehensive guides if you want to learn more.
If you made it to the end, thank you so much for taking this journey with me. Even though I barely scratched the surface, I had a great time learning a new technology and I can't wait to learn more and start using Docker in my own personal projects. I hope you found this helpful in getting a broad overview of Docker and if you'd like to learn more, check out some of the resources I used below. Also feel free to follow me elsewhere on Twitter, Github, or LinkedIn. Happy Coding!