Docker is an open source project supported by a commercial entity of the same name that makes it super-easy to run an application process inside a relatively isolated environment called a container. Unlike a virtual machine (VM), which has its own kernel, a container is dependent on the host operating system’s kernel.
As a result, it’s much lighter and boots up much faster. It’s the simplest tool that puts enterprise-class application deployment and management capabilities right on your desktop (laptop). By default, Docker containers are run using application images hosted on Docker Hub.
In this article, we’ll go through the process of installing and using it to run containers on any desktop flavor of Fedora 25.
Install Docker on Fedora 25
Every task will be completed from the command line, so fire up a terminal emulator. The latest version of Docker is in the Fedora official repository, so all you need to install Docker is type the following command:
# Installing Docker on Fedora 25 sudo dnf install docker #
That installs the latest Docker Engine (version 1.12.3), which consists of the Docker daemon and Docker client. Because one needs the other to do anything useful, you first need to start the daemon and configure the system to start it every time the computer is (re)booted. The following pair of commands will accomplish that.
# Start the Docker daemon sudo systemctl start docker # Enable Docker to start on boot sudo systemctl enable docker #
Run Docker Containers on Fedora 25
The Docker daemon is now up and running, so you can run your first container. By default, the Docker is pointed to Docker Hub, which holds hundreds (perhaps thousands) of images waiting to be used to run containers. But let’s start by running the famous Hello World of the (Docker) container universe.
# Run your first Docker container sudo docker run hello-world #
The container will run and exit, because that’s how it was configured to behave. In the output, you’ll see lines like those below.
# Output of running a Docker container Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash #
The above output tells you each step involved, and what else you can run, so let’s try running the suggested container, using an official Ubuntu image. Unlike the first container you just ran, this one will not exit soon after running. Instead, the command will run the container, keep it running, and give you interactive tty access inside it, with the Bash shell.
# Run your first Docker container sudo docker run -it ubuntu bash #
What I’m sure you observed with that container is how fast it took to start. As noted earlier, that’s one difference between a software container and a virtual machine instance – speed. Containers boot up really, really fast. And they can be killed just as quickly.
You’ll notice that your command prompt has changed to something like root@131a58505d2d:/#, where the string after the @ sign is the unique id of the container. So your host machine is powered by your favorite desktop flavor of Fedora 25, but you’re now operating from inside an Ubuntu container.
With command line access inside the container, you can do anything you want, like just exit the container, or do somethings more interesting, like update the package database, upgrade the system, and install any software you feel like installing. Note that because you’re operating from inside Ubuntu, the package management command is apt, not dnf. For now, you may exit the container by typing exit. In a future article, we’ll go into details of how to install applications inside a container, commit the changes, and push the new image derived from that to Docker Hub.
Let’s end this by running another container using the official Nginx image. By default, running the Nginx container will expose its ports 80 (http) and 443 (https). What this command does is map port 80 on the host machine to the equivalent port inside the container.
# Run Nginx container sudo docker run -p 80:80 nginx # Alternatively, you can force the container to detach by running this command sudo docker run -d -p 80:80 nginx #
Mapping the port in that fashion makes it possible to access the default Nginx page by pointing your browser to the host machine’s IP address. If you did that, you should see the default Nginx page, and if you ran the command without the -d option, you should see some output indicating that the page has been accessed.
Depending on the host machine’s resources, you can run as many containers as you want – at the same time. To search for images available on Docker Hub that you may use to run containers, type the following command:
# To search for an image on Docker Hub sudo docker search# For example, to search for a Redis image sudo docker search redis #
Clean Up After Yourself
Whenever you run a container, the Docker client has to download an image from (by default) Docker Hub. The images are stored on your computer, and will remain there even after you’ve stopped or killed the container. With time, those images can take up a significant chunk of your computer’s storage space.
So a good habit to adopt is to delete those images you’ll not be using anytime soon. To list the images stored on your computer, type:
# Listing Docker images sudo docker images #
You should get an output like the following:
# Listing Docker images REPOSITORY TAG IMAGE ID CREATED SIZE sematext/sematext-agent-docker swarm3k d0b840c91025 6 weeks ago 146.5 MB alpine latest 4e38e38c8ce0 5 months ago 4.799 MB cassandra latest 075b7d5e2ed5 5 months ago 385.2 MB nginx latest 0d409d33b27e 6 months ago 182.8 MB finid/ubuntu-nodejs latest 925bbd11e0d7 6 months ago 206.7 MB finid/ubuntu-nodejs v2 5a33c83a0835 6 months ago 206.6 MB ubuntu-do latest 62359544c9ba 6 months ago 206.6 MB ubuntu latest c5f1cf30c96b 7 months ago 120.8 MB hello-world latest 94df4f0ce8a4 7 months ago 967 B redis latest 0f0e96f1f267 7 months ago 177.5 MB swarm latest 0f1a3829719c 7 months ago 18.71 MB #
Don’t think you’ll be needing an image anytime soon? Delete it:
# Deleting Docker images sudo docker rmi# For example, to delete the Cassandra image shown in the previous output, type: sudo docker rmi cassandra #
Note that a container image can be deleted only if no container is using it, so if you get an error after attempting to delete an image, first stop and/or delete the container.
Running individual Docker containers like you just read in this article is fun, but that’s just the beginning. Docker has built-in support for load balancing, orchestration, service discovery and a host of other features. I’ll be exploring those in future articles. More on Docker here