In this lesson, you will learn how to start basic containers and how to use them in your day to day developer life. You will also learn how to start a database container that can be used with the demo application for this course.
Instructor: [0:00] Before you spin up your first container, let's take a quick look at what a container is. In this course, I will be using Docker for most of my container management. Docker is one of the tools that can be used to run container. I usually like to use Podman as my container engine, but it's only available for Linux right now.
[0:21] Docker comes with a virtual machine that will let you run containers in any operating system. It's interesting to note that they are both 100 percent equivalent, and each command I will be showing here will also work on Podman, should you decide to use that alternative instead.
[0:40] What is a container, exactly? You can think of a container as a way to package up all of an application, its source code, its configuration, and everything else it needs to run into one single package. Imagine a big Zip file that would contain your source code or executables along with all the applications required to run it.
[1:05] This way, instead of shipping your source code, you're shipping your source code along with a web server pre-configured and ready to use. Let's start your first container.
[1:17] Head to your terminal window. From here, you can do a docker run, hello-world. Docker will now pull the image from its registry and run this image. You should see a welcome message from Docker. This image contains an operating system and contains a txt file. Once the container was started, it outputted the content of that file.
[1:45] That's it! You've just run your first container. Congratulations. To get a better feel of what just happened, let's try another example. First, let's output some content into a file. Using your editor or with an echo statement, you can create a file called "hello.txt." This file will have a simple message saying, "Hello."
[2:13] Now, we can use the Alpine image, which is a minimalistic version of Linux that is great for small containers. Let's start by using a docker run command again. Now, you will mount a volume. What this means is that you will take your folder from your local machine and map this into a folder accessible from within the container.
[2:37] In this case, you will take your current working directory and map it to the /app folder inside the container.
[2:46] Next, you can tell Docker to run the Alpine image. Finally, you specify a command that you want to execute once the container is started. In this case, you can ask Alpine to use the cat command, which is a command in Linux that prints a file to the standard output, to show the content of the hello.text file. The result should be a line that says, "Hello," from the container.
[3:14] You can also log inside a container by running Docker in interactive mode with the -it flag. Say you want to see the content of the file system inside of our container this time. You will use a very similar command. First, we will use docker run with the -it flag. We will mount our volume just like we did previously...
Windows docker command FIX:
docker run -it -v "$(pwd):/app" alpine cat "app/hello.txt"
Running this cmd on windows gives error
Error > cat: can't open '/app/hello.txt': No such file or directory
But I did run following fine.