1, first of all, we have to write a basic dockerfile
In any directory
$ VI Dockerfile
Fill in the contents as follows:
From ubuntu:14.04
RUN apt-get Update
RUN apt-get-y Install Redis-server
EXPOSE 6379
entrypoint ["/usr/bin/redis-server"]
2. Build and run the container
After writing the Dockerfile, we can run a command to build a mirror:
$ sudo
docker build -t <your username>
/redis
.
Notice the "." In the back of this command. is indispensable.
After a moment, the image will be created. We then run the Docker Run command to generate the built-in container:
$ sudo
docker run --name redis -d <your username>
/redis
Where the-d parameter indicates that we want to run the container through the detached mode, which means that the container will continue to run in the background.
3. Create and connect the test Container
We can link the generated Redis container to the new test container by using the Docker run with a-link parameter, so that the test container can access the Redis service without exposing any ports:
$ sudo
docker run --link redis:db -i -t ubuntu:14.04
/bin/bash
With the above command, we link the Redis container to the new Ubuntu container and name it db, so that we will only expose the Redis service to this container instead of the larger range.
Next we put the REDIS-CLI in the test Container:
$ apt-get Update
$ apt-get-y Install Redis-server
$ Service Redis-server Stop
Because the-link parameter is used, we find that the environment variable produces some variables that begin with Db_:
$
env
|
grep
DB_
db_name=/condescending_mclean/db
db_port_6379_tcp_port=6379
db_port=tcp://172.17.0.6:6379
db_port_6379_tcp=tcp://172.17.0.6:6379
db_port_6379_tcp_addr=172.17.0.6
Db_port_6379_tcp_proto=tcp
So we can connect to the Redis service through these variables in the test Container:
$ redis-cli -h $DB_PORT_6379_TCP_ADDR
$ redis 172.17.0.6:6379>
$ redis 172.17.0.6:6379>
set
hello world
OK
$ redis 172.17.0.6:6379> get hello
"world"
$ redis 172.17.0.6:6379>
exit
At this point a Docker container containing the Redis service is created.
Construct and run a Redis container