Source: Canva Team

Beyond Names & Labels: Docker Commands

Ananya
5 min readOct 2, 2023

--

“I learned very early the difference between knowing the name of something and knowing something.”
Richard P. Feynman

These blog series capture the essence of Feynman’s quote, emphasizing the distinction between superficial knowledge (knowing the name of something) and deep understanding (knowing something). If you want to learn some basic Docker commands then read away 😊

Pre-read

In the previous article, I showed you the Hello World of Docker. In that example, you pulled a hello-world image and started a container out of it.

Let’s see another example of working with the open-source image of redis.

Redis is an open-source, in-memory, key-value data store.

Docker Run

You can specify an image name with the docker run command to pull the image from the repository, create a container, and start the container out of it.

docker run redis/redis-stack:latest

docker create — creates the docker container and returns its id.

docker start — starts the container

See Container List

To see the list of containers running, run the below command:

docker ps
Sample docker ps output

Executing Commands in Running Container

You can execute commands within docker containers. For example, we started a redis container. Now, we want to set some key-value data in redis. We can use a utility called redis-cli to achieve the same.

docker exec -it <containerId> <command>

In our case, redis-cli is the command that we want to run inside the redis container having id d37e8aaff699

Running commands inside the docker container

Docker Container Logs

You can see the logs of your container using the following command:

 docker logs --follow <containerId>

Stopping the container

You can stop a container by using one of the below commands:

docker stop <containerId>

docker stop command issues a SIGTERM to the container and allows the container 10s time to shut down gracefully.

Or

docker kill <containerId>

docker kill command issues a SIGKILL to the container and terminates the container instantly.

Data Persistence with Docker

Let’s try some commands:

<!-- Start a redis server on background -->
docker run -d redis/redis-stack-server:latest

<!-- Add one key-value pair (k1:v1) in the redis container -->
docker exec -it <containerId> redis-cli

<!-- Stop the container -->
docker stop <containerId>

<!-- Clean-up stopped containers-->
docker system prune

<!-- Start a new redis server on background -->
docker run -d redis/redis-stack-server:latest

<!-- Try to Get k1-->
docker exec -it <containerId> redis-cli
<!-- No value is returned -->
Setting k1 to v1 in the redis container
Trying to fetch k1 in a new container

What does this show you 🤔? The redis data was only available till the first container was up and running. And why 🤔? Because Containers are isolated. In short, there is no data persistence beyond the container lifetime by default.

Docker Volume addresses this requirement of data persistence. You can configure a docker volume to specify where to persist your data outside the container.

docker run -v `pwd`/redis_data_backup:/data redis/redis-stack:latest

In this command, redis_data_backup is the path in the local system where redis’s data (/data path inside the container) will be persisted.

Let’s run the above scenario again, but this time using docker volumes:

<!-- Start a redis server on background with docker volume-->
docker run -d -v `pwd`/redis_data_backup:/data --env REDIS_ARGS="--save 60 1" redis/redis-stack-server:latest

<!-- Add one key-value pair (k1:v1) in the redis container -->
docker exec -it <containerId> redis-cli

<!-- Stop the container after 60 s (We have configured data write to disk in 60s interval) -->
docker stop <containerId>

<!-- Clean-up stopped containers-->
docker system prune

<!-- Check the content of redis_data_backup folder to see the backed up data-->
ls redis_data_backup/

<!-- Start a new redis server on background with docker volume -->
docker run -d -v `pwd`/redis_data_backup:/data --env REDIS_ARGS="--save 60 1" redis/redis-stack-server:latest

<!-- Try to Get k1-->
docker exec -it <containerId> redis-cli
<!-- Voila! data is returned -->
Setting k1 to v1 in the redis container with docker volume
Trying to fetch k1 in a new container

And it worked!!

Docker volume is extremely useful for:

  1. Supporting data persistence.
  2. Configuring backup of container data to any remote host or cloud provider.
  3. Sharing data across multiple containers.

Docker Port Mapping

Let’s try some commands:

<!-- Start a redis server on background with docker volume-->
docker run -d -v `pwd`/redis_data_backup:/data --env REDIS_ARGS="--save 60 1" redis/redis-stack-server:latest

<!-- From your local system try connecting to the redis running inside the docker container -->
<!-- I will be using redis-cli for that -->
redis-cli
Trying to connect to docker container redis from outside

What does this show you 🤔? The redis, even though it is running at 6379 port, is only accessible within the container. And why 🤔? Again because Containers are isolated. Containers, by default, do not expose any of its ports to the outside world.

Port Mapping addresses this requirement. You can configure port mapping with the docker run command to open the port of the container to the outside world.

docker run -p <outside world port>:<port inside docker container> redis/redis-stack:latest
With port mapping, we are able to connect to redis running inside the container

Thank you for giving me the gift of your precious time 😇 Happy Reading!!

Reference

--

--

Ananya

Software Developer | Technical Writer | Technology Enthusiast