Docker, Horrible days and now!

Image from: https://justdrupal.com/content/images/2016/06/docker-image.png

Few days ago, I started learning docker. So, I was trying to find some good sources from where I can learn the docker and implement that. I was searching for two days and couldn’t found anything good. I tried some videos and articles. I messed up. IMHO, the problem when you try to learn by yourself is too many diversions. Like, I am watching a video from a YouTube channel and they follow a way. Whenever I try to switch to another one, they use something else. So, in two days I ended up nowhere. But, I found some terminologies and some common things they all used. Then one said, “You just can’t learn docker by watching. Try working on it. Then you’ll learn easily”.

Before you start reading, as I said, I am learning & implementing docker. Here in my article you MAY find a few misinformation. Apologize me for those things(if that happens).


What is Docker?

Okay. I am not pulling any quote from the docker website. AFAIK, in my simple understanding Docker wraps up all your project dependent environments. Like, when you say you will create a project with PHP, Apache and MySQL. In docker terminology you’ll need php-apache & MySQL containers for that.

Why should we use docker instead of local development environment?

It may have happened to you that you work on your own machine and you share your code via flash drive (seriously?) or Git or other Version Control System and you mate says, “What man? Why it’s not working?” Then you look into your code and you say, “Nah bro. It’s working on MY PC!” This is the problem. We were using our own local development. Our machine runs PHP-7.1 while his runs on PHP-7.0. That’s actually a problem. Docker manages that. It will help you managing the same environment across all the machines.


Installing Docker

My PC runs on Ubuntu-16.04. I installed the DOCKER CE. Just copy and paste those commands to your terminal and get the docker installed. I also installed DOCKER COMPOSE with pip. For the current article you don’t need to install docker compose. But I will use it on my next docker article. So, it’s good to install it. After installing docker and docker compose you can try these in your terminal to see the status and version.

sudo service docker status
docker --version && docker-compose --version

Install Apache & PHP with docker

As we have our docker installed, now we are going to install PHP & Apache. To do that we need to create a file. We will create a Dockerfile. Dockerfile is a textual representation of the commands to build an image. That image will be needed to boot the container. So, let’s create a file with that name anywhere in your file system and paste the following code.

FROM php:7.1.7-apache#COPY ./src /var/www/html/

Create a folder name src within that directory. Place a PHP file inside that. Lets create a file named and place that inside folder. Assume that this will be our working directory.

Folder structure & codes

Now, if we run (copy the dot too after the image name) in terminal, then an image with that name will be created.

docker build -t article-apache-php . (Creating docker image)

If you run in your terminal, then you can see the image is listed there.

docker images

Now, it’s time to boot the container and see some action. In the same terminal, where the Dockerfile is present if you type then you’ll see a hash has been generated.

docker run -v $(pwd)/src:/var/www/html -d -t -p 8001:80 article-apache-php

If you run you’ll see that your container is up and running if everything goes perfectly.

docker ps

Now, open your favorite browser, and hit http://127.0.0.1:8001/ and see that your file is showing the result.


Understanding of Dockerfile and commands

Dockerfile:

Dockerfile is the image builder. All the requirements & workaround are listed inside the file.

  • says, pull the image php 7.1.7-apache from https://hub.docker.com/. You can go there and search for your required image. If no image is found on docker hub, you’ll have to build it by yourself and use that.
  • is the comment. On second line, I have intentionally added a comment.
  • says, copy the directory from current directory to CONTAINER’s . The problem with the copy command is that, whenever you change in the file system of that directory, the change is not going to affect the container. This is not what we want.

Commands:

  • : Here, we’re telling docker to build and image we said in Dockerfile. adds a tag to your image. And, after the flag you give your image name. From the beginning we’re following convention. We’re using Dockerfile instead of any other name. If we want to use any custom name, you can use
  • : shows the images available in your host machine.
  • : Runs the image specified. flag mounts the specified volume to CONTAINER. This is how we overcome the COPY command problem inside the Dockerfile. flag says mount the folder from the current directory to container’s /var/www/html. /var/www/html is the location from where apache serves files. Left one is the HOST machine’s directory & right one is the container's directory, where it will be mounted. The changes on will be reflected there. flag says run in daemon mode. says, grab all the calls from PORT 8001 & map them to container’s PORT 80. Finally, the image name to boot.
  • : shows the list of containers. Other options are available. Try

Other commands:

  • List all docker containers:
  • List active containers:
  • List all containers(only ids):
  • Stop a container: or
  • Stop all containers:
  • Remove all containers:
  • Remove all images:

SSH into docker container

Sometimes, we need to ssh into docker container. To do that we can run the following command. or . Example: The above command will propmt an interactive tty pointing to

So far, that’s it. I tried to my best to make everyone understand. A long post. But, I think someone should give this much time to learn a new thing. If you find anything wrong here, please feel free to inform. I am also learning this. And I’ll update this one.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade