Running Containers
Instructions to run the Docker containers.
Running Containers
Note: At the end of this topic, you will be provided with a terminal to an environment that has all the prerequisites (such as Docker and Kubernetes) up and running. You can practice your commands in this tutorial without any need to setup your own environment. |
Containers are running instances of an Image. To run containers, follow these steps:
- Create a container from the base image for the latest version of the Ubuntu that is available.
- Important
- If you do not have an Ubuntu base image installed locally, extract the latest one for your local repository.
- You must start the container in interactive mode attached to the current terminal and running the bash shell.
- After running, make sure you shut down the container by running 'exit'.
Lifecycle
The following commands illustrate the Docker Lifecycle:
docker create
creates a container but does not start the container.docker rename
allows the container to be renamed.docker run
creates and starts a container in a single operation.docker rm
deletes a container.docker update
updates a container's resource limits.
Usually, when you run a container without options, it will start and stop immediately. If you want the container to keep running, you can use the command, docker run -td container_ID
. This command uses the option-t
to allocate a pseudo-TTY session and option-d
to detach the container automatically (you can run container in background and print the container ID).
To have a transient container, use the command docker run –rm
. This command will remove the container after it stops.
To map a directory on the host to a docker container, use the command docker run -v $HOSTDIR:$DOCKERDIR<
.
To remove the volumes associated with the container, the deletion of the container must include the option-vswitch
like in docker rm -v
.
There is also a logging driver available for individual containers in docker 1.10. To run docker with a custom log driver (that is syslog), use the command docker run --log-driver=syslog.
docker run --name yourname docker_image
is a useful command because when you specify --name
inside the run command, you can start and stop a container by calling it with the name that you specified when you created it.
Starting and Stopping a Container
Commands to start and stop a container:
docker start
starts a container so it is running.docker stop
stops a running container.docker restart
stops and starts a container.docker pause
pauses a running container, "freezing" it in place.docker unpause
unpauses a running container.docker wait
blocks until running container stops.docker kill
sends a SIGKILL signal to a running container.docker attach
connects to a running container.
To integrate a container with a host process manager, start the daemon with the commands -r=false
and then use docker start -a
.
You can practice the above-mentioned commands using the following widget: