Introduction to Docker - Part 4

Introduction to Docker - Part 4

Welcome to part 4 of my Introduction to Docker blog post series! In part 1 I quickly went through installing docker and what containers are and in Part 2 we actually played with Docker containers a little bit by running a couple of Docker containers, in Part 3 we went got introduced to the Docker images and some basic operations. If you've not read the previous posts I encourage you to do so:

https://bobbyiliev.com/blog/introduction-to-docker-part-1

https://bobbyiliev.com/blog/introduction-to-docker-part-2

https://bobbyiliev.com/blog/introduction-to-docker-part-3

In this post I'll show you how to work with a Dockerfile

I'll be using DigitalOcean for all of the demos, so I would strongly encourage you to create a DigitalOcean account follow along. You would learn more by doing!

To make things even better you can use my referral link to get a free $50 credit that you could use to deploy your virtual machines and test the guide yourself on a few DigitalOcean servers:

Digital Ocean $50 Free Credit

Once you have your account here's how to deploy your first droplet/server:

https://www.digitalocean.com/docs/droplets/how-to/create/

I'll be using Ubuntu 18.04 so I would recommend that you stick to the same so you could follow along.

What is a Dockerfile?

A Dockerfile is basically a text file that contains all of the required commands to build a certain image. The Dockerfile reference page: https://docs.docker.com/engine/reference/builder/ lists the various commands and format details for Dockerfiles.

Dockerfile example

Here's a really simple example of how to create a Dockerfile and add our source code to an image:

I have a simple Hello world index.html file in my current directory that I would add to the container with the following content:

<h1>Hello World - Bobby Iliev</h1>

DockerFile example:

FROM webdevops/php-apache-dev
MAINTAINER Bobby I.
COPY . /var/www/html
WORKDIR /var/www/html
EXPOSE 8080

Example:

Dockerfile example

Quick rundown of the File:

FROM - The image that we would use as a ground

MAINTAINER - The person who would be maintaining the image

COPY - Copy some files in the image

WORKDIR - The directory where you want to run your commands on start

EXPOSE - Specify a port that you would like to access the container on

Now it is time to run the build command:

docker build -f Dockerfile -t your-name/php-apache-dev .

After the built is complete you can list your images with the docker images command and also run it:

docker run -d -p 8080:80 bobby/php-apache-dev

And again just like we did in the last tutorial we can go ahead and publish our image:

docker login

docker push your-docker-user/name-of-image-here

Then you will be able to see your new image in your Docker Hub account (https://hub.docker.com) you can pull from the hub directly:

docker pull your-docker-user/name-of-image-here

Conclusion

Of course this is a really simple example, you could go above and beyond with your Dockerfiles!

That is pretty much it! Now you  know how to pull, modify and push images!

We are ready to move to Part 4 of the Introduction to Docker blog post series:

https://bobbyiliev.com/blog/introduction-to-docker-part-5

Let me know if you have any questions!

Coffee For Me
Recent Posts
Proxies Demystified
Proxies Demystified
2019-11-05 09:31:05