Installing and using docker containers on Fedora 24

Recently a lot of organizations started to migrate their environment to microservices architecture using containerization tools such as docker. The advantage with container tools are it is fast, robust and gives you ability to control its behavior. I am going to show you some basics with docker containers.  But first we need to have it installed on the system. I am using Fedora 24 as my workstation. So this guide is written keeping in mind F24, but it should work on any linux flavors as well.

Installing docker

Docker website has very good documentation about installing and using docker which can be found here. To save time I will list the steps below.

First get docker repo file or create one with below contents in it.

[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/fedora/$releasever/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg

Now you can use dnf to install the docker engine.

#dnf install docker-engine

Next you need to start the docker service and enable it to run at boot.

#systemctl start docker
#systemctl enable docker.service

This will allow you to run docker containers as root user, but if you want to allow normal users to run containers then you need to add them to docker group. Make sure the users have sudo privilages as well.

#usermod -aG docker username

Running nginx container

Now that we have installed docker and want to deploy nginx container. There are few ways to do it, but for simplicity we will use nginx from docker repo. To get the nginx container image run below command.

$docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx

8ad8b3f87b37: Pull complete 
c6b290308f88: Pull complete 
f8f1e94eb9a9: Pull complete 
Digest: sha256:aa5ac743d65e434c06fff5ceaab6f35cc8519d80a5b6767ed3bdb330f47e4c31
Status: Downloaded newer image for nginx:latest

As we have not specified any tags it will default to latest and pull the latest nginx container image available from docker repository. You can verify the available images locally using below command.

$docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 4a88d06e26f4 4 days ago 183.5 MB

Lets say we want to run the container in detached mode and want to forward port 8080 from our host to port 90 on nginx container run the below command.

$ docker run -itd --name web -p 8080:80 nginx:latest
8ab540475170566d15f4576b1f93b8193947c80cf60bf57d9448f858a15a8410

Docker operations with container

We can check running docker containers using below command.

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8ab540475170 nginx:latest "nginx -g 'daemon off" 40 seconds ago Up 37 seconds 443/tcp, 0.0.0.0:8080->80/tcp web

As you can see the port 8080 is forwarded to container port 80.

Let’s check nginx on local port to see if it works

$ curl localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
 body {
 width: 35em;
 margin: 0 auto;
 font-family: Tahoma, Verdana, Arial, sans-serif;
 }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

If we want to stop the container just run below command.

$ docker stop web
web

And if you check no container should be running on the system now.

$docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$

There are lots of things that could be done with docker containers but we would revisit it next time.

Leave a Reply