The first part of this article introduces you to Docker, monolithic and microservice architecture, virtual environment, and containers.
With this general knowledge of Docker and containers, it's time to jump right into running applications in containers.
If you're just getting started with Docker, you'll learn some practical Docker commands in this post that will let you pull and run a Docker image locally, start and stop a container, list all currently running containers, and debug containers.
This blog post also provides some background information that will clarify the precise difference between a container and a Docker image.
To get the most out of this post, ensure to practice each step to get a feel for working with applications in Docker.
Before you begin you should have Docker installed and running. If you haven’t, please see Docker’s official documentation on how to install Docker and make sure to install the stable version. Docker can be installed on Linux, Windows, and macOS.
Launch your preferred terminal, which could be Terminal on Mac, a Bash shell on Linux, or PowerShell on Windows.
Create a DockerHub Account
DockerHub is a public registry where applications are published. DockerHub is like GitHub but for Docker images.
You'll need an internet connection for some commands to execute.
Docker image is simply a file that represents a container. For example, when you package an application with all its dependencies and push it to DockerHub or your private registry, the file in the registry is an image that represents your container.
A container can be viewed as a "box". In this box is an application that is packaged with its dependencies needed to run the application.
This application has its distinct virtual environment- the hostname, disk, and IP address, which is managed by Docker. The application inside the box cannot have any interaction with the outside environment.
The box is called the container because it contains the application with its dependencies. Containers share the host operating system (OS), CPU, and memory.
A container is an instance of a Docker image and a running environment of an image.
To understand exactly the difference between container and image let's practice some commands.
This tutorial uses redis for illustration. You could choose to use a different image or container and it'll still work the same.
Usage: docker pull IMAGE NAME
docker pull is used to download or pull an image from the repository to your local machine. To access an image locally you must pull it first.
let's pull a redis image from DockerHub to our local machine.
In your terminal, run the following command:
docker pull redis
The application package consists of six layers coupled together to run the application.
Note: The latest tag is automatically pulled when you pull an image without specifying a tag. A tag is the same as a version.
To ensure that the image has been downloaded, run the following command in your terminal
docker images. This will list all existing images.
In the above, there's just one image with the REPOSITORY-redis, TAG-latest, image ID, when it was CREATED, and SIZE of the application.
Note: You could specify a version or tag if you don't want Docker to download the default tag.
You could specify the image tag by running the command
docker pull redis:10.1.
10.1 is the version specified. Remember the version is always followed after a semicolon.
Now, you need to get redis running to make it reachable by your application.
Usage: docker run IMAGE NAME
The run command is used to create a container of an image. The first time you use the run command, Docker informs you that the image doesn't exist locally so it'll search for it on DockerHub, pulls and starts the image.
If the image already exists locally, docker goes ahead to run a container of the image.
But if the image doesn't exist in DockerHub or your local machine, you'll get no error message from Docker.
At the point at which the image is running, the image is no more regarded as an image but as a container.
To run the just downloaded image, use
docker run redis. It'll run a container called redis.
In the above image, the docker run command starts the redis image in a container.
Usage: docker run [OPTIONS] IMAGE NAME
-d is one of several docker run options. It is used to run a container in a detached mood.
docker ps command lists all running containers. This command allows you to track your container’s progress. If you ever get an empty list from running the command, ensure that your container is running.
Note: You could use a separate terminal window to run the container and another for the docker ps command.
Usage: docker ps [OPTIONS]
Several options could be used with the docker ps command such as
-s, etc. For instance, the
-a option lists all the exited and running containers. This allows you to track the status of a container. Run the command
docker ps -a to view all your containers.
This image shows all containers in their current status.
Usage: docker stop CONTAINER ID
Containers can be terminated by using the stop command. There are several reasons why you may want to stop a container.
An error or a crash might require you to stop a container. You'll need the ID of the container that you want to stop.
In your terminal run
docker stop "Container ID"
Usage: docker start CONTAINER ID
Using this command, you can restart a stopped container. You can do this by running the docker start "container ID" command.
The next part of this blog post will walk you through the steps and basic commands of working with a running container. For your application to connect to the container, the container must be reachable.
To connect the container to your application, you'll need to bind the host port to the container’s port. The diagram below illustrates how host binding works.
A host port is a port on your computer that your container binds to. Whereas, a container's port is a port on which the container runs and is used to access the services or dependencies inside the container.
For instance, the port of the container below is 6379/tcp
Execute the following command in your terminal:
docker run -p8080:6379 redis
Note: Before executing the command, make sure the container has been stopped.
To check that the host and the container's port have been connected, let's run the
docker ps command.
Usage: docker logs [OPTIONS] CONTAINER ID
You can look through a container's logs using this command. It is utilized to debug or troubleshoot containers.
Note: This command is only functional for containers that are started with the json-file or journald logging driver. -- Docker
Usage: docker exec [OPTIONS] CONTAINER ID /bin/bash
This command is used to access a container's terminal. In the container's terminal, you could navigate directories and issue commands just like on your command line.
docker exec -it "container ID" /bin/bash, for instance, to access the redis container terminal.
-it flag means interactive and indicates that you would like to assess the terminal inside the container.
In the first line, we executed the command and got access to the root directory. In the second line, the
pwd command lists our working directory. We navigate to the directory in line 3 and use the
ls command in line 4 to list all the directories inside the root directory --
Usage: docker rm [OPTIONS] CONTAINER ID
If you want to clean up or remove containers or application packages after following the exercises, you can run this command:
docker rm -f CONTAINER ID
In this blog post, you’ve practiced working with basic Docker commands such as pull, run – which pulls and launches an image, stop, start, logs, exec, rm, etc. You learned how to bind a host and container ports as well as the difference between container and Docker image.
In addition to the Docker commands that were covered in this article, there are many others that you might want to look into. If you'd like to learn more about the Docker command line, please use the link in the resource section.