A Soft Introduction to Docker


If you’re reading this article, you may have heard about Docker and wondered what all the buzz is about. Maybe you’ve looked into it some, but was overwhelmed by all of the configuration and different concepts. Maybe you’ve tried it out, but didn’t know what to do or where to start.
I’ve worked with Docker for about 4 years, and during this time, I’ve come to use many and most of the different parts and tools available. I’ve made mistakes that I can hopefully help you avoid, and I’ve gathered a good overview to hopefully allow you to understand and learn each aspect of Docker in a way that speeds up your learning.
Today, we’ll go through some of the most important concepts, then learn how to run a Docker container.
What is Docker?
Docker is a tool used to “containerize” applications. To “containerize” an application means to bundle the application and it’s dependencies into an isolated system called a container. The container should include all necessary files and tools needed for the application to run successfully.
Docker containers can be anything from Python interpreters to Ubuntu servers. Nearly anything that you would run on your system can be run inside of a Docker container instead, sometimes with hardly any effort and sometimes with a great deal of effort for more complicated applications or workflows.
Containers are created using Docker images. For example, when it is ran, the python
Docker image will create a new container that you can use to run a Python script. The power of Docker images is that they can be run from any system running Docker with a guarantee that the containers will be identical. Running docker run python
from my system will launch the exact same container as would be created on your system.
You can build your own custom images, but most of the time, you should check Docker Hub to see if there’s an official image you can use instead. You can use other images you find on Docker Hub, but these can be made and published by anyone, so you should make sure you trust the image before you do so. We will discuss why you might want to create your own image later in the article.
Images and containers are the core concepts in Docker. For the most part, every other feature Docker offers centers around those things. Take the following examples:
docker network
- manages the network configuration for containers.docker volume
- manages storage for containers.docker build
- build an image from a file.docker exec
- run commands inside of a container.
I admit that I am simplifying here, but it helps to ease into everything as most of what you’ll be working with at the start of your Docker journey will be images and containers.
Running a Container
In this section, I’ll walk you through the most common Docker commands and explain anything interesting we come across. Before we get started, make sure you have Docker installed.
To install Docker on Linux, go to the Docker installation documentation and click on the click on the platform of your system. If you’re on Windows or Mac, or would prefer it, try Docker Desktop for Docker’s desktop application which gives you a graphical user interface you can interact with instead of doing everything in your system’s terminal.
Because Docker Desktop also enables you to use Docker commands in your terminal, the examples we will be done using the Docker CLI commands. Complete the Docker installation, then move onto the following sections to learn how to use some of the most important Docker commands.
The Docker Run Command
Containers are the heart of Docker, so we will start with looking at the most simple commands and why we’d want to use them. Let’s start with running our first container using the docker run
command.
The docker run
command follows the following format:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Let’s try running a Python container. To do this, simply use the following command:
docker run python
You’ll see the image download to your system, and then the command will return. However, this isn’t very useful as we haven’t told the container to do anything. Let’s try to find it and run a command in it.
To list your running containers, use one of the following commands:
# List running containers
docker ps
docker container ls
But no containers were found. Well, we know we’ve executed the run command, so where is it?
The docker ps
command (and the docker container ls
command) list running containers, so maybe the container has been stopped. Let’s check all containers with the -a | --all
option:
# List all containers (including stopped)
docker ps -a
You should then see your Python container in a stopped state with an exit code of 0. This means the container exited without errors. This happened because the Python container expects to run a script or some code, and we didn’t tell Python to run anything in our docker run
command, such as:
docker run python example.py
Being a soft introduction, we’ll open an interactive Python terminal as a demo instead.
Let’s try the run command again, but this time, we’ll use some other arguments:
docker run -it --rm python
The -it
option tells Docker to attach an interactive terminal to the container and pass any output to our terminal, effectively letting us control the container as if our terminal was running in the container. The --rm
option removes the container once we exit
the interactive terminal instead of it sitting in a stopped state like we saw earlier.
Once you run the command above, the Python interpreter should show, and you can then try various commands such as:
>> print(“This is running in a Python container!”)
This is running in a Python container!
>> print(“Use ‘exit()’ to exit the terminal.”)
Use ‘exit()’ to exit the terminal.
The example we’ve just completed is something I like to do often.
Imagine there was a Python package you wanted to test, but didn’t want to install on your actual system. Just spin up a Python Docker container and try it there! Want to run a script, but don’t want keep track of a Python “venv” (virtual environment)? Mount the script and run it with the Python container.
As you can imagine, there’s many other use cases for this and for many other applications, and as you work with Docker, you’ll find more ways you can use them to benefit your workflow.
Conclusion
You now know some of the basics of Docker such as running a container or listing containers. This article was to help you get introduced to these concepts. In future articles, we’ll jump into more of the details in each aspect of Docker such as a more in depth look at containers, building custom Docker images, and a deep dive into Docker commands.
I hope this article gives you enough information to get started with Docker and try it out. If you’re curious to learn more, you can look at the Docker run command reference for more details on all the options there, and I also recommend looking into bind mounts for containers, which allows you to put a folder from your system into the container, making it easier to access files from the host machine.
If you have any questions, leave a comment on the article and I’ll do my best to help you out! Thank you for reading, and I’ll see you back for the more in-depth Docker articles.
Subscribe to my newsletter
Read articles from Ethan Dean directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
