Running CentOS in a Docker container

I’m just getting started with Docker. I’ve thought for years that containerization is a great idea, but I haven’t actually done anything with containers yet. Time to get started.

I ran through a couple tutorials on the Docker docs site and created a cloud.docker.com account to get some basic familiarity.

I found the CentOS container repository on Docker Hub: https://hub.docker.com/_/centos/

Let’s try running it!

$ docker pull centos
$ docker run centos

Did it do anything? It looks like it did something. At least, it didn’t give me an error. What did it do? How do I access it?

$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Nothing is actively running. That makes sense, because we’re not telling the containerized OS to do anything — it starts, it doesn’t have anything to do, and so it shuts down immediately. Instead we can tell it to run interactively and with a terminal by specifying a couple options:

-i, --interactive
-t, --tty
(“allocate a pseudo-TTY”, i.e. a terminal)
(see docker run --help for details)

$ docker run -i -t centos
[root@4f0b435cdbd7 /]#

I’m in!

What if I want to modify the container? Right now it is pretty bare-bones. For example, this doesn’t even have man installed:

[root@4f0b435cdbd7 /]# man man
bash: man: command not found

[root@4f0b435cdbd7 /]# yum install man
...
[root@4f0b435cdbd7 /]# man man
No manual entry for man

Quite the improvement! Now we need to save our change:

[root@4f0b435cdbd7 /]# exit

$ docker commit 4f0b435cdbd7 man-centos
$ docker run -i -t man-centos

[root@953c512d6707 /]# man man
No manual entry for man

Progress! Now we have a CentOS container where man is already installed. Exciting.

I can’t (that I know of) inspect the container and know whether or not man is installed without running it. That’s fine for many cases, but next I will attempt to figure out how specify via a Dockerfile that man is installed.

10 thoughts on “Running CentOS in a Docker container”

  1. Can you install MySql inside of Centos and make it run when connecting to Docker container?

  2. You can run MySQL (or MariaDB, an open source MySQL-compatible database) on CentOS in a Docker container. However, unless you are running it merely as a temporary test environment, I would recommend against it. Any changes to the database would not be persistent and would be lost if you started another instance of the container.

    One alternative would be to use an external database. I’ve looked at AWS RDS instances, for example, although those tend to be expensive for the trivial workloads I need.

    This question (and the answers) on StackOverflow have some ways to address this issue using containers: How to deal with persistent storage (e.g. databases) in docker

  3. I pulled ubuntu image from docker but when I’m trying to install man using apt-get, it’s showing an error “Unable to locate package man”. It might be that ubuntu container doesn’t have Internet connectivity. How do we fix this in a container?

  4. @AV: CentOS, unlike Ubuntu, is based in RHEL. This means apt is not available. Instead, you should use the command yum (eg. yum install man).
    The error could also be due to the fact that the network is disabled by default, at least in my installation. To enable it I use the command nmtui (it displays the current interface adapters and let’s you enable them).

  5. Thanks for this. Although running “man man” gives you “No manual entry found for man”, try running “man ls” or “man cd” and you’ll see that having man installed is pretty useless as is. Here’s what else you need to do after completing your steps.
    1) yum install vim
    2) vim /etc/yum.conf
    3) Remove the tsflags=nodocs line
    a => down arrow to that line => delete that line => esc => :wq
    4) yum install man-pages
    5) man ls
    6) repeat the docker commit steps again

  6. awesome, very simple, in less then 3 min i get control of centos, tks bro

  7. That’s what we as a developer expect a document to be!!

    Very well done, really appreciate it!

Leave a Reply

Your email address will not be published. Required fields are marked *