Docker, Horrible days and now!

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.


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 index.php and place that inside src folder. Assume that this src will be our working directory.

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

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

Now, it’s time to boot the container and see some action. In the same terminal, where the Dockerfile is present if you type docker run -v $(pwd)/src:/var/www/html -d -t -p 8001:80 my-docker-image-name then you’ll see a hash has been generated.

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

Now, open your favorite browser, and hit http://127.0.0.1:8001/ and see that your index.php 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.
FROM php:7.1.7-apachesays, 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.COPY ./src /var/www/html/says, copy thesrcdirectory from current directory to CONTAINER’s/var/www/html. 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:
docker build -t article-apache-php .: Here, we’re telling docker to build and image we said in Dockerfile.-tadds a tag to your image. And, after the-tflag 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 usedocker build -f MyDockerfile -t another-article .docker images: shows the images available in your host machine.docker run -v $(pwd)/src:/var/www/html -d -t -p 8001:80 article-apache-php: Runs the image specified.-vflag mounts the specified volume to CONTAINER. This is how we overcome the COPY command problem inside the Dockerfile.-vflag says mount thesrcfolder 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 onsrcwill be reflected there.-dflag says run in daemon mode.-psays, grab all the calls from PORT 8001 & map them to container’s PORT 80. Finally, the image name to boot.docker ps: shows the list of containers. Other options are available. Try--help
Other commands:
- List all docker containers:
docker ps -a - List active containers:
docker ps - List all containers(only ids):
docker ps -aq - Stop a container:
docker stop container_nameordocker stop container_id - Stop all containers:
docker stop $(docker ps -aq) - Remove all containers:
docker rm $(docker ps -aq) - Remove all images:
docker rmi $(docker images -q)
SSH into docker container
Sometimes, we need to ssh into docker container. To do that we can run the following command. docker exec -it container-name command or docker exec -it container-id command. Example: docker exec -it article-part-1 The above command will propmt an interactive tty pointing to /var/www/html
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.
