Deploy Your First Docker Container — Part 3
In last article, we learned how to run specific instance of Redis. Now we’ll learn further step to deploy your first docker container :
Step 5 : Persisting Data
After working with containers for a few days, Jane realises that the data stored keeps being removed when she deletes and re-creates a container. Jane needs the data to be persisted and reused when she recreates a container.
Containers are designed to be stateless. Binding directories (also known as volumes) is done using the option -v <host-dir>:<container-dir>. When a directory is mounted, the files which exist in that directory on the host can be accessed by the container and any data changed/written to the directory inside the container will be stored on the host. This allows you to upgrade or change containers without losing your data.
Using the Docker Hub documentation for Redis, Jane has investigated that the official Redis image stores logs and data into a /data directory.
Any data which needs to be saved on the Docker Host, and not inside containers, should be stored in /opt/docker/data/redis.
The complete command to solve the task is
docker run -d --name redisMapped -v /opt/docker/data/redis:/data redis
Step 6 : Running A Container In The Foreground
Jane has been working with Redis as a background process. Jane wonders how containers work with foreground processes, such as ps or bash.
Previously, Jane used the -d to execute the container in a detached, background, state. Without specifying this, the container would run in the foreground. If Jane wanted to interact with the container (for example, to access a bash shell) she could include the options -it.
As well as defining whether the container runs in the background or foreground, certain images allow you to override the command used to launch the image. Being able to replace the default command makes it possible to have a single image that can be re-purposed in multiple ways. For example, the Ubuntu image can either run OS commands or run an interactive bash prompt using /bin/bash
docker run ubuntu ps launches an Ubuntu container and executes the command ps to view all the processes running in a container.
docker run -it ubuntu bash allows Jane to get access to a bash shell inside of a container.