Your First Docker Container (How to Get Started)

Your First Docker Container (How to Get Started) Programming

In this beginner tutorial on Docker. For a new app that needs to be created, you need to set up and setup the Redis software (an in-memory database, NoSQL).

As a complete beginner with Redis, you are of course overwhelmed as you don’t know where to start. However, you’ve read online that Docker is great for bringing services into development and production.

Goal: In this tutorial, you’ll learn how to set up Redis as a Docker container so that the new LearnProgramming app can take over the world.

Your development environment already has the latest version of the Docker engine and access to Docker. Locally, you’ve set up the “Docker Desktop” client on your Windows machine, which can be controlled via a GUI as well as the command line.

I’ll quickly give you a rough overview of “what Docker actually is” and then we’ll get right into your hands-on task in this tutorial.

What is Docker?

Imagine you’re working on a complex app. Since you need help developing it, you send your code to a collaborator.

Case 1 – Without Docker: This person runs exactly the same code, with exactly the same data set, but gets a slightly different result. This can be due to various reasons, such as a different operating system, a different version of the programming language, etc.

Docker tries to solve such problems.

Case 2 – Using Docker: A Docker container can be seen as a computer inside your computer. The good thing is that you can send your code to your collaborator, he will start the computer, run your code and he will get exactly the same result as you.

In short, you should use Docker because it allows you to accurately reproduce dependencies from the operating system down to the smallest version details.

Basic vocabulary of Docker explained simply – Images & Containers

The words “images” and “containers” will come up more often in the following. An instance of an image, is called a container. An images is the setup of the virtual machine, sort of the files and dependencies.

When you run a particular image, you have an instance of it, which we call a container. You can have many running containers of the same image.

Tutorial step 1 – Start a container

The first task is to identify the name of the Docker image configured to run Redis.

Docker is used to launch all containers based on a Docker image. These images contain everything needed to start the process. Conversely, this means that the host does not need any configuration or dependencies.

You can find the existing base images at Alternatively, you can use the docker search <image name> command in your command line to search for images. Since you are searching for “Redis”, the command would look like this:

docker search redis

Since Redis is a database system, you want to run it as a background service so you can continue working. By default, containers are started as foreground processes.

To run Redis as a background process, you need to use the “docker run” command with the “-d” option. It will look like this:

docker run -d redis

Since you are using the Redis image for the first time, it needs to be downloaded first. This process is done automatically after running the above command.

Tutorial step 2 – List running containers

The Redis container you started is now running in the background. You can use the docker ps command to list all running containers. This command also shows the nickname (shortname) and the container ID.

With this information, you can learn more about the container (if desired):

  • The docker inspect <nicename|ContainerID> command provides more details about a running container, such as its IP address.
  • The docker logs <nicename|ContainerID> command displays the recorded log of read and write processes of the respective container.

Tutorial step 3 – Access to Redis

Finally, you’ve done it. Your Redis instance is running.

But, wait a minute. Somehow you don’t have access to it?

The reason for this problem is that each container runs in a sandbox (isolated environment).

If a service is to be accessible to a process that is not running in a container, then the port must be opened through the host. Once the port is open, it is possible to access the process as if it were running on the host OS itself.

Tip: Redis runs on port 6379

To make your instance accessible to the host, the next step is to bind the port (in this case, port 6379). You have to specify the <hostport> as well as <containerport>. We also assign an easy to remember name for the hostport. The command for this looks like this:

docker run -d --name redisPort -p 6379:6379 redis:latest

Tutorial Step 4 – Persistent Data

Great! We’ll now assume that you’ve already been working with Docker containers for a week. However, you find that your valuable data keeps getting removed as soon as you delete or recreate a container.

This is not a good solution for the LearnProgramming app, as the data needs to be saved (persistent) and reused even if containers are removed or recreated.

To solve the problem, you need to bind a directory (volume) with the -v <host path>:<container path> option. When a directory is bound, or more precisely mounted, the files that exist in that directory on the host can be accessed through the container. Any data that is modified or created in the directory inside the container is stored in the bound directory on the host.

This allows you to change containers on a whim without losing your valuable data each time. With the Redis image, log files and other files are stored in the /data folder.

The data you want to be persistent should be stored on the host under /opt/docker/data/redis. Now that you have defined both host path and container path, you can write them in your command:

docker run -d --name redisMount -v /opt/docker/data/redis:/data redis

Conclusion of the tutorial

In this first beginner tutorial on Docker containers and images, you have successfully completed your task of setting up the Redis in-memory database. You chose to run Redis as a Docker container to make it easier to manage and share with others. You started Redis, listed container data, established access to Redis between the Docker instance and the host, and persisted data storage from the container to the host.

Rate article
Add a comment