Introduction to Docker

This blogpost will be an introduction to Docker. What is it? What is its purpose? How do you set it up? How do you use it? And, what is compose and how does it relate to Docker?

What is it?

Docker is a desktop application used to create containers. Containers are isolated development environments that the application you’re developing depends on in order to run as expected. What if you aren’t the only one developing an application. Let’s say the application was developed based on a Node version of 18, but your peer developer has a Node version of 16 and that will break the application when it runs. With a container, that won’t necessarily be a problem as long as the Node version in the container is still 18. So, they can still have Node version 16 on their PC while having Node version 18 in the container. The example given was for Node, but it could just as well have been any other dependency you could think of. Also, because the environment is already specified in each container, it’s also saving you time by not requiring you to change whatver your current development setup is.

Installing it

If you’d like to install Docker, simply go here and follow their instructions. They’re already pretty straightforward, so it would be redundant for me to tell you how to do it:

https://docs.docker.com/compose/gettingstarted/

Using it

If the project you want to use Docker with hasn’t already completed the Docker setup as outlined in the Getting Started page from Docker, then complete that first. Once that is complete, navigate to your project directory in your terminal. You can access your terminal in a number of ways such as via Powershell (Windows OS), within Visual Studio Code (VS Code), or Git Bash. Whichever way you choose, if you’re using Windows like I am, right click whichever application you’ll be accessing your terminal from and choose “Run as Administrator” otherwise some permissions that one lacks as a non-admin will prevent you from running some docker commands. If you happen to be using VS Code, here are 2 ways to access your terminal: #1 Hold ctrl and press the tilde symbol (~) which is to the left of the 1 key above the letters of the keyboard or #2 At the top of the VS Code Window towards the left side, click on Terminal (may have to click on the … to reveal it), and then select New Terminal.

After opening your new terminal window, assuming you opened your desired project in VS Code, you shouldn’t have to really navigate to it. It should navigate to it automatically when you open your terminal. Before running ANY docker commands, make sure that your desktop Docker application is running. Once you’ve confirmed that, then enter “docker compose up” without the quotes (from now on, whenever I put something in quotes, always assume it shouldn’t be run or displayed with quotes unless I specify otherwise). When you run docker compose up, its creating your containers, setting up your application’s configuration, OS, assets, and database system if your container has one.

When you run docker compose up, you’ll notice that in the very left hand column of your terminal, there’s some text of different colors between blue, yellow and green.

That’s because these reference the different containers that currently exist. This is a good time to talk about what exactly compose does.

What is Compose?

In the root of your project’s file structure, navigate to your docker-compose.yml file.

If you focus on looking for “container_name”, you’ll see the same names pop up.

So, that text with the different colors is the name of the various containers that currently exist (in the future, you can just use “docker ps” to see what are the current containers currently in existence). And then, if you look for “build” and then “dockerfile” within that in the docker-compose.yml file, you will see the dockerfile that each of those containers pulls from. That is, what commands are specified to run for those respective containers. And, that is what compose is doing. It’s used to specify what services to run so that someone else can run and build the application with the same environment. If you’re curious about how to create a docker-compose.yml file, this is a start: https://docs.docker.com/get-started/08_using_compose/