In this docker tutorial series, my aim is to explain basics of docker and to do that with min number of words but maximum number of examples. Before doing anything I want you to install docker on your machine and I hope you have an idea what docker does. If not please check docker.com.
Now let me explain two basic things related to docker:
image: File to be used in docker container. You may have a docker image of mysql for instance. It will contain an operating system, and whatever is needed to make mysql run.
container: A running instance of an image. So if you have an image of mysql, you can use it and run mysql on docker.
Now let’s see why we need these two.
First open a command prompt or terminal and write
This should give you details of your docker server and client similar to
Client: Docker Engine – Community
Server: Docker Engine – Community
This means we installed docker successfully and good to go.
Now let’s download an image and start a container using it. I’ll use nginx which’s a web server + load balancer and perfect for my simple examples.
docker container run nginx
which gives me this
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
Seems to be working right? Your docker first checks your local repo for images, if you don’t have this image it checks docker hub and download the latest version of this image because you did not specify it.
Let’s open our browser and type localhost. It’s a web server so there should be something in localhost:80 right?
Hmm that does not seem right. Maybe we did something wrong. Did we bind container’s port to our machine? I think we missed that.
docker container run –publish 1234:80 nginx
But which one’s which? Left is your host machine, right is the container port.
Now let’s check our browser by going to http://localhost:1234/.
Nice! I don’t want to have a terminal window for every container I run so I hope engineers in docker find a solution to that.
docker container run –publish 1234:80 –detach nginx
Cool! I even have a huge alphanumeric thing. Also nginx is working on port 1234!
Let’s check which containers are running right now:
docker container ls
Wait a minute that container id is similar to the one I got in the previous step! Also I can see my port binding under ports. Awesome!
What if I want to check some logs?
docker container logs 66f
will return nginx logs of my container. I did not write the whole id but docker’s super-smart as you can see. I have logs but this does not work like tail -f. Then try
docker container logs -f 66f
Then go to localhost:1234 for a few times and see the access log scrolling.
Let’s stop the container before saying goodbye.
docker container stop 66f
Ok. It’s stopped I guess? Should we check it?
docker container ls
Nothing’s there, that’s good news. You can also see your container cemetery:
docker container ls -a
In this part of my docker tutorial, you learnt two basics concepts related to docker: container and image.
Also you learnt how to run containers, to check running container logs, to stop containers.