The software described in this documentation is either no longer supported or is in extended support.
Oracle recommends that you upgrade to a current supported release.

4.2 Creating and Running Docker Containers

You use the docker run command to run an application inside a container, for example:

[root@host ~]# docker run -i -t --name guest oraclelinux:6.8 /bin/bash
[root@guest ~]# cat /etc/oracle-release
Oracle Linux Server release 6.8
[root@guest ~]# 

This example runs an interactive bash shell using the Oracle Linux 6 image named oraclelinux:6.8 to provide the container. The -t and -i options allow you to use a pseudo-terminal to run the container interactively. [root@host ~] and [root@guest ~]# represent the prompts shown by the host and by the container respectively. The actual prompt displayed by the container might be different.

The --name option specifies the name guest for the container instance. Docker does not remove the container when it exits and we can restart it at a later time.

If an image does not already exist on your system, the Docker Engine performs a docker pull operation to download the image from the Docker Hub (or from another repository that you specify) as shown in the following example:

[root@host ~]# docker run -i -t --rm oraclelinux:7.0
Unable to find image 'oraclelinux:7.0' locally
Pulling repository oraclelinux
073ded22ac0f: Download complete 
511136ea3c5a: Download complete 
ad98bd7101f2: Download complete 
cbb192d7f4cf: Download complete 
Status: Downloaded newer image for oraclelinux:7.0
[root@guest /]# cat /etc/oracle-release 
Oracle Linux Server release 7.0
[root@guest /]# exit
exit
[root@host ~]# 

Because we specified the --rm option instead of naming the container, Docker removes the container when it exits and we cannot restart it.

From another shell window, you can use the docker ps command to display information about the containers that are currently running, for example:

[root@host ~]# docker ps
CONTAINER ID  IMAGE              COMMAND    CREATED         STATUS        PORTS  NAMES
768a3d7b605a  oraclelinux:6.8    /bin/bash  14 minutes ago  Up 14 minutes        guest

The container named guest with the ID 77bacba845e2 is currently running the command /bin/bash. It is more convenient to manage a container by using its name than by its ID.

To display the processes that a container is running, use the docker top command:

[root@host ~]# docker top guest
UID    PID    PPID   C   STIME   TTY     TIME       CMD
root   7474   1958   1   15:40   pts/2   00:00:00   /bin/bash

In version 1.3.0 and later of Docker, you can use the docker exec command to run additional processes in a container that is already running, for example:

[root@host ~]# docker exec -i -t guest bash
[root@guest ~]#

In version 1.3.0 and later of Docker, you can use the docker create command to set up a container that you can start at a later time, for example:

[root@host ~]# docker create -i -t --name newguest oraclelinux:6 /bin/bash
af621dc9888019a4e8b58c5ef95e265d18c05c983761d5b8c7c046fcbf1176e0
[root@host ~]# docker start -a -i newguest
[root@newguest ~]#

The -a and -i options to docker start attach the current shell's standard input, output, and error streams to those of the container and also cause all signals to be forwarded to the container.

You can exit a container by typing Ctrl-D or exit at the bash command prompt inside the container or by using the docker stop command:

[root@host ~]# docker stop guest
guest

The -a option to docker ps displays all containers that are currently running or that have exited.

[root@host ~]# docker ps -a
CONTAINER ID  IMAGE              COMMAND    CREATED  STATUS             PORTS  NAMES
768a3d7b605a  oraclelinux:6.8    ...        ...      Exited (0) 9 seconds ago  guest
8a1b9b19bb70  oraclelinux:6.6    ...        ...      Up 38 seconds   ...       newguest

You can use docker start to restart a stopped container. After reattaching to it, the contents remain unchanged from the last time that you used the container.

[root@host ~]# docker start -a -i guest
[root@guest ~]# touch /tmp/foobar
[root@guest ~]# exit
[root@host ~]# docker start -a -i guest
[root@guest ~]# ls -l /tmp/foobar
-rw-r--r--. 1 root root 0 Aug 29 05:23 /tmp/foobar

Because the container preserves any changes that you make to it, you can reconfigure files and install packages in the container without worrying that your changes will disappear.

If you need to remove a container permanently so that you can create a new container with the same name, use the docker rm command:

[root@host ~]# docker rm guest
guest
Note

If you specify the --rm option when you run a container, Docker removes the container when the container exits. You cannot combine the --rm option with the -d option.

In version 1.2.0 and later of Docker, specifying the -f option to docker rm kills a running container before removing it. In previous versions, the same command stops the container before removing it. If you want to stop a container safely, use docker stop.

You can use the docker logs command to watch what is happening inside a container, for example:

[root@host ~]# docker logs -f guest
...
bash-4.x# touch /tmp/foobar
bash-4.x# exit
exit
bash-4.x# 
bash-4.x# ls -l /tmp/foobar
-rw-r--r--. 1 root root 0 Aug 29 05:23 /tmp/foobar

The -f option causes the command to update its output as events happen in the container. Type Ctrl-C to exit the command.

You can obtain full information about a container in JSON format by using the docker inspect command. This command also allows you to retrieve specified elements of the configuration, for example:

[root@host ~]# docker inspect --format='{{ .State.Running }}' guest
true