6 Working with Containers #
After you have created your images, you can start your containers based on
that image. You can run an instance of the image by using the docker
run
command. Docker Open Source Engine then creates and starts the
container. The command docker run
takes several arguments:
A container name - it is recommended to name your container.
Specify a user to use in your container.
Define a mount point.
Specify a particular host name, etc.
The container typically exits if its main process finishes. For example, if your container starts a particular application, as soon as you quit the application, the container exits. You can start the container again by running:
>
docker start -ai <container name>
You may need to remove unused containers, you can achieve this by using:
>
docker rm <container name>
6.1 Linking Containers #
Docker Open Source Engine enables you to link containers together which allows for
communication between containers on the same host server. If you use the
standard networking model, you can link containers by using the
--link
option when running containers:
First, create a container to link to:
>
docker run -d --name sles sles12sp4 /bin/bash
Then create a container that will link to the sles container:
>
docker run --link sles:sles sles12sp4 /bin/bash
The container that links to sles has defined environment variables that enable connecting to the linked container.