A common misconception is that Docker is a system for running containers. Docker is only a build-tool to wrap up any piece of Linux based software with all its dependencies in a complete filesystem that contains everything it needs to run: code, runtime, system tools, and system libraries. The technology to run Linux containers is called operating-system-level virtualization and provides multiple isolated environments built in every modern Linux kernel by default. This guarantees that it will always run the same, regardless of the environment it is deployed in; thus making your application portable. Therefore, when it comes to distributing your Docker applications into Linux containers, two major conceptional terms must be introduced: Docker images and containers. If you ever wanted to set up and run your own WordPress installation, in this recipe we will show you how to do so the fastest way possible by downloading a pre-made WordPress image from the official Docker hub; we will then run a container from.
To complete this recipe, you will require a working installation of the CentOS 7 operating system with root privileges, a console-based text editor of your choice, and a connection to the Internet in order to facilitate the download of additional Docker images. It is expected that Docker has already been installed and is running.
The official WordPress image from Docker Hub does not contain its own MySQL server. Instead it relies on it externally, so we will start this recipe by installing and running a MySQL docker container from Docker Hub.
<PASSWORD>
in the following command with a strong MySQL database password of your own choice (at the time of writing, the latest WordPress needs MySQL v.5.7; this can change in the future, so check out the official WordPress Docker Hub page):docker run --restart=always --name wordpressdb -e MYSQL_ROOT_PASSWORD=<PASSWORD> -e MYSQL_DATABASE=wordpress -d mysql:5.7
<PASSWORD>
string from the previous step):docker run --restart=always -e WORDPRESS_DB_PASSWORD=<password> -d --name wordpress --link wordpressdb:mysql -p 8080:80 wordpress
docker ps
docker inspect wordpress
docker logs -f wordpress
http://<IP ADDRESS OF DOCKER SERVER>:8080/
A Docker image is a collection of all the files that make up a software application and its functional dependencies, as well as information about any changes as you modify or improve on its content (in the form of a change log). It is a non-runnable, read-only version of your application and can be compared to an ISO file. If you want to run such an image, a Linux container will be created out of it automatically by cloning the image. This is what then actually executes. It's a real scalable system because you can run multiple containers from the same image. As we have seen, Docker is really not only the tools you need to work with images and containers but a complete platform as it also provides tools to access already pre-made images of all kinds of Linux server software. This is really the beauty of the whole Docker system because most of the time you don't have to reinvent the wheel twice trying to create your own docker image from scratch. Just go to the Docker Hub (https://hub.docker.com), search for a software you want to run as a container, and when you find it then just use the docker run
command, providing the Docker Hub name of the image, and you are done. Docker really can be a life-saver when thinking about all the endless hours trying to get the latest trendy programs to work with all the dependencies you need to compile and trying to get it to install.
So what did we learn from this experience?
We started our journey by using the docker run
command which downloaded two images from the remote Docker Hub repos and put them into a local image store (called mysql:5.7
and wordpress
) and then run them (create containers out of them). To get a list of all the images downloaded on our machine, type docker images
. As we have seen, both run
command lines provided the -e
command line parameter, which we need to set some essential environment variables that will then be visible within the container. These include the MySQL database we want to run and the MySQL root password to set and access them. Here we see a very important feature of Docker: containers that can communicate which each other! Often you can just stack your application together from different Docker container pieces and make the whole system very easy to use. Another important parameter was -p
which is used to create a port mapping from our host port 8080
to the internal HTTP port 80 and opens the firewall to allow incoming traffic on this port as well. --restart=always
is useful to make the image container restartable, so the containers automatically get restarted on reboot of the host machine. Afterwards, we introduced you to Docker's ps
command line parameter which prints out all running Docker containers. Here the command should print out two running containers called wordpressdb
and wordpress
, together with their CONTAINER_ID
. This ID is a unique MD5 hash we will use all the time in most of the Docker command line inputs whenever we need to reference a specific container (in this recipe we referenced by container name which is also possible). Afterwards, we showed you how to print out a container's configuration by using the inspect
parameter. Then, to get the Wordpress container's log file in an open stream, we used the log -f
parameter. Finally, since the -p 8080:80
mapping allows incoming access to our server at port 8080, we could then access our Wordpress installation from any computer in the same network using a browser. This will open the Wordpress installation screen.
In this section, we will show you how to start and stop a container and how to attach to your container.
In the main recipe, we used Docker's run
command which is actually a wrapper for two other Docker commands: create
and start
. As the names of these commands suggest, the create
command creates (clones) a container from an existing image and if it does not exist in the local image cache then it downloads it from a given Docker registry (such as the predefined Docker hub), while the start
command actually starts it. To get a list of all the containers (running or stopped) on your computer, type: docker ps -a
. Now identify a stopped or a started container, and find out its specific CONTAINER_ID
. Then we can start a stopped container or stop a running one by providing the correct CONTAINER_ID
such as docker start CONTAINER_ID
. Examples are: docker start 03b53947d812
or docker stop a2fe12e61545
(the CONTAINER_ID
hashes will vary on your computer).
Sometimes you need to remove a container; for example, if you want to completely change its command line parameters when creating from an image. For removing a container, use the rm
command (but remember that it has to be stopped before): docker stop b7f720fbfd23; docker rm b7f720fbfd23
Linux containers are completely isolated processes running in a separated environment on your server and there is no way to log in to it like logging into a normal server using ssh
. If you need to access your containers BASH shell then you can run the docker exec
command, which is particularly useful for debugging problems or modifying your container (for example, installing new packages or updating programs or files in it). Note that this only works on running containers and you need to know your container's ID before (type docker ps
to find out) you run the following command: docker exec -it CONTAINER_ID /bin/bash,
for example docker exec -it d22ddf594f0d /bin/bash
. Once successfully attached to the container, you will see a slightly changed command-line prompt with the CONTAINER_ID
as hostname; for example, root@d22ddf594f0d:/var/www/html#
. If you need to exit your container, type exit
.