Installing Linux
Installing Linux is a task most developers and all Linux administrators are familiar with. Several Linux distributions are available including Red Hat Linux, Ubuntu, openSuse and Oracle Linux. Some of the options for installing Linux include using the Amazon Linux AMIs, ISO images and virtual machine images. Linux could also be installed using a Docker image. Several Docker images for Linux distributions are available from the Docker public repository (https://hub.docker.com/). In this chapter we will install Oracle Linux using a Docker image.
Setting the Environment
The following software is required for this chapter:
For Host OS we have used the Red Hat Enterprise Linux 7.1 (HVM), SSD Volume Type - ami-12663b7a on Amazon EC2. Login to the Amazon EC2 instance using the following command; the IP address (54.165.251.73) will be different for different users and may be obtained as explained in Appendix A.
ssh -i "docker.pem" [email protected]
Install Docker as explained in Chapter 1. Start Docker with the following command.
sudo service docker start
An OK message indicates that Docker has started. To confirm that Docker has started run the following command.
sudo service docker status
If the Active: label has the active (running) value as shown in Figure 2-1, Docker has started and is ready to deploy applications in Docker containers.
Figure 2-1. Finding Docker Status
Downloading the Docker Image
We have used the Docker image oraclelinux available from the Docker Hub Repository (https://hub.docker.com/_/oraclelinux/). Download the latest version of the oraclelinux Docker image with the following command.
sudo docker pull oraclelinux
Docker images are tagged to the image name to differentiate the variants (or versions) of the image. For example, to download the oraclelinux 6.6 version, run the following command.
sudo docker pull oraclelinux:6.6
To download the oraclelinux 7 version run the following command.
sudo docker pull oraclelinux:7
The Docker images for oraclelinux 6.6 and 7 versions get downloaded as indicated by the output in Figure 2-2.
Figure 2-2. Downloading Docker Images
Listing Docker Images
The Docker images downloaded and available to run applications may be listed with the following command.
sudo docker images
The two oraclelinux images; versions 6.6 and 7 are listed as shown in Figure 2-3. The TAG column lists the version (or variant) of the image.
Figure 2-3. Listing Docker Images
Running a Container in Detached Mode
The docker run command is used to run a process in a container. The docker run command may be run in detached mode or attached mode. In detached mode the container is detached from the command line and the I/O is done through networking and shared volumes. The following command syntax would run a Docker container in a detached mode as indicated by the –d option. The –name option sets the name of the container.
sudo docker run –d --name <container-name> <image-name>
The –i –t options if specified with the –d option do not start an interactive terminal or shell. For example run the following command to start a container in detached mode with name oraclelinux using the oraclelinux Docker image with tag 6.6.
sudo docker run –i –t –d --name oraclelinux6 oraclelinux:6.6
Even though the –i and –t options are specified, the container runs in detached mode as shown in Figure 2-4.
Figure 2-4. Starting Docker Container in Detached Mode
In detached mode, the Docker container is detached from the STDIN, STDOUT and STDERR streams. The –rm option cannot be used in the detached mode. For docker run command syntax detail, refer to https://docs.docker.com/engine/reference/run/.
Running a Container in Foreground
To run a Docker container in attached mode, omit the –d option.
sudo docker run <image-name>
In attached mode, a container process is started and attached to all the standard streams (STDIN, STDOUT and STDERR). The –name option may also be used in attached mode to specify a container name. To start an interactive terminal, use the –i and –t options, which allocates a tty to the container process. The –rm option if specified cleans up the container resources including the filesystem allocated the container after the container has exited. Run the following command to run a container process using the oraclelinux:7.0 Docker image; the –name option specifies a name to the container, the –i –t options start an interactive terminal (tty) and the –rm option cleans up the container after the container has exited.
sudo docker run –i –t –rm –name oraclelinux7 oraclelinux:7.0
The Docker container process using the oracleinux image starts and attaches to an interactive shell or tty as shown in Figure 2-5.
Figure 2-5. Starting Docker Container in Attached Mode
A container name must be unique. If a container with the same name as a running container is started, an error is generated as indicated in Figure 2-6.
Figure 2-6. Container Name must be Unique
Listing Docker Containers
Docker containers can be running or not running. Run the following command to list Docker containers that are running.
sudo docker ps
The only running containers, oraclelinux:6.6 and oraclelinux:7.0, get listed as shown in Figure 2-7. The STATUS column indicates whether the container is “Up” and running or “Exited”. The CONTAINER ID column lists the container ID.
Figure 2-7. Listing Running Docker Containers
To list all containers running or exited, run the following command.
sudo docker ps –a
The containers that have exited also get listed as shown in Figure 2-8.
Figure 2-8. Listing All Docker Containers
Finding Oracle Linux Container Information
Information about a container can be listed with the docker inspect command. Run the following command to list information about container oraclelinux7.
sudo docker inspect oraclelinux7
The container detail gets listed in JSON format as shown in Figure 2-9.
Figure 2-9. Output from docker inspect
Listing the Container Processes
List the processes that a container is running with the docker top command. The following command lists the processes run by the oraclelinux6 container.
sudo docker top oraclelinux6
The UID and PID are among the columns listed for the processes as shown in Figure 2-10.
Figure 2-10. Listing Container Processes
Starting an Interactive Shell
The interactive shell or tty may be started when the container process is started with the docker run command using the attached mode and the –i –t options to indicate an interactive terminal.
sudo docker run –i –t --rm <image-name>
Run the following command to run a container for the oraclelinux:7.0 image and start a tty terminal.
sudo docker run –i –t --rm –name oraclelinux7 oraclelinux:7.0
An interactive shell gets started and the container process gets attached to the terminal as shown in Figure 2-11.
Figure 2-11. The interactive shell gets started when a Docker container is started in Attached Mode
If a container process has already been started in detached mode using the –d option, the interactive terminal may be started with the following command syntax.
docker exec -i -t <container> bash
The –i and –t options could be combined into –it. Run the following command to start a tty for the oraclelinux6 container.
sudo docker exec –it oraclelinux6 bash
An interactive tty gets started as shown in Figure 2-12.
Figure 2-12. Starting an Interactive Terminal for a Docker Docker Container running in Detached Mode
Whether the tty is started when a container process is started using the –rm, -it options or subsequently using the preceding command, container commands may be run in the interactive shell. Commands run in an interactive shell are directed at the software or application that is running in the container. For example, if the Docker container is running Oracle Linux, the tty commands are for the Oracle Linux platform. For example, output the Oracle release using the following command.
cat /etc/oracle-release
The Oracle Linux Server release 7.0 gets listed as shown in Figure 2-13.
Figure 2-13. Outputting Oracle Release
Run some other Linux commands to create a directory, set the permissions on the directory, and list the files and directories.
mkdir /orcl
chmod 777 /orcl
ls -l
The /orcl directory gets created and gets listed as shown in Figure 2-14.
Figure 2-14. Listing Files and Directories
Run the exit command to exit the interactive shell as shown in Figure 2-15.
Figure 2-15. Running the exit Command
Creating a Container
The docker create command is used to create a container. Run the following command to create a container called orcl6 for the oraclelinux:6.6 image. Even though the –i –t options are specified, an interactive shell does not get started.
docker create -i -t --name orcl6 oraclelinux:6.6 /bin/bash
To start the Docker container orcl6 and an interactive shell for the orcl6 container, run the docker start command. The -a and -i options attach the current shell’s standard input, standard output and standard error streams to those of the container. All signals are forwarded to the container.
sudo docker start –a –i orcl6
The Docker container orcl6 and an interactive shell get started as shown in Figure 2-16.
Figure 2-16. Starting an Interactive Shell with docker start
Stopping a Container
To stop a running container, run the docker stop command. Run the following command to stop the orcl6 container.
sudo docker stop orcl6
The orcl6 container gets stopped as shown in Figure 2-17.
Figure 2-17. Stopping a Docker Container
Subsequently, the docker ps –a command should list the orcl6 container as “Exited” as shown in Figure 2-18.
Figure 2-18. Listing an Exited Container
Removing a Container
To remove a container, run the docker rm command. The container first must be stopped before removing, or the docker rm command will not remove the container. Run the following command to remove the orcl6 container.
sudo docker rm orcl6
The orcl6 container gets removed as shown in Figure 2-19.
Figure 2-19. Removing A Docker Container
Summary
In this chapter we installed Oracle Linux in a Docker container. We discussed how to download the Docker image and run a container process. We also discussed using the different image tags, starting an interactive shell, the different modes of running a container, and starting, stopping and removing a container. In the next chapter we shall discuss running Oracle database in a Docker container.