I’m using Docker since a couple of months and I am really excited about this piece of software. Docker makes it really easy to develop your application, put it in a container and run it on a server. Sure, Docker gives you many more possibilities (like scaling or clustering), but I want to start with the simple things first.
Since I am using macOS, I’m glad that you can use Docker natively on macOS since a couple of month. You can download Docker for Mac and Docker for Windows here (you don’t need any other tools like VirtualBox to run Docker).
Once you’ve installed Docker for Mac, open your terminal to check if the installation worked correct. The command
docker version should give you some basic information about your local Docker installation.
Anyway, let’s go back on track. This article is not only about installing Docker on you computer, I also want to describe how a webserver can be run with Docker. When you are using Docker, you wrap your software into containers. There are already many ready-to-use containers available at the Docker Hub. You can download these images by using the docker pull command (for example
docker pull nginx).
A Docker image can have multiple versions (tags), if you don’t specify a tag in the
docker pull command, the latest version will be downloaded. If you are using Docker on your production servers, you might want to define which version of an application is used. Fortunately it is not that hard to use a specific version. The repository information website will give you an overview about the available versions / tags (have a look at the MySQL repository). Typically the repository information website also describes how you can run the image or which environment variables are available.
Okay, you’ve already downloaded two Docker images until now. You can get a list of your local images with the
docker images command.
Fine, let’s run the NGINX based webserver on your machine. To start containers, you need to use the docker run command. As we want to access the webserver, we need to specify a port mapping by using
-p $HOSTPORT:$CONTAINERPORT. By default, NGINX is listening to port 80, which is then our
$CONTAINERPORT. For your
$HOSTPORT I have chosen port 8080. Docker also needs to know the name of the image we want to run, in our case we use
nginx which we have downloaded earlier.
Open up your terminal again and type the following command to run your webserver:
docker run -p 8080:80 nginx
Once the command was executed successfully, you can open your browser and navigate to http://localhost:8080. If everything worked correct, you should see the NGINX default page.
Because the container was not started as a daemon (don’t care about that now), all logging output from your NGINX webserver is displayed in your terminal.
As there is not much to see on your webserver right now, you might want to stop it. Just use CTRL + C and you will force your container to stop.
This was really a very basic hands-on guide with Docker and I hope I was able to make you curious about it’s possibilities.
You can read my article about Docker Compose to continue your Docker journey.
If you want to play around with Docker I can suggest you the following resources: